You are welcome!
To elaborate a little on my post: I actually have the if clause a bit more complex, as I test for either
- the http_error (= network actually being down) or
- an environment variable simulating the network being down.
This way I can have a test_that expectation that tests the evaluation of the condition of a network failure (it was sort of difficult to fake a network being down in a remote CI workflow, such as Travis or CRAN, and this was the best I could think of).
This is the code that I actually use, including the part from unit tests (it is not an expect_error(), but expect_message() - an error would not be considered as a graceful fail :))
As for the timeout error I have left this to {curl} to handle, which may not be an option in your case.
# in my function init:
network <- as.logical(Sys.getenv("NETWORK_UP", unset = TRUE)) # dummy variable to allow testing of network
# actual evaluation
if (httr::http_error(remote_file) | !network) { # network is down = message (not an error anymore)
message("No internet connection or data source broken.")
return(NULL)
} else { # network is up = proceed to download via curl
message("RCzechia: downloading remote dataset.")
curl::curl_download(url = remote_file, destfile = local_file, quiet = T)
} # /if - network up or down
# in my tests
Sys.setenv("NETWORK_UP" = FALSE)
expect_message(chr_uzemi(), "internet") # message about faulty internet connection
Sys.setenv("NETWORK_UP" = TRUE)