rrcurlgeturl

Avoiding "Could not resolve host" error to stop the program running in R


I use the getURL function from the Rcurl package in R to read content from a list of links.

When trying to fetch a broken link of the list I get the error "Error in function (type, msg, asError = TRUE) : Could not resolve host:" and the program stops running.

I use the Try command to try to avoid the program stopping but it doesn´t work.

try(getURL(URL, ssl.verifypeer = FALSE, useragent = "R")

Any hint on how can I avoid the program to stop running when trying to get a broken link?


Solution

  • You need to be doing some type of error handling. I would argue tryCatch is actually better for your situation.

    I'm assuming you are inside a loop over the links, then you can check the response from your try/tryCatch to see if an error was thrown, and if so just move to the following iteration in your loop using next.

    status <- tryCatch(
        getURL(URL, ssl.verifypeer=FALSE, useragent="R"),
        error = function(e) e
    )
    
    if(inherits(status,  "error")) next