Revert "Merge pull request #3318 from ultraworkers/pr-3292"

This reverts commit 2d920ce915, reversing
changes made to 80bfe94b77.
This commit is contained in:
clawdbot committed 2026-10-12 23:09:13 +00:00
1 parent 2d920ce915
commit f021597942
18 files changed
+464 -1940

No files matched your search

+57 -99
View File
@@ -52,13 +52,19 @@ pub enum ApiError {
body_snippet: String,
source: serde_json::Error,
},
/// A provider returned an error response.
///
/// Boxed: the payload is ~139 bytes, and `ApiError` is the `Err` half of nearly every
/// `Result` in this crate, so inlining it inflates the size of every one of those returns
/// (clippy::result_large_err). Boxing keeps `ApiError` small and means adding a field here
/// cannot silently re-inflate every signature in the crate.
Api(Box<ApiErrorDetails>),
Api {
status: reqwest::StatusCode,
error_type: Option<String>,
message: Option<String>,
request_id: Option<String>,
body: String,
retryable: bool,
/// Suggested user action based on error type (e.g., "Reduce prompt size" for 413)
suggested_action: Option<String>,
/// Parsed Retry-After header value (seconds) for 429 responses.
/// When present, overrides the exponential backoff delay.
retry_after: Option<Duration>,
},
RetriesExhausted {
attempts: u32,
last_error: Box<ApiError>,
@@ -75,38 +81,7 @@ pub enum ApiError {
},
}
/// Payload of [`ApiError::Api`], held behind a `Box` so the enum stays small.
#[derive(Debug)]
pub struct ApiErrorDetails {
pub status: reqwest::StatusCode,
pub error_type: Option<String>,
pub message: Option<String>,
pub request_id: Option<String>,
pub body: String,
pub retryable: bool,
/// Suggested user action based on error type (e.g., "Reduce prompt size" for 413)
pub suggested_action: Option<String>,
/// Parsed Retry-After header value (seconds) for 429 responses.
/// When present, overrides the exponential backoff delay.
pub retry_after: Option<Duration>,
}
impl ApiError {
/// Convenience constructor so callers do not have to spell out the `Box` at every site.
#[must_use]
pub fn api(details: ApiErrorDetails) -> Self {
Self::Api(Box::new(details))
}
/// Borrow the provider-error payload, if this is one.
#[must_use]
pub fn api_details(&self) -> Option<&ApiErrorDetails> {
match self {
Self::Api(details) => Some(details),
_ => None,
}
}
#[must_use]
pub const fn missing_credentials(
provider: &'static str,
@@ -162,7 +137,7 @@ impl ApiError {
/// over the computed backoff delay when it exists.
pub fn retry_after(&self) -> Option<Duration> {
match self {
Self::Api(details) => details.retry_after,
Self::Api { retry_after, .. } => *retry_after,
Self::RetriesExhausted { last_error, .. } => last_error.retry_after(),
_ => None,
}
@@ -171,7 +146,7 @@ impl ApiError {
pub fn is_retryable(&self) -> bool {
match self {
Self::Http(error) => error.is_connect() || error.is_timeout() || error.is_request(),
Self::Api(details) => details.retryable,
Self::Api { retryable, .. } => *retryable,
Self::RetriesExhausted { last_error, .. } => last_error.is_retryable(),
Self::MissingCredentials { .. }
| Self::ContextWindowExceeded { .. }
@@ -189,7 +164,7 @@ impl ApiError {
#[must_use]
pub fn request_id(&self) -> Option<&str> {
match self {
Self::Api(details) => details.request_id.as_deref(),
Self::Api { request_id, .. } => request_id.as_deref(),
Self::RetriesExhausted { last_error, .. } => last_error.request_id(),
Self::MissingCredentials { .. }
| Self::ContextWindowExceeded { .. }
@@ -216,12 +191,12 @@ impl ApiError {
Self::MissingCredentials { .. } | Self::ExpiredOAuthToken | Self::Auth(_) => {
"provider_auth"
}
Self::Api(details) if matches!(details.status.as_u16(), 401 | 403) => "provider_auth",
Self::Api { status, .. } if matches!(status.as_u16(), 401 | 403) => "provider_auth",
Self::ContextWindowExceeded { .. } => "context_window",
Self::Api(_) if self.is_context_window_failure() => "context_window",
Self::Api(details) if details.status.as_u16() == 429 => "provider_rate_limit",
Self::Api(_) if self.is_generic_fatal_wrapper() => "provider_internal",
Self::Api(_) => "provider_error",
Self::Api { .. } if self.is_context_window_failure() => "context_window",
Self::Api { status, .. } if status.as_u16() == 429 => "provider_rate_limit",
Self::Api { .. } if self.is_generic_fatal_wrapper() => "provider_internal",
Self::Api { .. } => "provider_error",
Self::Http(_) | Self::InvalidSseFrame(_) | Self::BackoffOverflow { .. } => {
"provider_transport"
}
@@ -233,12 +208,11 @@ impl ApiError {
#[must_use]
pub fn is_generic_fatal_wrapper(&self) -> bool {
match self {
Self::Api(details) => {
details
.message
Self::Api { message, body, .. } => {
message
.as_deref()
.is_some_and(looks_like_generic_fatal_wrapper)
|| looks_like_generic_fatal_wrapper(&details.body)
|| looks_like_generic_fatal_wrapper(body)
}
Self::RetriesExhausted { last_error, .. } => last_error.is_generic_fatal_wrapper(),
Self::MissingCredentials { .. }
@@ -255,36 +229,21 @@ impl ApiError {
}
}
/// True for failures whose own text cannot say what went wrong: transport, decode, and frame
/// errors.
///
/// These matter because a server that answers an oversized request with a non-SSE HTTP 500
/// body lands here rather than in [`Self::is_context_window_failure`] — reqwest reports
/// "error decoding response body" and the real cause is lost. Callers that know how large the
/// request was can combine the two signals; callers that do not must treat this as unknown,
/// never as an overflow.
#[must_use]
pub fn is_ambiguous_transport_failure(&self) -> bool {
match self {
Self::Http(_) | Self::Json { .. } | Self::InvalidSseFrame(_) => true,
Self::RetriesExhausted { last_error, .. } => {
last_error.is_ambiguous_transport_failure()
}
_ => false,
}
}
#[must_use]
pub fn is_context_window_failure(&self) -> bool {
match self {
Self::ContextWindowExceeded { .. } => true,
Self::Api(details) => {
matches!(details.status.as_u16(), 400 | 413 | 422)
&& (details
.message
Self::Api {
status,
message,
body,
..
} => {
matches!(status.as_u16(), 400 | 413 | 422)
&& (message
.as_deref()
.is_some_and(looks_like_context_window_error)
|| looks_like_context_window_error(&details.body))
|| looks_like_context_window_error(body))
}
Self::RetriesExhausted { last_error, .. } => last_error.is_context_window_failure(),
Self::MissingCredentials { .. }
@@ -368,14 +327,14 @@ impl Display for ApiError {
"failed to parse {provider} response for model {model}: {source}; first 200 chars of body: {body_snippet}"
),
// #28: enhance 401/403 errors with actionable auth guidance
Self::Api(details) if matches!(details.status.as_u16(), 401 | 403) => {
let (status, error_type, message, request_id, body) = (
&details.status,
&details.error_type,
&details.message,
&details.request_id,
&details.body,
);
Self::Api {
status,
error_type,
message,
request_id,
body,
..
} if matches!(status.as_u16(), 401 | 403) => {
if let (Some(error_type), Some(message)) = (error_type, message) {
write!(f, "api returned {status} ({error_type})")?;
if let Some(request_id) = request_id {
@@ -397,14 +356,14 @@ impl Display for ApiError {
Run `claw doctor` to verify your credential configuration."
)
}
Self::Api(details) => {
let (status, error_type, message, request_id, body) = (
&details.status,
&details.error_type,
&details.message,
&details.request_id,
&details.body,
);
Self::Api {
status,
error_type,
message,
request_id,
body,
..
} => {
if let (Some(error_type), Some(message)) = (error_type, message) {
write!(f, "api returned {status} ({error_type})")?;
if let Some(request_id) = request_id {
@@ -510,7 +469,6 @@ fn truncate_body_snippet(body: &str, max_chars: usize) -> String {
#[cfg(test)]
mod tests {
use super::ApiErrorDetails;
use super::{truncate_body_snippet, ApiError};
#[test]
@@ -575,7 +533,7 @@ mod tests {
#[test]
fn detects_generic_fatal_wrapper_and_classifies_it_as_provider_internal() {
let error = ApiError::Api(Box::new(ApiErrorDetails {
let error = ApiError::Api {
status: reqwest::StatusCode::INTERNAL_SERVER_ERROR,
error_type: Some("api_error".to_string()),
message: Some(
@@ -587,7 +545,7 @@ mod tests {
retryable: true,
suggested_action: None,
retry_after: None,
}));
};
assert!(error.is_generic_fatal_wrapper());
assert_eq!(error.safe_failure_class(), "provider_internal");
@@ -599,7 +557,7 @@ mod tests {
fn retries_exhausted_preserves_nested_request_id_and_failure_class() {
let error = ApiError::RetriesExhausted {
attempts: 3,
last_error: Box::new(ApiError::Api(Box::new(ApiErrorDetails {
last_error: Box::new(ApiError::Api {
status: reqwest::StatusCode::BAD_GATEWAY,
error_type: Some("api_error".to_string()),
message: Some(
@@ -611,7 +569,7 @@ mod tests {
retryable: true,
suggested_action: None,
retry_after: None,
}))),
}),
};
assert!(error.is_generic_fatal_wrapper());
@@ -621,7 +579,7 @@ mod tests {
#[test]
fn classifies_provider_context_window_errors() {
let error = ApiError::Api(Box::new(ApiErrorDetails {
let error = ApiError::Api {
status: reqwest::StatusCode::BAD_REQUEST,
error_type: Some("invalid_request_error".to_string()),
message: Some(
@@ -633,7 +591,7 @@ mod tests {
retryable: false,
suggested_action: None,
retry_after: None,
}));
};
assert!(error.is_context_window_failure());
assert_eq!(error.safe_failure_class(), "context_window");
@@ -642,7 +600,7 @@ mod tests {
#[test]
fn classifies_openai_configured_limit_errors_as_context_window_failures() {
let error = ApiError::Api(Box::new(ApiErrorDetails {
let error = ApiError::Api {
status: reqwest::StatusCode::BAD_REQUEST,
error_type: Some("invalid_request_error".to_string()),
message: Some(
@@ -654,7 +612,7 @@ mod tests {
retryable: false,
suggested_action: None,
retry_after: None,
}));
};
assert!(error.is_context_window_failure());
assert_eq!(error.safe_failure_class(), "context_window");
+4 -5
View File
@@ -12,7 +12,7 @@ pub use client::{
resolve_startup_auth_source, MessageStream, OAuthTokenSet, ProviderClient, ProviderConfig,
ProviderConfigKind,
};
pub use error::{ApiError, ApiErrorDetails};
pub use error::ApiError;
pub use http_client::{
build_http_client, build_http_client_or_default, build_http_client_with,
build_http_client_with_opts, ProxyConfig, TimeoutConfig,
@@ -29,10 +29,9 @@ pub use providers::openai_compat::{
OpenAiCompatClient, OpenAiCompatConfig,
};
pub use providers::{
context_window_override, detect_provider_kind, estimate_message_request_input_tokens,
max_tokens_for_model, max_tokens_for_model_with_override, model_family_identity_for,
model_family_identity_for_kind, model_token_limit, provider_diagnostics_for_model,
resolve_model_alias, ModelTokenLimit, ProviderDiagnostics, ProviderKind,
detect_provider_kind, max_tokens_for_model, max_tokens_for_model_with_override,
model_family_identity_for, model_family_identity_for_kind, provider_diagnostics_for_model,
resolve_model_alias, ProviderDiagnostics, ProviderKind,
};
pub use sse::{parse_frame, SseParser};
pub use types::{
+82 -27
View File
@@ -12,7 +12,7 @@ use serde::Deserialize;
use serde_json::{Map, Value};
use telemetry::{AnalyticsEvent, AnthropicRequestProfile, ClientIdentity, SessionTracer};
use crate::error::{ApiError, ApiErrorDetails};
use crate::error::ApiError;
use crate::http_client::build_http_client_or_default;
use crate::prompt_cache::{PromptCache, PromptCacheRecord, PromptCacheStats};
@@ -892,7 +892,7 @@ async fn expect_success(response: reqwest::Response) -> Result<reqwest::Response
let retryable = is_retryable_status(status);
let retry_after = parse_retry_after(&headers, status);
Err(ApiError::Api(Box::new(ApiErrorDetails {
Err(ApiError::Api {
status,
error_type: parsed_error
.as_ref()
@@ -905,7 +905,7 @@ async fn expect_success(response: reqwest::Response) -> Result<reqwest::Response
retryable,
suggested_action: None,
retry_after,
})))
})
}
fn parse_retry_after(
@@ -952,30 +952,86 @@ fn is_retryable_400(status: reqwest::StatusCode, body: &str) -> bool {
const SK_ANT_BEARER_HINT: &str = "sk-ant-* keys go in ANTHROPIC_API_KEY (x-api-key header), not ANTHROPIC_AUTH_TOKEN (Bearer header). Move your key to ANTHROPIC_API_KEY.";
fn enrich_bearer_auth_error(error: ApiError, auth: &AuthSource) -> ApiError {
let ApiError::Api(mut details) = error else {
let ApiError::Api {
status,
error_type,
message,
request_id,
body,
retryable,
suggested_action,
retry_after,
..
} = error
else {
return error;
};
if details.status.as_u16() != 401 {
return ApiError::Api(details);
if status.as_u16() != 401 {
return ApiError::Api {
status,
error_type,
message,
request_id,
body,
retryable,
suggested_action,
retry_after,
};
}
let Some(bearer_token) = auth.bearer_token() else {
return ApiError::Api(details);
return ApiError::Api {
status,
error_type,
message,
request_id,
body,
retryable,
suggested_action,
retry_after,
};
};
if !bearer_token.starts_with("sk-ant-") {
return ApiError::Api(details);
return ApiError::Api {
status,
error_type,
message,
request_id,
body,
retryable,
suggested_action,
retry_after,
};
}
// Only append the hint when the AuthSource is pure BearerToken. If both
// api_key and bearer_token are present (`ApiKeyAndBearer`), the x-api-key
// header is already being sent alongside the Bearer header and the 401
// is coming from a different cause — adding the hint would be misleading.
if auth.api_key().is_some() {
return ApiError::Api(details);
return ApiError::Api {
status,
error_type,
message,
request_id,
body,
retryable,
suggested_action,
retry_after,
};
}
let enriched_message = match message {
Some(existing) => Some(format!("{existing} — hint: {SK_ANT_BEARER_HINT}")),
None => Some(format!("hint: {SK_ANT_BEARER_HINT}")),
};
ApiError::Api {
status,
error_type,
message: enriched_message,
request_id,
body,
retryable,
suggested_action,
retry_after,
}
details.message = Some(match details.message.take() {
Some(existing) => format!("{existing} — hint: {SK_ANT_BEARER_HINT}"),
None => format!("hint: {SK_ANT_BEARER_HINT}"),
});
ApiError::Api(details)
}
fn anthropic_wire_model(model: &str) -> &str {
@@ -1027,7 +1083,6 @@ struct AnthropicErrorBody {
#[cfg(test)]
mod tests {
use super::{ALT_REQUEST_ID_HEADER, REQUEST_ID_HEADER};
use crate::error::ApiErrorDetails;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::sync::{Mutex, OnceLock};
@@ -1592,7 +1647,7 @@ mod tests {
fn enrich_bearer_auth_error_appends_sk_ant_hint_on_401_with_pure_bearer_token() {
// given
let auth = AuthSource::BearerToken("sk-ant-api03-deadbeef".to_string());
let error = crate::error::ApiError::Api(Box::new(ApiErrorDetails {
let error = crate::error::ApiError::Api {
status: reqwest::StatusCode::UNAUTHORIZED,
error_type: Some("authentication_error".to_string()),
message: Some("Invalid bearer token".to_string()),
@@ -1601,7 +1656,7 @@ mod tests {
retryable: false,
suggested_action: None,
retry_after: None,
}));
};
// when
let enriched = super::enrich_bearer_auth_error(error, &auth);
@@ -1623,8 +1678,8 @@ mod tests {
"request id should still flow through the enriched error: {rendered}"
);
match enriched {
crate::error::ApiError::Api(details) => {
assert_eq!(details.status, reqwest::StatusCode::UNAUTHORIZED);
crate::error::ApiError::Api { status, .. } => {
assert_eq!(status, reqwest::StatusCode::UNAUTHORIZED);
}
other => panic!("expected Api variant, got {other:?}"),
}
@@ -1634,7 +1689,7 @@ mod tests {
fn enrich_bearer_auth_error_leaves_non_401_errors_unchanged() {
// given
let auth = AuthSource::BearerToken("sk-ant-api03-deadbeef".to_string());
let error = crate::error::ApiError::Api(Box::new(ApiErrorDetails {
let error = crate::error::ApiError::Api {
status: reqwest::StatusCode::INTERNAL_SERVER_ERROR,
error_type: Some("api_error".to_string()),
message: Some("internal server error".to_string()),
@@ -1643,7 +1698,7 @@ mod tests {
retryable: true,
suggested_action: None,
retry_after: None,
}));
};
// when
let enriched = super::enrich_bearer_auth_error(error, &auth);
@@ -1664,7 +1719,7 @@ mod tests {
fn enrich_bearer_auth_error_ignores_401_when_bearer_token_is_not_sk_ant() {
// given
let auth = AuthSource::BearerToken("oauth-access-token-opaque".to_string());
let error = crate::error::ApiError::Api(Box::new(ApiErrorDetails {
let error = crate::error::ApiError::Api {
status: reqwest::StatusCode::UNAUTHORIZED,
error_type: Some("authentication_error".to_string()),
message: Some("Invalid bearer token".to_string()),
@@ -1673,7 +1728,7 @@ mod tests {
retryable: false,
suggested_action: None,
retry_after: None,
}));
};
// when
let enriched = super::enrich_bearer_auth_error(error, &auth);
@@ -1693,7 +1748,7 @@ mod tests {
api_key: "sk-ant-api03-legitimate".to_string(),
bearer_token: "sk-ant-api03-deadbeef".to_string(),
};
let error = crate::error::ApiError::Api(Box::new(ApiErrorDetails {
let error = crate::error::ApiError::Api {
status: reqwest::StatusCode::UNAUTHORIZED,
error_type: Some("authentication_error".to_string()),
message: Some("Invalid bearer token".to_string()),
@@ -1702,7 +1757,7 @@ mod tests {
retryable: false,
suggested_action: None,
retry_after: None,
}));
};
// when
let enriched = super::enrich_bearer_auth_error(error, &auth);
@@ -1719,7 +1774,7 @@ mod tests {
fn enrich_bearer_auth_error_ignores_401_when_auth_source_has_no_bearer() {
// given
let auth = AuthSource::ApiKey("sk-ant-api03-legitimate".to_string());
let error = crate::error::ApiError::Api(Box::new(ApiErrorDetails {
let error = crate::error::ApiError::Api {
status: reqwest::StatusCode::UNAUTHORIZED,
error_type: Some("authentication_error".to_string()),
message: Some("Invalid x-api-key".to_string()),
@@ -1728,7 +1783,7 @@ mod tests {
retryable: false,
suggested_action: None,
retry_after: None,
}));
};
// when
let enriched = super::enrich_bearer_auth_error(error, &auth);
+9 -63
View File
@@ -619,43 +619,16 @@ fn web_passthrough_diagnostic(
}
}
/// Overrides the context window every sizing guard is computed against.
///
/// The model *name* is not a reliable statement of the window when the endpoint is a local
/// server: the harness sends its compiled-in default name (a Claude alias) while the backend
/// serves whatever `-c` it was started with. Every guard downstream of [`model_token_limit`]
/// then sizes itself against the name's window instead of the real one, which makes the
/// preflights unable to fire rather than merely inaccurate. This variable is the operator's
/// statement of the truth, and it wins over the table.
const CONTEXT_WINDOW_ENV_VAR: &str = "CLAW_CONTEXT_WINDOW";
/// Fraction of the window a single response may be allowed to claim when the window is
/// declared. Output and input share one budget on a local server, so reserving a quarter for
/// the reply leaves three quarters for prompt plus transcript.
const MAX_OUTPUT_SHARE_OF_WINDOW: u32 = 4;
/// The operator-declared context window, when set and parseable as a positive token count.
#[must_use]
pub fn context_window_override() -> Option<u32> {
parse_context_window_override(std::env::var(CONTEXT_WINDOW_ENV_VAR).ok().as_deref())
}
#[must_use]
fn parse_context_window_override(value: Option<&str>) -> Option<u32> {
value
.and_then(|raw| raw.trim().parse::<u32>().ok())
.filter(|window| *window > 0)
}
#[must_use]
pub fn max_tokens_for_model(model: &str) -> u32 {
// A declared window caps the reply too. Asking a 65k server for 32k of output reserves half
// the window for a single message, which the server then silently clamps — leaving the
// client's own accounting wrong by the difference.
model_token_limit(model).map_or_else(
|| max_tokens_for_model_by_name(model),
|limit| max_tokens_for_model_by_name(model).min(limit.max_output_tokens),
)
let canonical = resolve_model_alias(model);
let heuristic = if canonical.contains("opus") {
32_000
} else {
64_000
};
model_token_limit(model).map_or(heuristic, |limit| heuristic.min(limit.max_output_tokens))
}
/// Returns the effective max output tokens for a model, preferring a plugin
@@ -668,33 +641,6 @@ pub fn max_tokens_for_model_with_override(model: &str, plugin_override: Option<u
#[must_use]
pub fn model_token_limit(model: &str) -> Option<ModelTokenLimit> {
// A declared window replaces the table's, and supplies one for models the table has never
// heard of. Returning `Some` for an unknown model is the point: `None` makes both preflights
// return `Ok` without looking at anything, so an unrecognised local model gets no guard at all.
if let Some(context_window_tokens) = context_window_override() {
return Some(ModelTokenLimit {
max_output_tokens: max_tokens_for_model_by_name(model)
.min(context_window_tokens / MAX_OUTPUT_SHARE_OF_WINDOW),
context_window_tokens,
});
}
model_token_limit_by_name(model)
}
#[must_use]
fn max_tokens_for_model_by_name(model: &str) -> u32 {
let canonical = resolve_model_alias(model);
let heuristic = if canonical.contains("opus") {
32_000
} else {
64_000
};
model_token_limit_by_name(model)
.map_or(heuristic, |limit| heuristic.min(limit.max_output_tokens))
}
#[must_use]
fn model_token_limit_by_name(model: &str) -> Option<ModelTokenLimit> {
let canonical = resolve_model_alias(model);
let base_model = canonical.rsplit('/').next().unwrap_or(canonical.as_str());
match base_model {
@@ -777,7 +723,7 @@ pub fn preflight_message_request(request: &MessageRequest) -> Result<(), ApiErro
Ok(())
}
pub fn estimate_message_request_input_tokens(request: &MessageRequest) -> u32 {
fn estimate_message_request_input_tokens(request: &MessageRequest) -> u32 {
let mut estimate = estimate_serialized_tokens(&request.messages);
estimate = estimate.saturating_add(estimate_serialized_tokens(&request.system));
estimate = estimate.saturating_add(estimate_serialized_tokens(&request.tools));
+13 -13
View File
@@ -7,7 +7,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::Deserialize;
use serde_json::{json, Value};
use crate::error::{ApiError, ApiErrorDetails};
use crate::error::ApiError;
use crate::http_client::build_http_client_or_default;
use crate::types::{
ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent,
@@ -239,7 +239,7 @@ impl OpenAiCompatClient {
.get("code")
.and_then(serde_json::Value::as_u64)
.map(|c| c as u16);
return Err(ApiError::Api(Box::new(ApiErrorDetails {
return Err(ApiError::Api {
status: reqwest::StatusCode::from_u16(code.unwrap_or(400))
.unwrap_or(reqwest::StatusCode::BAD_REQUEST),
error_type: err_obj
@@ -255,7 +255,7 @@ impl OpenAiCompatClient {
.unwrap_or(reqwest::StatusCode::BAD_REQUEST),
),
retry_after: None,
})));
});
}
}
let payload = serde_json::from_str::<ChatCompletionResponse>(&body).map_err(|error| {
@@ -1613,7 +1613,7 @@ fn parse_sse_frame(
.map(|c| c as u16);
let status = reqwest::StatusCode::from_u16(code.unwrap_or(500))
.unwrap_or(reqwest::StatusCode::INTERNAL_SERVER_ERROR);
return Err(ApiError::Api(Box::new(ApiErrorDetails {
return Err(ApiError::Api {
status,
error_type: err_obj
.get("type")
@@ -1625,12 +1625,12 @@ fn parse_sse_frame(
retryable: false,
suggested_action: suggested_action_for_status(status),
retry_after: None,
})));
});
}
}
// Detect HTML responses
if trimmed.starts_with('<') || trimmed.starts_with("<!") {
return Err(ApiError::Api(Box::new(ApiErrorDetails {
return Err(ApiError::Api {
status: reqwest::StatusCode::BAD_REQUEST,
error_type: Some("invalid_response".to_string()),
message: Some(
@@ -1641,7 +1641,7 @@ fn parse_sse_frame(
retryable: false,
suggested_action: Some("verify the API endpoint URL is correct".to_string()),
retry_after: None,
})));
});
}
return Ok(None);
}
@@ -1665,7 +1665,7 @@ fn parse_sse_frame(
.map(|c| c as u16);
let status = reqwest::StatusCode::from_u16(code.unwrap_or(400))
.unwrap_or(reqwest::StatusCode::BAD_REQUEST);
return Err(ApiError::Api(Box::new(ApiErrorDetails {
return Err(ApiError::Api {
status,
error_type: err_obj
.get("type")
@@ -1677,13 +1677,13 @@ fn parse_sse_frame(
retryable: false,
suggested_action: suggested_action_for_status(status),
retry_after: None,
})));
});
}
}
// Detect HTML or other non-JSON responses early for better error messages
let trimmed_payload = payload.trim();
if trimmed_payload.starts_with('<') || trimmed_payload.starts_with("<!") {
return Err(ApiError::Api(Box::new(ApiErrorDetails {
return Err(ApiError::Api {
status: reqwest::StatusCode::BAD_REQUEST,
error_type: Some("invalid_response".to_string()),
message: Some(
@@ -1694,7 +1694,7 @@ fn parse_sse_frame(
retryable: false,
suggested_action: Some("verify the API endpoint URL is correct".to_string()),
retry_after: None,
})));
});
}
serde_json::from_str::<ChatCompletionChunk>(&payload)
.map(Some)
@@ -1754,7 +1754,7 @@ async fn expect_success(response: reqwest::Response) -> Result<reqwest::Response
let suggested_action = suggested_action_for_status(status);
Err(ApiError::Api(Box::new(ApiErrorDetails {
Err(ApiError::Api {
status,
error_type: parsed_error
.as_ref()
@@ -1767,7 +1767,7 @@ async fn expect_success(response: reqwest::Response) -> Result<reqwest::Response
retryable,
suggested_action,
retry_after,
})))
})
}
fn parse_retry_after(