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

Return reponse header in error condition #117

Merged
merged 5 commits into from
Apr 7, 2022

Conversation

gadenbuie
Copy link
Contributor

@gadenbuie gadenbuie commented Jun 16, 2020

This small PR returns the response headers as $response in the error condition thrown from gh_error() to make it possible to use information returned in the headers by GitHub when handling the error.

In particular, this is useful when reaching rate limits as it would allow users to use the Retry-After or X-RateLimit-Reset properties to wait for the rate limit to reset or the retry-after period to pass.

# this search will quickly make too many requests per minute
# and trigger the abuse rate limit
err <- tryCatch(
  gh("/search/code", q = "foo", .limit = Inf),
  "http_error_403" = identity
)
err
#> <github_error in gh_process_response(raw): 
#> GitHub API error (403): 403 Forbidden
#> Message: You have triggered an abuse detection mechanism. Please wait a few minutes before you try again.
#> Read more at https://developer.github.com/v3/#abuse-rate-limits>

str(err$response[c("status", "retry-after")])
#> List of 2
#>  $ status     : chr "403 Forbidden"
#>  $ retry-after: chr "60"

I used response to align with the attribute name in a successful response.

x <- gh("/repos/r-lib/gh")
attributes(x)$response
#> ... response headers

Copy link
Member

@gaborcsardi gaborcsardi left a comment

Choose a reason for hiding this comment

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

Good idea! Can you please rename it to response_headers? Also, can you please add a bullet point to the NEWS? Thanks!

Btw. I wonder if we should just include the content of the response as well?

@gadenbuie
Copy link
Contributor Author

I renamed the headers item response_headers and added the response content as response_content.

I hope it's okay, but I rolled #116 into this PR as it also touches gh_error() and resolves an issue that occurs with conditional requests that don't return a response body. (In short, when the body is empty res is raw(0) so gh_error() fails when trying to access res$message.)

R/gh_response.R Outdated Show resolved Hide resolved
@gaborcsardi
Copy link
Member

Thanks again and sorry for the long wait! I removed #116 because I think we handle that elsewhere now.

@gaborcsardi gaborcsardi merged commit 61a9a47 into r-lib:main Apr 7, 2022
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.

None yet

3 participants