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

Handle streaming errors like regular errors #492

Merged
merged 2 commits into from
Jul 14, 2024
Merged

Handle streaming errors like regular errors #492

merged 2 commits into from
Jul 14, 2024

Conversation

hadley
Copy link
Member

@hadley hadley commented Jul 11, 2024

Fixes #479

@jwimberl how does this look to you?

@jwimberl
Copy link

Testing this branch, I see uniform behavior btw req_perform and req_perform_stream in one of the reprex cases from #479:

packageVersion("httr2")
#> [1] '1.0.1.9000'
library(httr2)
req <- request("https://api.github.com/asdfsdadf")
a <- req |> req_perform()
#> Error in `req_perform()`:
#> ! HTTP 404 Not Found.
last_response() |> resp_body_string()
#> [1] "{\n  \"message\": \"Not Found\",\n  \"documentation_url\": \"https://docs.github.com/rest\",\n  \"status\": \"404\"\n}\n"
b <- req |> req_perform_stream(callback = \(z) TRUE)
#> Error in `req_perform_stream()`:
#> ! HTTP 404 Not Found.
last_response() |> resp_body_string()
#> [1] "{\n  \"message\": \"Not Found\",\n  \"documentation_url\": \"https://docs.github.com/rest\",\n  \"status\": \"404\"\n}\n"

Created on 2024-07-12 with reprex v2.1.1

So, both lead to an R error, and last_response() contains the error body in both cases, which looks good to me, as this most likely how I would want to call endpoints like this.

I do see a slight difference behavior in the other reprex case:

packageVersion("httr2")
#> [1] '1.0.1.9000'
library(httr2)
req <- request("https://api.github.com/asdfsdadf")
a <- req |> req_error(is_error = \(z) FALSE) |> req_perform()
last_response() |> resp_body_string()
#> [1] "{\n  \"message\": \"Not Found\",\n  \"documentation_url\": \"https://docs.github.com/rest\",\n  \"status\": \"404\"\n}\n"
b <- req |> req_error(is_error = \(z) FALSE) |> req_perform_stream(callback = \(z) TRUE)
last_response() |> resp_body_string()
#> Error in `resp_body_raw()`:
#> ! Can't retrieve empty body.

Created on 2024-07-12 with reprex v2.1.1

but this makes sense too -- if I'm overriding things to tell it its not an error, of course it would leave the body to be handled by the callback. So, this seems consistent to me as well.

@hadley
Copy link
Member Author

hadley commented Jul 13, 2024

@jwimberl thanks for the thoughtful review. I really appreciate it 😄

@hadley hadley merged commit a434f9c into main Jul 14, 2024
13 checks passed
@hadley hadley deleted the stream-error branch July 14, 2024 15:22
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.

Cannot capture 4xx response bodies with req_perform_stream
2 participants