R/error_check.R

Defines functions error_check

#'
#'
error_check <- function(resp,text) {
  if (httr::http_error(resp)) {
    if(httr::http_type(resp) == "application/json") {
      parsed <- jsonlite::fromJSON(httr::content(resp, "text"),
                                 simplifyVector = FALSE)
    } else {
      parsed <- NULL
      parsed$error$message = "No error message."
    }
    stop(
      sprintf(
        "%s (HTTP %d). %s.\n %s",
        httr::http_status(httr::status_code(resp))$reason,
        httr::status_code(resp),
        text,
        parsed$error$message
      ),
      call. = FALSE
    )
  }
}
TR-API-Samples/Example.TRTH.R.Package documentation built on Nov. 1, 2021, 5:31 a.m.