Skip to main content

apimock_server/response/
error_response.rs

1use hyper::{HeaderMap, StatusCode};
2
3use super::status_code_response::{status_code_response, status_code_response_with_message};
4use crate::types::BoxBody;
5
6/// error response on http BAD_REQUEST (400)
7///
8/// # Uncalled today (RFC 079 M-04d, audit F-09) — re-verified, still
9/// # true
10///
11/// A prior handoff expected RFC 068 (tranche 1, body-size limits) to
12/// give this its first caller; it didn't — RFC 068 added the separate
13/// `payload_too_large_response` (413) for its own overflow case instead
14/// of reusing this one. Re-checked for this tranche:
15/// `grep -rn bad_request_response crates/ --include='*.rs'` still
16/// returns only this definition. F-09 still wants a caller for it, so
17/// it stays — a dead function about to be used is correct to keep; the
18/// note is what says *why* it's unused, rather than leaving a reader to
19/// wonder or assume it's reachable.
20///
21/// A plausible caller exists and was noticed while checking this
22/// (deliberately **not** implemented here — RFC 079's own scope is "no
23/// behaviour change anywhere", and wiring this up would be one):
24/// `parsed_request.rs`'s `parsed_request_from` currently answers a
25/// malformed JSON body claiming `Content-Type: application/json` with
26/// `ParsedRequestError::Other`, which `server.rs` maps to a bare 500 —
27/// a client mistake reported as a server failure. `bad_request_response`
28/// is the obvious fix, but it's a real behaviour change (500 → 400 for
29/// that one case) that belongs to whichever RFC picks it up next, not
30/// to this hygiene pass.
31pub fn bad_request_response(
32    message: &str,
33    request_headers: &HeaderMap,
34    cors_allow_credentials_origins: &[String],
35) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
36    status_code_response_with_message(
37        &StatusCode::BAD_REQUEST,
38        message,
39        None,
40        request_headers,
41        cors_allow_credentials_origins,
42    )
43}
44
45/// error response on http PAYLOAD_TOO_LARGE (413) — RFC 068 S-02: a
46/// request body over the configured limit, refused before it is
47/// buffered.
48pub fn payload_too_large_response(
49    message: &str,
50    request_headers: &HeaderMap,
51    cors_allow_credentials_origins: &[String],
52) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
53    status_code_response_with_message(
54        &StatusCode::PAYLOAD_TOO_LARGE,
55        message,
56        None,
57        request_headers,
58        cors_allow_credentials_origins,
59    )
60}
61
62/// error response on http NOT_FOUND (404)
63pub fn not_found_response(
64    request_headers: &HeaderMap,
65    cors_allow_credentials_origins: &[String],
66) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
67    status_code_response(
68        &StatusCode::NOT_FOUND,
69        None,
70        request_headers,
71        cors_allow_credentials_origins,
72    )
73}
74
75/// error response on http INTERNAL_SERVER_ERROR (500)
76pub fn internal_server_error_response(
77    message: &str,
78    request_headers: &HeaderMap,
79    cors_allow_credentials_origins: &[String],
80) -> Result<hyper::Response<BoxBody>, hyper::http::Error> {
81    status_code_response_with_message(
82        &StatusCode::INTERNAL_SERVER_ERROR,
83        message,
84        None,
85        request_headers,
86        cors_allow_credentials_origins,
87    )
88}