Skip to content

Add response body to error when fetching access token #4005

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Nashluffy
Copy link

We spent some time trying to debug why we were getting a 403 from GitHub when the credentials worked fine elsewhere, would be super useful if the body was present in the error.

@@ -1056,10 +1056,14 @@ func (c *Client) fetchAccessToken(ctx context.Context, gitHubConfigURL string, c
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
body, err := io.ReadAll(resp.Body)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the response status is not created, and if reading the body fails, we would lose the error type GitHubAPIError which contains the RequestID. This request ID is important because we can troubleshoot the issues related to the request on the back end.

Maybe something along the lines of:

errMsg := fmt.Sprintf("failed to get access token for GitHub App auth (%v)", resp.Status)
if body, err := io.ReadAll(resp.Body); err == nil {
  errMsg = fmt.Sprintf("%s: %s", errMsg, string(body))
}

return nil, &GitHubAPIError{
  StatusCode: resp.StatusCode,
  RequestID:  resp.Header.Get(HeaderGitHubRequestID),
  Err:        errors.New(errMsg),
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants