trifid/trifid-api/src/error.rs

113 lines
4.2 KiB
Rust
Raw Normal View History

2023-11-19 03:51:45 +00:00
use std::fmt::{Display, Formatter};
2023-04-02 17:06:16 +00:00
use actix_web::error::{JsonPayloadError, PayloadError};
2023-11-19 03:51:45 +00:00
use serde::Serialize;
2023-04-02 17:06:16 +00:00
2023-11-19 03:51:45 +00:00
#[derive(Serialize, Debug)]
pub struct APIErrorResponse {
2023-04-02 17:06:16 +00:00
pub code: String,
pub message: String,
2023-11-19 03:51:45 +00:00
pub path: Option<String>
2023-04-02 17:06:16 +00:00
}
2023-11-19 03:51:45 +00:00
impl Display for APIErrorResponse {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
2023-05-11 00:32:19 +00:00
}
2023-04-02 17:06:16 +00:00
2023-11-19 03:51:45 +00:00
impl From<&JsonPayloadError> for APIErrorResponse {
2023-04-02 17:06:16 +00:00
fn from(value: &JsonPayloadError) -> Self {
match value {
JsonPayloadError::OverflowKnownLength { length, limit } => {
2023-11-19 03:51:45 +00:00
APIErrorResponse {
2023-04-02 17:06:16 +00:00
code: "ERR_PAYLOAD_OVERFLOW_KNOWN_LENGTH".to_string(),
message: format!("Payload size is bigger than allowed & content length header set. (length: {}, limit: {})", length, limit),
2023-11-19 03:51:45 +00:00
path: None,
2023-04-02 17:06:16 +00:00
}
},
JsonPayloadError::Overflow { limit } => {
2023-11-19 03:51:45 +00:00
APIErrorResponse {
2023-04-02 17:06:16 +00:00
code: "ERR_PAYLOAD_OVERFLOW".to_string(),
message: format!("Payload size is bigger than allowed but no content-length header is set. (limit: {})", limit),
2023-11-19 03:51:45 +00:00
path: None,
2023-04-02 17:06:16 +00:00
}
},
JsonPayloadError::ContentType => {
2023-11-19 03:51:45 +00:00
APIErrorResponse {
2023-04-02 17:06:16 +00:00
code: "ERR_NOT_JSON".to_string(),
message: "Content-Type header not set to expected application/json".to_string(),
path: None,
}
},
JsonPayloadError::Deserialize(e) => {
2023-11-19 03:51:45 +00:00
APIErrorResponse {
2023-04-02 17:06:16 +00:00
code: "ERR_JSON_DESERIALIZE".to_string(),
message: format!("Error deserializing JSON: {}", e),
path: None,
}
},
JsonPayloadError::Serialize(e) => {
2023-11-19 03:51:45 +00:00
APIErrorResponse {
2023-04-02 17:06:16 +00:00
code: "ERR_JSON_SERIALIZE".to_string(),
message: format!("Error serializing JSON: {}", e),
path: None,
}
},
JsonPayloadError::Payload(e) => {
e.into()
},
_ => {
2023-11-19 03:51:45 +00:00
APIErrorResponse {
2023-04-02 17:06:16 +00:00
code: "ERR_UNKNOWN_ERROR".to_string(),
message: "An unknown error has occured".to_string(),
path: None,
}
}
}
}
}
2023-11-19 03:51:45 +00:00
impl From<&PayloadError> for APIErrorResponse {
2023-04-02 17:06:16 +00:00
fn from(value: &PayloadError) -> Self {
match value {
2023-11-19 03:51:45 +00:00
PayloadError::Incomplete(e) => APIErrorResponse {
2023-05-11 00:32:19 +00:00
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),
},
path: None,
},
2023-11-19 03:51:45 +00:00
PayloadError::EncodingCorrupted => APIErrorResponse {
2023-05-11 00:32:19 +00:00
code: "ERR_CORRUPTED_PAYLOAD".to_string(),
message: "Payload content encoding corrupted".to_string(),
path: None,
},
2023-11-19 03:51:45 +00:00
PayloadError::Overflow => APIErrorResponse {
2023-05-11 00:32:19 +00:00
code: "ERR_PAYLOAD_OVERFLOW".to_string(),
message: "Payload reached size limit".to_string(),
path: None,
},
2023-11-19 03:51:45 +00:00
PayloadError::UnknownLength => APIErrorResponse {
2023-05-11 00:32:19 +00:00
code: "ERR_PAYLOAD_UNKNOWN_LENGTH".to_string(),
message: "Unable to determine payload length".to_string(),
path: None,
},
2023-11-19 03:51:45 +00:00
PayloadError::Http2Payload(e) => APIErrorResponse {
2023-05-11 00:32:19 +00:00
code: "ERR_HTTP2_ERROR".to_string(),
message: format!("HTTP/2 error: {}", e),
path: None,
},
2023-11-19 03:51:45 +00:00
PayloadError::Io(e) => APIErrorResponse {
2023-05-11 00:32:19 +00:00
code: "ERR_IO_ERROR".to_string(),
message: format!("I/O error: {}", e),
path: None,
},
2023-11-19 03:51:45 +00:00
_ => APIErrorResponse {
2023-05-11 00:32:19 +00:00
code: "ERR_UNKNOWN_ERROR".to_string(),
message: "An unknown error has occured".to_string(),
path: None,
},
2023-04-02 17:06:16 +00:00
}
}
2023-11-19 03:51:45 +00:00
}