in my package, there is a function calling a https-address.
I am checking with curl::has_internet()
for an internet connection and with httr::http_error()
for http-errors.
My problem is:
if this function gets an invalid URL, like https://foobarfoo.bar, the function dies with 'Couldn't resolve host name'
My question is:
is there a way to keep the function alive, throwing "NA" or something instead of dying? E.g. is there a way to "safely" test for the host name before executing my function?
CodePudding user response:
You can use the tryCatch
function with the error
argument to solve this issue:
tryCatch(curl::nslookup("foobardodod.fd"), error = function(e) NA)
returns
#> NA
as defined in error
, if curl::nslookup("foobardodod.fd")
fails.