cat_gateway/service/common/responses/
code_412_precondition_failed.rs

1//! Define `Precondition Failed` response type.
2
3use poem_openapi::{
4    types::{Example, ToJSON},
5    Object,
6};
7
8use crate::service::{common, common::types::array_types::impl_array_types};
9
10/// The client has not sent valid data in its request, headers, parameters or body.
11#[derive(Object, Debug, Clone)]
12#[oai(example)]
13pub(crate) struct PreconditionFailed {
14    /// Details of each error in the content that was detected.
15    ///
16    /// Note: This may not be ALL errors in the content, as validation of content can stop
17    /// at any point an error is detected.
18    detail: ContentErrorDetailList,
19}
20
21impl PreconditionFailed {
22    /// Create a new `ContentErrorDetail` Response Payload.
23    pub(crate) fn new(errors: Vec<poem::Error>) -> Self {
24        let mut detail = vec![];
25        for error in errors {
26            detail.push(ContentErrorDetail::new(&error));
27        }
28
29        Self {
30            detail: detail.into(),
31        }
32    }
33}
34
35impl Example for PreconditionFailed {
36    /// Example for the Too Many Requests Payload.
37    fn example() -> Self {
38        Self {
39            detail: Example::example(),
40        }
41    }
42}
43
44// List of Content Error Details
45impl_array_types!(
46    ContentErrorDetailList,
47    ContentErrorDetail,
48    Some(poem_openapi::registry::MetaSchema {
49        example: Self::example().to_json(),
50        min_items: Some(1),
51        max_items: Some(1000),
52        items: Some(Box::new(ContentErrorDetail::schema_ref())),
53        ..poem_openapi::registry::MetaSchema::ANY
54    })
55);
56
57impl Example for ContentErrorDetailList {
58    fn example() -> Self {
59        Self(vec![Example::example()])
60    }
61}
62
63//--------------------------------------------------------------------------------------
64
65#[derive(Object, Debug, Clone)]
66#[oai(example)]
67/// Individual details of a single error that was detected with the content of the
68/// request.
69pub(crate) struct ContentErrorDetail {
70    /// The location of the error
71    #[oai(skip_serializing_if_is_none)]
72    loc: Option<common::types::generic::error_list::ErrorList>,
73    /// The error message.
74    #[oai(skip_serializing_if_is_none)]
75    msg: Option<common::types::generic::error_msg::ErrorMessage>,
76    /// The type of error
77    #[oai(rename = "type", skip_serializing_if_is_none)]
78    err_type: Option<common::types::generic::error_msg::ErrorMessage>,
79}
80
81impl Example for ContentErrorDetail {
82    /// Example for the `ContentErrorDetail` Payload.
83    fn example() -> Self {
84        Self {
85            loc: Some(vec!["body".into()].into()),
86            msg: Some("Value is not a valid dict.".into()),
87            err_type: Some("type_error.dict".into()),
88        }
89    }
90}
91
92impl ContentErrorDetail {
93    /// Create a new `ContentErrorDetail` Response Payload.
94    pub(crate) fn new(error: &poem::Error) -> Self {
95        // TODO: See if we can get more info from the error than this.
96        Self {
97            loc: None,
98            msg: Some(error.to_string().into()),
99            err_type: None,
100        }
101    }
102}