Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix error message when fetching the instance details fails #123

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading