api_response/error_code/
mod.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
83
84
mod codes;
mod helper;
mod http_status;
mod macros;
pub use codes::*;
pub use helper::*;
pub use macros::*;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ApiError;
    #[test]
    fn new_api_error() {
        assert_eq!(
            r##"ApiError { code: 12, message: "message", details: None }"##,
            format!("{:?}", ApiError::new(INVALID_ARGUMENT, "message"))
        );
    }
    #[test]
    fn code_segment() {
        assert_eq!(12010203, INVALID_ARGUMENT | S01 | S02 | S03)
    }
    #[test]
    #[should_panic]
    fn code_segment_overflow() {
        let _ = INVALID_ARGUMENT | S01 | S02 | S03 | S04 | S05;
    }
    #[test]
    fn api_err() {
        const BLOCK_ERROR0: ApiErr = ApiErr::new0();
        assert_eq!(
            r##"ApiError { code: 12, message: "message", details: None }"##,
            format!("{:?}", BLOCK_ERROR0.invalid_argument("message"))
        );
        const BLOCK_ERROR1: ApiErr = ApiErr::new1(S99);
        assert_eq!(
            r##"ApiError { code: 1299, message: "message", details: None }"##,
            format!("{:?}", BLOCK_ERROR1.invalid_argument("message"))
        );
        const BLOCK_ERROR2: ApiErr = ApiErr::new2(S99, S99);
        assert_eq!(
            r##"ApiError { code: 129999, message: "message", details: None }"##,
            format!("{:?}", BLOCK_ERROR2.invalid_argument("message"))
        );
        const BLOCK_ERROR3: ApiErr = ApiErr::new3(S99, S99, S99);
        assert_eq!(
            r##"ApiError { code: 12999999, message: "message", details: None }"##,
            format!("{:?}", BLOCK_ERROR3.invalid_argument("message"))
        );
        const BLOCK_ERROR4: ApiErr = ApiErr::new0().intro("this is intro");
        assert_eq!(r##"[??******]: this is intro"##, BLOCK_ERROR4.to_string());
        const BLOCK_ERROR5: ApiErr = ApiErr::new1(S01).intro("this is intro");
        assert_eq!(r##"[??01****]: this is intro"##, BLOCK_ERROR5.to_string());
        const BLOCK_ERROR6: ApiErr = ApiErr::new2(S01, S02).intro("this is intro");
        assert_eq!(r##"[??0102**]: this is intro"##, BLOCK_ERROR6.to_string());
        const BLOCK_ERROR7: ApiErr = ApiErr::new3(S01, S02, S33).intro("this is intro");
        assert_eq!(r##"[??010233]: this is intro"##, BLOCK_ERROR7.to_string());
    }
    #[test]
    fn api_err_x() {
        const BLOCK_ERROR1: ApiErrX = ApiErrX::new1();
        assert_eq!(
            r##"ApiError { code: 1299, message: "message", details: None }"##,
            format!("{:?}", BLOCK_ERROR1.invalid_argument(S99, "message"))
        );
        const BLOCK_ERROR2: ApiErrX = ApiErrX::new2(S99);
        assert_eq!(
            r##"ApiError { code: 129999, message: "message", details: None }"##,
            format!("{:?}", BLOCK_ERROR2.invalid_argument(S99, "message"))
        );
        const BLOCK_ERROR3: ApiErrX = ApiErrX::new3(S99, S99);
        assert_eq!(
            r##"ApiError { code: 12999999, message: "message", details: None }"##,
            format!("{:?}", BLOCK_ERROR3.invalid_argument(S99, "message"))
        );
        const BLOCK_ERROR4: ApiErrX = ApiErrX::new1().intro("this is intro");
        assert_eq!(r##"[??******]: this is intro"##, BLOCK_ERROR4.to_string());
        const BLOCK_ERROR5: ApiErrX = ApiErrX::new2(S11).intro("this is intro");
        assert_eq!(r##"[??11****]: this is intro"##, BLOCK_ERROR5.to_string());
        const BLOCK_ERROR6: ApiErrX = ApiErrX::new3(S11, S02).intro("this is intro");
        assert_eq!(r##"[??1102**]: this is intro"##, BLOCK_ERROR6.to_string());
    }
}