pub struct ContentNegotiation { /* private fields */ }Expand description
Plugin that converts plain-text error responses into structured JSON.
When installed, ContentNegotiation wraps the request pipeline with a
middleware that inspects every outgoing response. If the status is a
client error (4xx) or server error (5xx) and the Content-Type is
text/plain, it re-encodes the body as:
{"error": "<original message>", "status": <status code>}This ensures that API clients always receive a machine-readable JSON error body rather than an opaque string, regardless of where in the framework the error originated.
§Configuration
| Builder method | Default | Description |
|---|---|---|
pretty | false | Pretty-print the JSON error body |
§Example
use churust_core::{Churust, Call, Error, TestClient};
use churust_json::ContentNegotiation;
let app = Churust::server()
.install(ContentNegotiation::new())
.routing(|r| {
r.get("/fail", |_c: Call| async {
Err::<&str, _>(Error::bad_request("something went wrong"))
});
})
.build();
let res = TestClient::new(app).get("/fail").send().await;
assert_eq!(res.status().as_u16(), 400);
assert_eq!(res.header("content-type"), Some("application/json"));
let body: serde_json::Value = serde_json::from_slice(res.body_bytes()).unwrap();
assert_eq!(body["error"], "something went wrong");
assert_eq!(body["status"], 400);Implementations§
Source§impl ContentNegotiation
impl ContentNegotiation
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new ContentNegotiation plugin with default settings.
By default, JSON error bodies are compact (not pretty-printed). Call
pretty on the returned value to change this.
§Example
use churust_core::{Churust, Call, Error, TestClient};
use churust_json::ContentNegotiation;
let app = Churust::server()
.install(ContentNegotiation::new())
.routing(|r| {
r.get("/boom", |_c: Call| async {
Err::<&str, _>(Error::not_found("no such resource"))
});
})
.build();
let res = TestClient::new(app).get("/boom").send().await;
assert_eq!(res.header("content-type"), Some("application/json"));Sourcepub fn pretty(self, pretty: bool) -> Self
pub fn pretty(self, pretty: bool) -> Self
Controls whether JSON error bodies are pretty-printed.
When pretty is true, error responses are formatted with newlines and
indentation, which is helpful during development or when error responses
may be read by humans. For production APIs, leave this at the default
false to keep response sizes small.
§Parameters
pretty—trueto enable pretty-printing;false(the default) for compact output.
§Example
use churust_core::{Churust, Call, Error, TestClient};
use churust_json::ContentNegotiation;
let app = Churust::server()
.install(ContentNegotiation::new().pretty(true))
.routing(|r| {
r.get("/oops", |_c: Call| async {
Err::<&str, _>(Error::internal("disk full"))
});
})
.build();
let res = TestClient::new(app).get("/oops").send().await;
assert_eq!(res.status().as_u16(), 500);
// Pretty-printed JSON contains newlines.
let text = res.text();
assert!(text.contains('\n'));Trait Implementations§
Source§impl Clone for ContentNegotiation
impl Clone for ContentNegotiation
Source§fn clone(&self) -> ContentNegotiation
fn clone(&self) -> ContentNegotiation
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more