Skip to content

Commit

Permalink
Move handlers out
Browse files Browse the repository at this point in the history
romainfrancois committed Mar 30, 2024
1 parent 1b10fc6 commit 07de5b0
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions R/httr2.R
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
req_mistral_perform <- function(req, error_call = caller_env()) {

url <- req$url

handle_invalid_model_error <- function(err, resp) {
status <- resp_status(resp)
if (status == 400 && resp_body_json(resp)$type == "invalid_model") {
model <- req$body$data$model
cli_abort(c(
"Invalid mistrai.ai model {.emph {model}}.",
i = "Use one of {.or {models()}}."
), call = error_call)
withCallingHandlers(
req_perform(req),
error = function(err) {
resp <- err$resp
handle_invalid_model_error(err, req, resp, error_call = error_call)
handle_req_perform_error(err, req, resp, error_call = error_call)
}
}
)
}

handle_req_perform_error <- function(err, resp) {
bullets <- c(x = "with endpoint {.url {url}}")
handle_invalid_model_error <- function(err, req, resp, error_call = caller_env()) {
status <- resp_status(resp)
if (status == 400 && resp_body_json(resp)$type == "invalid_model") {
model <- req$body$data$model
cli_abort(c(
"Invalid mistrai.ai model {.emph {model}}.",
i = "Use one of {.or {models()}}."
), call = error_call)
}
}

if (!inherits(err, "error_mistral_req_perform")) {
bullets <- c(
bullets,
i = "Make sure your api key is valid {.url https://console.mistral.ai/api-keys/}",
i = "And set the {.envvar MISTRAL_API_KEY} environment variable",
i = "Perhaps using {.fn usethis::edit_r_environ}"
)
}
handle_req_perform_error <- function(err, req, resp, error_call = caller_env()) {
url <- req$url
bullets <- c(x = "with endpoint {.url {url}}")

cli_abort(
bullets, class = c("error_mistral_req_perform"),
call = error_call, parent = err
if (!inherits(err, "error_mistral_req_perform")) {
bullets <- c(
bullets,
i = "Make sure your api key is valid {.url https://console.mistral.ai/api-keys/}",
i = "And set the {.envvar MISTRAL_API_KEY} environment variable",
i = "Perhaps using {.fn usethis::edit_r_environ}"
)
}

withCallingHandlers(
req_perform(req),
error = function(err) {
resp <- err$resp
handle_invalid_model_error(err, resp)
handle_req_perform_error(err, resp)
}
cli_abort(
bullets, class = c("error_mistral_req_perform"),
call = error_call, parent = err
)
}

0 comments on commit 07de5b0

Please sign in to comment.