Skip to main content

convert_error_body_or_raw

Function convert_error_body_or_raw 

Source
pub fn convert_error_body_or_raw(
    src_operation: OperationFamily,
    src_protocol: ProtocolKind,
    dst_operation: OperationFamily,
    dst_protocol: ProtocolKind,
    body: Vec<u8>,
) -> Vec<u8> 
Expand description

Convert an upstream error body (non-2xx response body) from the upstream protocol’s error schema to the client’s expected error schema, falling back to the raw bytes on any failure.

Each protocol uses a different error shape:

  • Claude: {"type":"error","error":{"type":"...","message":"..."}}
  • OpenAI: {"error":{"message":"...","type":"...","code":"..."}}
  • Gemini: {"error":{"code":N,"message":"...","status":"..."}}

An OpenAI-speaking client that receives a Claude error body cannot parse it, so we route error bodies through the same transform_response pipeline that success bodies use. The BodyEnvelope::from_body_bytes macro tries both the success and error variants, so if the upstream error conforms to the declared error schema, it’s converted via the existing TryFrom<SrcResponse> for DstResponse impls (which handle the Error { .. } variant separately).

If the upstream error schema doesn’t match the declared error_body type (e.g. codex returning {"detail":{"code":"..."}} which is not OpenAiApiErrorResponse), from_body_bytes fails and the whole transform errors out — in which case this helper forwards the raw upstream bytes so the client at least sees what the upstream sent.

For streaming operations, the operation family is substituted with GenerateContent before dispatch. Error bodies share the same schema regardless of whether the request was streaming, but transform_response only declares arms for non-stream ops.