tako_rs_plugins/middleware/
problem_json.rs1use std::future::Future;
15use std::pin::Pin;
16
17use tako_rs_core::middleware::IntoMiddleware;
18use tako_rs_core::middleware::Next;
19use tako_rs_core::problem::default_problem_responder;
20use tako_rs_core::types::Request;
21use tako_rs_core::types::Response;
22
23pub struct ProblemJson {
25 client_errors: bool,
27 server_errors: bool,
29}
30
31impl Default for ProblemJson {
32 fn default() -> Self {
33 Self::new()
34 }
35}
36
37impl ProblemJson {
38 pub fn new() -> Self {
40 Self {
41 client_errors: true,
42 server_errors: true,
43 }
44 }
45
46 pub fn client_errors(mut self, on: bool) -> Self {
48 self.client_errors = on;
49 self
50 }
51
52 pub fn server_errors(mut self, on: bool) -> Self {
54 self.server_errors = on;
55 self
56 }
57}
58
59impl IntoMiddleware for ProblemJson {
60 fn into_middleware(
61 self,
62 ) -> impl Fn(Request, Next) -> Pin<Box<dyn Future<Output = Response> + Send + 'static>>
63 + Clone
64 + Send
65 + Sync
66 + 'static {
67 let client = self.client_errors;
68 let server = self.server_errors;
69
70 move |req: Request, next: Next| {
71 Box::pin(async move {
72 let resp = next.run(req).await;
73 let status = resp.status();
74 let should_convert =
75 (client && status.is_client_error()) || (server && status.is_server_error());
76 if !should_convert {
77 return resp;
78 }
79 default_problem_responder(resp)
80 })
81 }
82 }
83}