~starkingdoms/starkingdoms

ref: 3f9784408f5c43e366106d0f547d80d9b33ffa37 starkingdoms/starkingdoms-api/src/error.rs -rw-r--r-- 4.3 KiB
3f978440 — ghostlyzsh attaching and despawning modules works 2 years ago
                                                                                
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use actix_web::error::{JsonPayloadError, PayloadError};
use serde::Serialize;
use std::fmt::{Display, Formatter};

#[derive(Serialize, Debug)]
pub struct APIErrorResponse {
    pub success: bool,
    pub code: String,
    pub message: String,
}
impl Display for APIErrorResponse {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl From<&JsonPayloadError> for APIErrorResponse {
    fn from(value: &JsonPayloadError) -> Self {
        match value {
            JsonPayloadError::OverflowKnownLength { length, limit } => {
                APIErrorResponse {
                    code: "ERR_PAYLOAD_OVERFLOW_KNOWN_LENGTH".to_string(),
                    message: format!("Payload size is bigger than allowed & content length header set. (length: {}, limit: {})", length, limit),
                    success: false,
                }
            },
            JsonPayloadError::Overflow { limit } => {
                APIErrorResponse {
                    code: "ERR_PAYLOAD_OVERFLOW".to_string(),
                    message: format!("Payload size is bigger than allowed but no content-length header is set. (limit: {})", limit),
                    success: false,
                }
            },
            JsonPayloadError::ContentType => {
                APIErrorResponse {
                    code: "ERR_NOT_JSON".to_string(),
                    message: "Content-Type header not set to expected application/json".to_string(),
                    success: false,
                }
            },
            JsonPayloadError::Deserialize(e) => {
                APIErrorResponse {
                    code: "ERR_JSON_DESERIALIZE".to_string(),
                    message: format!("Error deserializing JSON: {}", e),
                    success: false,
                }
            },
            JsonPayloadError::Serialize(e) => {
                APIErrorResponse {
                    code: "ERR_JSON_SERIALIZE".to_string(),
                    message: format!("Error serializing JSON: {}", e),
                    success: false,
                }
            },
            JsonPayloadError::Payload(e) => {
                e.into()
            },
            _ => {
                APIErrorResponse {
                    code: "ERR_UNKNOWN_ERROR".to_string(),
                    message: "An unknown error has occured".to_string(),
                    success: false,
                }
            }
        }
    }
}

impl From<&PayloadError> for APIErrorResponse {
    fn from(value: &PayloadError) -> Self {
        match value {
            PayloadError::Incomplete(e) => APIErrorResponse {
                code: "ERR_UNEXPECTED_EOF".to_string(),
                message: match e {
                    None => "Payload reached EOF but was incomplete".to_string(),
                    Some(e) => format!("Payload reached EOF but was incomplete: {}", e),
                },
                success: false,
            },
            PayloadError::EncodingCorrupted => APIErrorResponse {
                code: "ERR_CORRUPTED_PAYLOAD".to_string(),
                message: "Payload content encoding corrupted".to_string(),
                success: false,
            },
            PayloadError::Overflow => APIErrorResponse {
                code: "ERR_PAYLOAD_OVERFLOW".to_string(),
                message: "Payload reached size limit".to_string(),
                success: false,
            },
            PayloadError::UnknownLength => APIErrorResponse {
                code: "ERR_PAYLOAD_UNKNOWN_LENGTH".to_string(),
                message: "Unable to determine payload length".to_string(),
                success: false,
            },
            PayloadError::Http2Payload(e) => APIErrorResponse {
                code: "ERR_HTTP2_ERROR".to_string(),
                message: format!("HTTP/2 error: {}", e),
                success: false,
            },
            PayloadError::Io(e) => APIErrorResponse {
                code: "ERR_IO_ERROR".to_string(),
                message: format!("I/O error: {}", e),
                success: false,
            },
            _ => APIErrorResponse {
                code: "ERR_UNKNOWN_ERROR".to_string(),
                message: "An unknown error has occured".to_string(),
                success: false,
            },
        }
    }
}