Skip to main content

malformed_response

Function malformed_response 

Source
pub fn malformed_response(status: u16, body: &[u8], err: impl Display) -> Error
Expand description

The error for a success response whose body could not be decoded: an HTML page from a proxy, a truncated body, or JSON of another shape.

It is an Error::UnexpectedResponse, which is neither retried nor failed over, with the status, the body (capped, and only its length reaches the log) and the decoder’s error as the upstream message, since a decoder’s text can quote the body. Add the provider and the request id, so the failure can be traced to a response:

use allama::Error;
use allama_http::malformed_response;

let body = b"<html>502 Bad Gateway</html>";
let decoded = serde_json::from_slice::<serde_json::Value>(body);
let err = malformed_response(200, body, decoded.unwrap_err())
    .with_provider("acme")
    .with_request_id("req_1");
assert!(matches!(err, Error::UnexpectedResponse { .. }));
assert_eq!(err.status(), Some(200));
assert_eq!(err.request_id(), Some("req_1"));
assert_eq!(
    err.as_log().to_string(),
    r#"kind=unexpected_response provider=acme status=200 request_id=req_1 body_len=28 message="the response body could not be decoded""#,
);

Error::Serialization is for failures of the caller’s own (de)serialization, never for what a provider sent.