Skip to content
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down
6 changes: 5 additions & 1 deletion R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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"
Expand Down
20 changes: 13 additions & 7 deletions R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
})
}

Expand All @@ -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))
})
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand All @@ -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")
})
}

Expand Down
9 changes: 5 additions & 4 deletions R/cytetype.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
))
}
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions R/errors.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Loading