cat_gateway/service/common/responses/code_422_unprocessable_content.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
//! Define `Unprocessable Content` response type.
use poem_openapi::{types::Example, Object};
use crate::service::common;
#[derive(Object)]
#[oai(example)]
/// The client has not sent valid data in its request, headers, parameters or body.
pub(crate) struct UnprocessableContent {
#[oai(validator(max_items = "1000", min_items = "1"))]
/// Details of each error in the content that was detected.
///
/// Note: This may not be ALL errors in the content, as validation of content can stop
/// at any point an error is detected.
detail: Vec<ContentErrorDetail>,
}
impl UnprocessableContent {
/// Create a new `ContentErrorDetail` Response Payload.
pub(crate) fn new(errors: Vec<poem::Error>) -> Self {
let mut detail = vec![];
for error in errors {
detail.push(ContentErrorDetail::new(&error));
}
Self { detail }
}
}
impl Example for UnprocessableContent {
/// Example for the Too Many Requests Payload.
fn example() -> Self {
Self {
detail: vec![ContentErrorDetail::example()],
}
}
}
//--------------------------------------------------------------------------------------
#[derive(Object)]
#[oai(example)]
/// Individual details of a single error that was detected with the content of the
/// request.
pub(crate) struct ContentErrorDetail {
/// The location of the error
#[oai(validator(max_items = 100))]
loc: Option<Vec<common::types::generic::error_msg::ErrorMessage>>,
/// The error message.
#[oai(validator(max_length = "1000", pattern = "^[0-9a-zA-Z].*$"))]
msg: Option<common::types::generic::error_msg::ErrorMessage>,
/// The type of error
#[oai(
rename = "type",
validator(max_length = "1000", pattern = "^[0-9a-zA-Z].*$")
)]
err_type: Option<common::types::generic::error_msg::ErrorMessage>,
}
impl Example for ContentErrorDetail {
/// Example for the `ContentErrorDetail` Payload.
fn example() -> Self {
Self {
loc: Some(vec!["body".into()]),
msg: Some("Value is not a valid dict.".into()),
err_type: Some("type_error.dict".into()),
}
}
}
impl ContentErrorDetail {
/// Create a new `ContentErrorDetail` Response Payload.
pub(crate) fn new(error: &poem::Error) -> Self {
// TODO: See if we can get more info from the error than this.
Self {
loc: None,
msg: Some(error.to_string().into()),
err_type: None,
}
}
}