diff --git a/DESCRIPTION b/DESCRIPTION index 7d62ad6..d33baa9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,6 +12,7 @@ Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 Imports: + glue, httr Suggests: knitr, diff --git a/NAMESPACE b/NAMESPACE index 6441900..0a75ac3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,5 +11,8 @@ importFrom(httr,GET) importFrom(httr,POST) importFrom(httr,add_headers) importFrom(httr,content) +importFrom(httr,http_error) +importFrom(httr,http_status) +importFrom(httr,status_code) importFrom(httr,stop_for_status) importFrom(httr,timeout) diff --git a/R/cancel.R b/R/cancel.R index 9a8d1df..082056f 100644 --- a/R/cancel.R +++ b/R/cancel.R @@ -11,7 +11,6 @@ proof_cancel <- function() { proof_header(), timeout(proofr_env$timeout_sec) ) - # FIXME: better error handling - surface error messages - stop_for_status(response) + stop_for_message(response) content(response, as = "parsed") } diff --git a/R/http.R b/R/http.R new file mode 100644 index 0000000..c431700 --- /dev/null +++ b/R/http.R @@ -0,0 +1,13 @@ +stop_for_message <- function(response) { + if (http_error(response)) { + parsed <- tryCatch(content(response), error = function(e) e) + if (inherits(parsed, "error")) stop_for_status(response) + if (!is.list(parsed)) stop_for_status(response) + msg <- glue::glue( + "{http_status(response)$reason}", + " (HTTP {status_code(response)})", + " {parsed$message}" + ) + stop(msg, call. = FALSE) + } +} diff --git a/R/proofr-package.R b/R/proofr-package.R index a13fd5f..801d48c 100644 --- a/R/proofr-package.R +++ b/R/proofr-package.R @@ -3,6 +3,7 @@ ## usethis namespace: start #' @importFrom httr GET POST DELETE add_headers content -#' stop_for_status timeout +#' stop_for_status timeout http_status status_code +#' http_error ## usethis namespace: end NULL diff --git a/R/start.R b/R/start.R index 9d698b2..ebe5d60 100644 --- a/R/start.R +++ b/R/start.R @@ -19,7 +19,6 @@ proof_start <- function(pi_name = NULL) { encode = "json", timeout(proofr_env$timeout_sec) ) - # FIXME: better error handling - surface error messages - stop_for_status(response) + stop_for_message(response) content(response, as = "parsed") } diff --git a/R/status.R b/R/status.R index fb0e861..a41ff3d 100644 --- a/R/status.R +++ b/R/status.R @@ -21,6 +21,6 @@ proof_status <- function() { proof_header(), timeout(proofr_env$timeout_sec) ) - stop_for_status(response) + stop_for_message(response) content(response, as = "parsed") }