use actix_web::error::{JsonPayloadError, PayloadError}; use serde::Serialize; use std::fmt::{Display, Formatter}; #[derive(Serialize, Debug)] pub struct APIErrorsResponse { pub errors: Vec, } #[derive(Serialize, Debug)] pub struct APIErrorResponse { pub code: String, pub message: String, pub path: Option, } 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), path: None, } }, 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), path: None, } }, JsonPayloadError::ContentType => { APIErrorResponse { code: "ERR_NOT_JSON".to_string(), message: "Content-Type header not set to expected application/json".to_string(), path: None, } }, JsonPayloadError::Deserialize(e) => { APIErrorResponse { code: "ERR_JSON_DESERIALIZE".to_string(), message: format!("Error deserializing JSON: {}", e), path: None, } }, JsonPayloadError::Serialize(e) => { APIErrorResponse { code: "ERR_JSON_SERIALIZE".to_string(), message: format!("Error serializing JSON: {}", e), path: None, } }, JsonPayloadError::Payload(e) => { e.into() }, _ => { APIErrorResponse { code: "ERR_UNKNOWN_ERROR".to_string(), message: "An unknown error has occured".to_string(), path: None, } } } } } 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), }, path: None, }, PayloadError::EncodingCorrupted => APIErrorResponse { code: "ERR_CORRUPTED_PAYLOAD".to_string(), message: "Payload content encoding corrupted".to_string(), path: None, }, PayloadError::Overflow => APIErrorResponse { code: "ERR_PAYLOAD_OVERFLOW".to_string(), message: "Payload reached size limit".to_string(), path: None, }, PayloadError::UnknownLength => APIErrorResponse { code: "ERR_PAYLOAD_UNKNOWN_LENGTH".to_string(), message: "Unable to determine payload length".to_string(), path: None, }, PayloadError::Http2Payload(e) => APIErrorResponse { code: "ERR_HTTP2_ERROR".to_string(), message: format!("HTTP/2 error: {}", e), path: None, }, PayloadError::Io(e) => APIErrorResponse { code: "ERR_IO_ERROR".to_string(), message: format!("I/O error: {}", e), path: None, }, _ => APIErrorResponse { code: "ERR_UNKNOWN_ERROR".to_string(), message: "An unknown error has occured".to_string(), path: None, }, } } }