-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.R
More file actions
56 lines (51 loc) · 1.67 KB
/
Copy patherrors.R
File metadata and controls
56 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#' Custom API Error
#'
#' @param message Error message
#' @param call Call that generated the error
#' @return An error object of class cytetype_api_error
#' @export
cytetype_api_error <- function(message, call = NULL) {
structure(
list(message = message, call = call),
class = c("cytetype_api_error", "error", "condition")
)
}
#' @export
print.cytetype_api_error <- function(x, ...) {
cat("CyteType API Error: ", x$message, "\n")
}
.parse_server_error <- function(resp) {
body <- tryCatch(httr2::resp_body_json(resp), error = function(e) NULL)
if (is.null(body)) return(NULL)
if ("detail" %in% names(body) && is.list(body$detail)) body <- body$detail
if (!is.null(body$error_code) && !is.null(body$message)) {
return(list(error_code = body$error_code, message = body$message))
}
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)
if (!is.null(parsed) && parsed$error_code == "RATE_LIMIT_EXCEEDED") {
stop(
parsed$message,
"\nUse your own LLM API key via llm_configs to bypass free-tier limits, ",
"or wait before retrying.",
call. = FALSE
)
}
}
}
.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)
}