Skip to content

Commit

Permalink
Improved printing of the HTTP error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Apr 23, 2024
1 parent 74f3565 commit fefa204
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion R/fetchDirectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fetchDirectory <- function(path, registry, url, cache=NULL, forceRemote=FALSE, o
}

req <- request(paste0(url, "/list?path=", URLencode(path), "&recursive=true"))
req <- req_error(req, body = function(res) resp_body_json(res)$reason)
req <- handle_error(req)
res <- req_perform(req)
listing <- resp_body_json(res)

Expand Down
2 changes: 1 addition & 1 deletion R/listFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ listFiles <- function(project, asset, version, registry, url, prefix=NULL, inclu
target <- paste0(target, "/", prefix)
}
req <- request(paste0(url, "/list?path=", URLencode(target, reserved=TRUE), "&recursive=true"))
req <- req_error(req, body = function(res) resp_body_json(res)$reason)
req <- handle_error(req)
res <- req_perform(req)

listing <- unlist(resp_body_json(res))
Expand Down
2 changes: 1 addition & 1 deletion R/listProjects.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ list_registry_directories <- function(path, registry, url, forceRemote) {
url <- paste0(url, "?path=", URLencode(path, reserved=TRUE))
}
req <- request(url)
req <- req_error(req, body = function(res) resp_body_json(res)$reason)
req <- handle_error(req)
res <- req_perform(req)
listing <- unlist(resp_body_json(res))
dirs <- listing[endsWith(listing, "/")]
Expand Down
2 changes: 1 addition & 1 deletion R/serviceInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' @import httr2
serviceInfo <- function(url) {
req <- request(paste0(url, "/info"))
req <- req_error(req, body = function(res) resp_body_json(res)$reason)
req <- handle_error(req)
res <- req_perform(req)
resp_body_json(res)
}
15 changes: 14 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ sanitize_uploaders <- function(uploaders) {
uploaders
}

handle_error <- function(req) {
req_error(req, body = function(res) {
ct <- resp_content_type(res)
if (ct == "application/json") {
resp_body_json(res)$reason
} else if (ct == "text/plain") {
resp_body_string(res)
} else {
NULL
}
})
}

#' @importFrom jsonlite toJSON
#' @import httr2
dump_request <- function(staging, url, action, payload) {
Expand All @@ -46,7 +59,7 @@ dump_request <- function(staging, url, action, payload) {

req <- request(paste0(url, "/new/", basename(actual)))
req <- req_method(req, "POST")
req <- req_error(req, body = function(res) resp_body_json(res)$reason)
req <- handle_error(req)
res <- req_perform(req)
resp_body_json(res)
}

0 comments on commit fefa204

Please sign in to comment.