R/check_for_failed_request.R

Defines functions check_for_failed_request

Documented in check_for_failed_request

#' Check for failed API request
#'
#' This function checks the response body from an API request to determine if the request failed.
#' If the request contains an error message, the function will raise an error with a detailed
#' message including the message ID and value.
#'
#' @param body The response body from the API request, typically in JSON format,
#' which may contain an error message if the request failed.
#'
#' @return This function does not return a value. If the request contains an error message,
#' it raises an error and halts execution.
#'
#' @details This function inspects the first element of the response body to check for
#' an error message. If a message is found, the function retrieves the `id`, `key`, and
#' `value` fields from the error message and constructs an error using `cli::cli_abort()`.
#' The function is intended to ensure failed API requests are handled gracefully.
#'
check_for_failed_request <- function(body) {
  if (!is.null(body[[1]]$message)) {

    message_id <- body[[1]]$message[[1]]$id
    message_key <- body[[1]]$message[[1]]$key
    message_value <- body[[1]]$message[[1]]$value

    cli::cli_abort(
      "Request failed with message ID {message_id}: {message_value}."
    )
  }
}

Try the wdi2 package in your browser

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

wdi2 documentation built on Sept. 13, 2024, 1:12 a.m.