Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JBGruber committed Sep 21, 2023
1 parent 8c10f82 commit 4092e01
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
19 changes: 13 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@ Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
Version: 4.0.12.9000
Authors@R:
c(person("Wouter", "van Atteveldt", email = "[email protected]", role = c("aut", "cre")),
person("Johannes", "Gruber", email = "[email protected]", role = "ctb"))
c(person(given = "Wouter",
family = "van Atteveldt",
email = "[email protected]",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-1237-538X")),
person(given = "Johannes B.",
family = "Gruber",
email = "[email protected]",
role = c("aut", "ctb"),
comment = c(ORCID = "0000-0001-9177-1772")))
Imports:
cli,
dplyr,
glue,
httr2,
jsonlite,
methods,
progress,
pillar,
purrr,
rappdirs,
rlang,
remotes,
rlang,
tibble,
utils,
pillar
utils
Suggests:
covr,
dockr,
Expand Down
35 changes: 23 additions & 12 deletions R/lib.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ request_response <- function(credentials,
method = "GET",
body = NULL,
error_on_404 = TRUE,
max_tries = NULL,
auto_unbox = TRUE,
...) {

Expand All @@ -39,7 +40,12 @@ request_response <- function(credentials,
error_on_404 ,
httr2::resp_status(resp) >= 400),
body = amcat_error_body
)
) |>
# for uploads, we sometimes get 500/502 when elastic is processing new documents
# in these cases amcat4 reports a server error because the connection times out.
# It makes sense to wait a little and retry
httr2::req_retry(max_tries = max_tries,
is_transient = function(x) httr2::resp_status(x) %in% c(429, 500, 502, 503))

if (!is.null(body)) {
req <- req |>
Expand Down Expand Up @@ -82,20 +88,25 @@ make_path <- function(...) {
#' @noRd
amcat_error_body <- function(resp) {

pkg.env$resp <- resp

if (grepl("json", httr2::resp_content_type(resp), fixed = TRUE)) {
ebody <- httr2::resp_body_json(resp)
# TODO: find a cleaner way to parse this
msg <- try(ebody[["detail"]][[1]][["msg"]])
if (methods::is(msg, "try-error")) msg <- NULL
detail <- try(toString(ebody[["detail"]][[1]][["loc"]]))
if (methods::is(detail, "try-error")) detail <- toString(ebody[["detail"]])
error <- c(
ebody$error,
paste0(msg, detail, .sep = ": ")
)

if (is.list(ebody$detail$body$error)) {
error <- purrr::map_chr(names(ebody$detail$body$error), function(n) {
paste0(tools::toTitleCase(n), ": ", ebody$detail$body$error[[n]])
})
} else {
# TODO: find a cleaner way to parse this
msg <- try(ebody[["detail"]][[1]][["msg"]], silent = TRUE)
if (methods::is(msg, "try-error")) msg <- NULL
detail <- try(toString(ebody[["detail"]][[1]][["loc"]]), silent = TRUE)
if (methods::is(detail, "try-error")) detail <- toString(ebody[["detail"]])
error <- paste0(msg, detail, .sep = ": ")
}

} else {
# if no further information is returned, revert to httr2 default by
# returning NULL
error <- NULL
}

Expand Down

0 comments on commit 4092e01

Please sign in to comment.