Skip to content
Merged
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
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_connection()` no longer errors with `no applicable method for 'close' applied to an object of class "c('httr2_failure', 'httr2_error', 'rlang_error', 'error', 'condition')` (#817).
* Refactor `url_modify()` to better retain exact formatting of URL components
that are not modified. (#788, #794)

Expand Down
7 changes: 5 additions & 2 deletions R/req-error.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ error_body <- function(req, resp, call = caller_env()) {
}

capture_curl_error <- function(code, call = caller_env()) {
resp <- tryCatch(
code,
tryCatch(
{
code
NULL
},
error = function(err) curl_cnd(err, call = call)
)
}
Expand Down
9 changes: 3 additions & 6 deletions R/req-perform-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ req_perform_connection <- function(
retry_check_breaker(req, tries)
sys_sleep(delay, "for retry backoff")

if (!is.null(resp)) {
if (is_response(resp)) {
close(resp)
}
resp <- req_perform_connection1(req, handle, blocking = blocking)
Expand Down Expand Up @@ -143,14 +143,11 @@ req_perform_connection1 <- function(req, handle, blocking = TRUE) {
err <- capture_curl_error({
conn <- curl::curl(req$url, handle = handle)
# Must open the stream in order to initiate the connection
withCallingHandlers(
open(conn, "rbf", blocking = blocking),
warning = \(cnd) tryInvokeRestart("muffleWarning"),
error = \(cnd) close(conn)
)
suppressWarnings(open(conn, "rbf", blocking = blocking))
body <- StreamingBody$new(conn)
})
if (is_error(err)) {
close(conn)
return(err)
}

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/req-perform-connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
Caused by error in `open()`:
! Failed to connect

# correclty reports curl error with retries (#817)

Code
req_perform_connection(req)
Condition
Error in `req_perform_connection()`:
! Failed to perform HTTP request.
Caused by error in `open()`:
! Failed to connect

# validates its input

Code
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-req-perform-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ test_that("curl errors become errors", {
expect_null(last_response())
})

test_that("correclty reports curl error with retries (#817)", {
local_mocked_bindings(open = function(...) abort("Failed to connect"))

req <- request("http://127.0.0.1") |>
req_retry(max_tries = 2, retry_on_failure = TRUE, backoff = \(i) 0)

expect_snapshot(req_perform_connection(req), error = TRUE)
})

test_that("mocking works", {
req_200 <- request("https://ok")
req_404 <- request("https://notok")
Expand Down
Loading