From 55827f965e8cbf5069f4c786ef8c9fbceaae4ad3 Mon Sep 17 00:00:00 2001 From: Robrecht Cannoodt Date: Mon, 2 Dec 2024 11:19:14 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20error=20message=20when=20f?= =?UTF-8?q?etching=20the=20instance=20details=20fails=20(#123)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/InstanceAPI.R | 27 ++++----------------------- R/connect.R | 5 +---- R/utils.R | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/R/InstanceAPI.R b/R/InstanceAPI.R index d503ee1..5901f53 100644 --- a/R/InstanceAPI.R +++ b/R/InstanceAPI.R @@ -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. @@ -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. @@ -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. @@ -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` @@ -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 } ) ) diff --git a/R/connect.R b/R/connect.R index 23720fb..378a2cd 100644 --- a/R/connect.R +++ b/R/connect.R @@ -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")) } diff --git a/R/utils.R b/R/utils.R index 57f2eaa..350afb3 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 +}