framework_cqrs_lib/cqrs/models/
errors.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
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

pub type ResultErr<DATA> = Result<DATA, Error>;

#[derive(Serialize, Deserialize, Clone, ToSchema, Debug)]
pub enum Error {
    Http(ErrorHttpCustom),
    Simple(String),
}


#[derive(Serialize, Deserialize, Clone, ToSchema, Debug)]
pub struct StandardHttpError {
    pub not_found: ErrorHttpCustom,
    pub internal_server_error: ErrorHttpCustom,
    pub unauthorized: ErrorHttpCustom,
}

impl StandardHttpError {
    pub fn new() -> Self {
        Self {
            not_found: ErrorHttpCustom::new("ressource not found", "00NOTFO", vec![], Some(404)),
            internal_server_error: ErrorHttpCustom::new("wip", "00INTER", vec![], Some(500)),
            unauthorized: ErrorHttpCustom::new("wip", "00UNAUT", vec![], Some(401)),
        }
    }
}

#[derive(Serialize, Deserialize, Clone, ToSchema, Debug)]
pub struct ErrorHttpCustom {
    #[schema(example = "titre")]
    pub title: String,
    #[schema(example = "00EXAMPLE")]
    pub code: String,
    #[schema(example = "[]")]
    pub causes: Vec<Problem>,
    #[schema(example = "200")]
    pub status: Option<u16>,
}

impl ErrorHttpCustom {
    pub fn new(title: &str, code: &str, problems: Vec<Problem>, status: Option<u16>) -> Self {
        Self {
            title: title.to_string(),
            code: code.to_string(),
            causes: problems,
            status,
        }
    }
}

#[derive(Serialize, Deserialize, Clone, ToSchema, Debug)]
pub struct Problem {
    #[schema(example = "titre")]
    pub title: String,
    #[schema(example = "description")]
    pub description: String,
}