diff --git a/DESCRIPTION b/DESCRIPTION index ce67b95..6c5430f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: CyteTypeR Title: CyteType for R -Version: 0.4.1 +Version: 0.4.2 Description: CyteTypeR is the R version of CyteType python package. Authors@R: person("Nygen Analytics AB", , ,"contact@nygen.io", role = c("aut", "cre")) diff --git a/R/api.R b/R/api.R index d3ea693..50cd073 100644 --- a/R/api.R +++ b/R/api.R @@ -59,6 +59,10 @@ # Check for other non-success status codes if (status_code < 200 || status_code >= 300) { + parsed <- .parse_server_error(response) + if (!is.null(parsed)) { + stop(paste0("HTTP ", status_code, " [", parsed$error_code, "] ", parsed$message)) + } error_body <- tryCatch(resp_body_string(response), error = function(e) "Unknown error") stop(paste("HTTP", status_code, "error:", error_body)) } @@ -117,7 +121,7 @@ req_perform() status <- resp_status(resp) - if (status >= 400L) { + if (status >= 400) { stop(cytetype_api_error( message = paste0("Presigned upload rejected with HTTP ", status), call = "api" diff --git a/R/client.R b/R/client.R index 73f7d81..2fae837 100644 --- a/R/client.R +++ b/R/client.R @@ -30,7 +30,7 @@ req_timeout(connection_timeout) |> req_perform() |> resp_body_json(), - error = function(e) { .stop_if_rate_limited(e); stop(e) } + error = function(e) .stop_with_server_error(e, "Upload initiate failed") ) upload_id <- init_resp$upload_id @@ -82,7 +82,9 @@ list(ok = TRUE) }, error = function(e) { .stop_if_rate_limited(e) - list(ok = FALSE, chunk_idx = chunk_idx, message = conditionMessage(e)) + detail <- .format_server_error(e) + list(ok = FALSE, chunk_idx = chunk_idx, + message = detail %||% conditionMessage(e)) }) } @@ -99,7 +101,9 @@ list(ok = TRUE, etag = etag, part_number = chunk_idx + 1L) }, error = function(e) { .stop_if_rate_limited(e) - list(ok = FALSE, chunk_idx = chunk_idx, message = conditionMessage(e)) + detail <- .format_server_error(e) + list(ok = FALSE, chunk_idx = chunk_idx, + message = detail %||% conditionMessage(e)) }) } @@ -167,7 +171,7 @@ req_timeout(connection_timeout) |> req_perform() |> resp_body_json(), - error = function(e) { .stop_if_rate_limited(e); stop(e) } + error = function(e) .stop_with_server_error(e, "Upload complete failed") ) complete_resp @@ -216,6 +220,10 @@ # Check status status <- resp_status(response) if (status != 200) { + parsed <- .parse_server_error(response) + if (!is.null(parsed)) { + stop(paste0("HTTP ", status, " [", parsed$error_code, "] ", parsed$message)) + } stop("HTTP ", status, ": ", resp_body_string(response)) } @@ -232,9 +240,7 @@ return(as.character(job_id)) }, error = function(e) { - .stop_if_rate_limited(e) - cat("An error occurred:", conditionMessage(e), "\n") - return(NA_character_) + .stop_with_server_error(e, "Job submission failed") }) } diff --git a/R/cytetype.R b/R/cytetype.R index bcad406..49d5030 100644 --- a/R/cytetype.R +++ b/R/cytetype.R @@ -363,12 +363,13 @@ CyteTypeR <- function(obj, ) }, error = function(e) { if (require_artifacts) { - log_error("Uploading artifacts failed: {conditionMessage(e)}") + log_error(paste("Uploading artifacts failed:", conditionMessage(e), + "Set `require_artifacts=FALSE` to continue without uploading artifacts.")) stop(e) } else { log_warn(paste( "Uploading artifacts failed. Continuing without artifacts.", - "Set `require_artifacts=TRUE` to raise this as an error.", + "Set `require_artifacts=TRUE` to stop on upload failures.", "Original error:", conditionMessage(e) )) } @@ -382,8 +383,8 @@ CyteTypeR <- function(obj, job_id <- .submit_job(query_for_json, api_url, auth_token) - if (is.na(job_id)) { - stop("Job submission failed.") + if (is.na(job_id) || is.null(job_id)) { + stop("Job submission failed: no job ID returned.", call. = FALSE) } obj <- .store_job_details_seurat(obj, job_id, api_url, results_prefix, group_key, prepped_data$clusterLabels) diff --git a/R/errors.R b/R/errors.R index ed31e25..f16f9e2 100644 --- a/R/errors.R +++ b/R/errors.R @@ -26,6 +26,13 @@ print.cytetype_api_error <- function(x, ...) { NULL } +.format_server_error <- function(e) { + if (!inherits(e, "httr2_http") || is.null(e$resp)) return(NULL) + parsed <- .parse_server_error(e$resp) + if (is.null(parsed)) return(NULL) + paste0("[", parsed$error_code, "] ", parsed$message) +} + .stop_if_rate_limited <- function(e) { if (inherits(e, "httr2_http") && !is.null(e$resp)) { parsed <- .parse_server_error(e$resp) @@ -39,3 +46,11 @@ print.cytetype_api_error <- function(x, ...) { } } } + +.stop_with_server_error <- function(e, context = NULL) { + .stop_if_rate_limited(e) + detail <- .format_server_error(e) + msg <- detail %||% conditionMessage(e) + if (!is.null(context)) msg <- paste0(context, ": ", msg) + stop(msg, call. = FALSE) +}