Skip to content

Commit

Permalink
Merge pull request #15 from conbench/2nd-try-verbose-error
Browse files Browse the repository at this point in the history
2nd try verbose error
  • Loading branch information
boshek authored Sep 21, 2023
2 parents 14b6e65 + 4c632c6 commit 34f8e7e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: conbenchcoms
Title: An API wrapper for conbench communications
Version: 0.0.4
Version: 0.0.5
Authors@R: c(
person("Jonathan", "Keane", , "[email protected]", role = c("aut", "ctb"),
comment = c(ORCID = "0000-0001-7087-9776")),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# conbenchcoms 0.0.5
* display more verbose errors when the Conbench API returns an error

# conbenchcoms 0.0.4
* add threshold endpoints to conbenchcoms

Expand Down
29 changes: 22 additions & 7 deletions R/session.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
#' @export
conbench_perform <- function(data, ...) {
# if session is already here, then we can use that
# Make an initial call to see if we need to authenticate
resp <- data |>
req_error(is_error = function(resp) FALSE) |>
req_headers(cookie = .conbench_session$cookie) |>
req_perform(...)

# TODO: is this status too narrow?
if (resp_status(resp) == 401L) {
auth_conbench()
# Authenticate if we need to
if (resp_status(resp) == 401L) auth_conbench()

resp <- data |>
req_headers(cookie = .conbench_session$cookie) |>
req_perform(...)
}
# Run the request again with better error handling
resp <- data |>
req_error(body = error_body) |>
req_headers(cookie = .conbench_session$cookie) |>
req_perform(...)

resp
}
Expand Down Expand Up @@ -64,6 +65,20 @@ get_config <- function() {
creds
}

error_body <- function(resp) {
method <- indent(glue::glue("{resp['method']} {resp['url']}"))
status_code <- indent(glue::glue("Status code: {resp[['status_code']]}"))
indented_message <- indent(resp_body_string(resp))
glue::glue("Request details:\n{method}\n\nResponse details:\n{status_code}\n---\n\n{indented_message}")
}

indent <- function(message) {
lines <- unlist(strsplit(message, "\n")) # Split the message into lines
indent <- paste(rep(" ", 2), collapse = "") # Create the indentation string
indented_lines <- paste(indent, lines, sep = "") # Add indentation
paste(indented_lines, collapse = "\n") # Combine the lines with newline characters
}

.conbench_session <- new.env(parent = emptyenv())


0 comments on commit 34f8e7e

Please sign in to comment.