Skip to main content

rejection

Function rejection 

Source
pub fn rejection<Body>(
    status: u16,
    body: impl AsRef<[u8]>,
) -> Result<Response<Body>>
Expand description

Build the crux_http::Result a feature receives when the server rejects a request.

A 4xx or 5xx response never reaches an app as Ok(Response)crux_http converts it to an HttpError::Http on the Err side, keeping the body, before the event is sent. So this, and not a ResponseBuilder with an error status, is the value to assert against (or feed to an update function) when testing how a feature handles a rejection:

let event = Event::Saved(rejection(409, r#"{"error":"that overlaps a booked day"}"#));

let Event::Saved(Err(error)) = event else {
    panic!("a 409 is delivered as an error, not a response")
};
assert_eq!(error.code(), Some(409));
assert_eq!(error.to_string(), "HTTP error 409: 409 Conflict");

The Body type parameter is the one the app’s event carries (Vec<u8>, String, or whatever expect_json decodes to); it is usually inferred, and never appears in the value, because body decoding is skipped for an error status.

The status and reason phrase are produced by the same code path a real response takes, so the value is byte-for-byte what the shell’s response would have produced.

§Errors

Always Err — a rejection has no other form. The Result is the return type so that the value can be used exactly where the app receives one.

§Panics

Panics if status is outside the valid HTTP range (100–999), or if it is not a client (4xx) or server (5xx) error — for any other status a feature receives Ok(Response), which is what ResponseBuilder builds.