R/test_url.R

Defines functions warn_connection_failure test_url

#' Tests if a URL can be accessed; raises a warning if not
#'
#' @param url URL to test
#' @noRd
test_url <- function(url) {
  url_exists <- FALSE

  tryCatch(
    {
      result <- GET(url)
      connection_success <- result$status_code == 200
      if (connection_success) {
        url_exists <- TRUE
      } else {
        warn_connection_failure(url)
      }
    },
    error = function(e) {
      if (grepl("Could not resolve host", e)) {
        warn_connection_failure(url)
      } else {
        e
      }
    }
  )

  return(url_exists)
}

warn_connection_failure <- function(url) {
  warning(paste0("URL ", url, " could not be reached"), call. = FALSE)
}

Try the codewhere package in your browser

Any scripts or data that you put into this service are public.

codewhere documentation built on June 22, 2024, 10:40 a.m.