Skip to content

Commit

Permalink
🐛 Fix error message when fetching the instance details fails (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood authored Dec 2, 2024
1 parent 362d285 commit 55827f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
27 changes: 4 additions & 23 deletions R/InstanceAPI.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
httr::add_headers(.headers = private$get_headers())
)

private$process_response(response, "get schema")
process_httr_response(response, "get schema from instance")
},
#' @description
#' Get a record from the instance.
Expand Down Expand Up @@ -106,7 +106,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
body = body
)

private$process_response(response, "get record")
process_httr_response(response, "get record from instance")
},
#' @description
#' Get a summary of available records from the instance.
Expand Down Expand Up @@ -191,7 +191,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
body = body
)

private$process_response(response, "get record")
process_httr_response(response, "get record from instance")
},
#' @description
#' Delete a record from the instance.
Expand Down Expand Up @@ -224,7 +224,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
)
)

private$process_response(response, "delete record")
process_httr_response(response, "delete record from instance")
},
#' @description
#' Print an `API`
Expand Down Expand Up @@ -266,25 +266,6 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
}

return(headers)
},
process_response = function(response, request_type) {
content <- httr::content(response)
if (httr::http_error(response)) {
if (is.list(content) && "detail" %in% names(content)) {
detail <- content$detail
if (is.list(detail)) {
detail <- jsonlite::minify(jsonlite::toJSON(content$detail))
}
} else {
detail <- content
}
cli_abort(c(
"Failed to {request_type} from instance with status code {response$status_code}",
"i" = "Details: {detail}"
))
}

content
}
)
)
5 changes: 1 addition & 4 deletions R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,8 @@ connect <- function(slug = NULL) {
),
body = body
)
content <- httr::content(request)
content <- process_httr_response(request, "connect to instance")

if (httr::http_error(request)) {
cli_abort(content)
}
if (length(content) == 0) {
cli_abort(paste0("Instance '", owner, "/", name, "' not found"))
}
Expand Down
27 changes: 27 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,30 @@ detect_path <- function() {

return(current_path)
}

#' Resolve an httr response with error handling
#'
#' @param response An httr response object
#' @param request_type A string describing the request type
#'
#' @return The content of the response if successful
#' @noRd
process_httr_response <- function(response, request_type) {
content <- httr::content(response)
if (httr::http_error(response)) {
if (is.list(content) && "detail" %in% names(content)) {
detail <- content$detail
if (is.list(detail)) {
detail <- jsonlite::minify(jsonlite::toJSON(content$detail))
}
} else {
detail <- content
}
cli_abort(c(
"Failed to {request_type} with status code {response$status_code}",
"i" = "Details: {detail}"
))
}

content
}

0 comments on commit 55827f9

Please sign in to comment.