Skip to content

Commit

Permalink
Ensure req_perform_parallel() respects req_error() settings (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
gergness committed Jul 16, 2024
1 parent 18b1a49 commit ec498a3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# httr2 (development version)

* `req_perform_parallel()` now respects error handling in `req_error()`
* New function `req_perform_promise()` allows creating a `promises::promise` for a request that runs in the background (#501, @gergness).

# httr2 1.0.2
Expand Down
2 changes: 1 addition & 1 deletion R/multi-req.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ Performance <- R6Class("Performance", public = list(
)
resp <- cache_post_fetch(self$req, resp, path = self$path)
self$resp <- tryCatch(
resp_check_status(resp, error_call = self$error_call),
handle_resp(self$req, resp, error_call = self$error_call),
error = identity
)
if (is_error(self$resp)) {
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/multi-req.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
Error in `req_perform_parallel()`:
! Could not resolve host: INVALID

# req_perform_parallel respects http_error() body message

Code
req_perform_parallel(reqs)
Condition
Error in `req_perform_parallel()`:
! HTTP 404 Not Found.
* hello

# multi_req_perform is deprecated

Code
Expand Down
23 changes: 21 additions & 2 deletions tests/testthat/test-multi-req.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test_that("can perform >128 file uploads in parallel", {
temp <- withr::local_tempfile(lines = letters)
req <- request(example_url()) %>% req_body_file(temp)
reqs <- rep(list(req), 150)

expect_no_error(req_perform_parallel(reqs, on_error = "continue"))
})

Expand Down Expand Up @@ -53,7 +53,7 @@ test_that("can download 0 byte file", {
test_that("objects are cached", {
temp <- withr::local_tempdir()
req <- request_test("etag/:etag", etag = "abcd") %>% req_cache(temp)

expect_condition(
resps1 <- req_perform_parallel(list(req)),
class = "httr2_cache_save"
Expand Down Expand Up @@ -115,6 +115,25 @@ test_that("errors can cancel outstanding requests", {
expect_null(out[[2]])
})

test_that("req_perform_parallel resspects http_error() error override", {
reqs <- list2(
req_error(request_test("/status/:status", status = 404), is_error = ~FALSE),
req_error(request_test("/status/:status", status = 500), is_error = ~FALSE)
)
resps <- req_perform_parallel(reqs)

expect_equal(resp_status(resps[[1]]), 404)
expect_equal(resp_status(resps[[2]]), 500)
})


test_that("req_perform_parallel respects http_error() body message", {
reqs <- list2(
req_error(request_test("/status/:status", status = 404), body = ~"hello")
)
expect_snapshot(req_perform_parallel(reqs), error = TRUE)
})

test_that("multi_req_perform is deprecated", {
expect_snapshot(multi_req_perform(list()))
})

0 comments on commit ec498a3

Please sign in to comment.