Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: return all token fetch related errors as structured #380

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions internal/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string,
func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
r, err := ctxhttp.Do(ctx, ContextClient(ctx), req)
if err != nil {
return nil, err
return nil, &RetrieveError{Err: err}
}
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1<<20))
r.Body.Close()
if err != nil {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
return nil, &RetrieveError{Err: err}
}
if code := r.StatusCode; code < 200 || code > 299 {
return nil, &RetrieveError{
Expand All @@ -251,7 +251,7 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
case "application/x-www-form-urlencoded", "text/plain":
vals, err := url.ParseQuery(string(body))
if err != nil {
return nil, err
return nil, &RetrieveError{Err: err}
}
token = &Token{
AccessToken: vals.Get("access_token"),
Expand Down Expand Up @@ -285,10 +285,15 @@ func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
}

type RetrieveError struct {
Err error
Response *http.Response
Body []byte
}

func (r *RetrieveError) Error() string {
return fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", r.Response.Status, r.Body)
if r.Err != nil {
return fmt.Sprintf("oauth2: cannot fetch token: %v", r.Err)
} else {
return fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", r.Response.Status, r.Body)
}
}
8 changes: 4 additions & 4 deletions jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ func (js jwtSource) Token() (*oauth2.Token, error) {
hc := oauth2.NewClient(js.ctx, nil)
resp, err := hc.PostForm(js.conf.Endpoint.TokenURL, v)
if err != nil {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
return nil, &oauth2.RetrieveError{Err: err}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
return nil, &oauth2.RetrieveError{Err: err}
}
if c := resp.StatusCode; c < 200 || c > 299 {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v\nResponse: %s", resp.Status, body)
return nil, &oauth2.RetrieveError{Response: resp, Body: body}
}

// tokenRes is the JSON response body.
Expand All @@ -129,7 +129,7 @@ func (js jwtSource) Token() (*oauth2.Token, error) {
ExpiresIn int64 `json:"expires_in"` // relative seconds from now
}
if err := json.Unmarshal(body, &tokenRes); err != nil {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
return nil, &oauth2.RetrieveError{Err: err}
}
token := &oauth2.Token{
AccessToken: tokenRes.AccessToken,
Expand Down
6 changes: 3 additions & 3 deletions jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func (js jwtSource) Token() (*oauth2.Token, error) {
v.Set("assertion", payload)
resp, err := hc.PostForm(js.conf.TokenURL, v)
if err != nil {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
return nil, &oauth2.RetrieveError{Err: err}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
return nil, &oauth2.RetrieveError{Err: err}
}
if c := resp.StatusCode; c < 200 || c > 299 {
return nil, &oauth2.RetrieveError{
Expand All @@ -145,7 +145,7 @@ func (js jwtSource) Token() (*oauth2.Token, error) {
ExpiresIn int64 `json:"expires_in"` // relative seconds from now
}
if err := json.Unmarshal(body, &tokenRes); err != nil {
return nil, fmt.Errorf("oauth2: cannot fetch token: %v", err)
return nil, &oauth2.RetrieveError{Err: err}
}
token := &oauth2.Token{
AccessToken: tokenRes.AccessToken,
Expand Down
11 changes: 9 additions & 2 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,21 @@ func retrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error)
}

// RetrieveError is the error returned when the token endpoint returns a
// non-2XX HTTP status code.
// non-2XX HTTP status code or fails with non-HTTP error
type RetrieveError struct {
// Err is the (net) error happened during HTTP request when response not received
Err error

Response *http.Response
// Body is the body that was consumed by reading Response.Body.
// It may be truncated.
Body []byte
}

func (r *RetrieveError) Error() string {
return fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", r.Response.Status, r.Body)
if r.Err != nil {
return fmt.Sprintf("oauth2: cannot fetch token: %v", r.Err)
} else {
return fmt.Sprintf("oauth2: cannot fetch token: %v\nResponse: %s", r.Response.Status, r.Body)
}
}