R/handle_download_error.R

Defines functions handle_download_error

#' Handle Download Errors Gracefully
#'
#' Wraps a download function in a `tryCatch` block. On error, it emits an
#' informational message and returns `fallback` instead of stopping.
#'
#' @param download_function A function to call for downloading or processing a
#'   resource.
#' @param ... Additional arguments passed to `download_function`.
#' @param fallback The value to return when `download_function` raises an
#'   error. Defaults to `NULL`.
#'
#' @returns The result of `download_function(...)`, or `fallback` on error.
#'
#' @keywords internal
#' @noRd
handle_download_error <- function(download_function, ..., fallback = NULL) {
  tryCatch(
    {
      download_function(...)
    },
    error = function(e) {
      cli::cli_inform(
        paste0(
          "Failed to download or process the resource. ",
          "The resource may not be available, or the URL may have changed. ",
          "Error message: {.message {e$message}}"
        )
      )
      fallback
    }
  )
}

Try the tidyfinance package in your browser

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

tidyfinance documentation built on Oct. 6, 2026, 1:06 a.m.