-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
116 lines (100 loc) · 3.31 KB
/
errors.rs
File metadata and controls
116 lines (100 loc) · 3.31 KB
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
113
114
115
116
//! Error handling for the crate.
//!
//! This module defines common error types and their integration with the Actix Web framework.
use actix_web::{HttpResponse, ResponseError, http::StatusCode};
use serde::Serialize;
/// The response body for an error response
#[derive(Serialize)]
pub struct ErrorResponse {
/// The error message
pub message: String,
}
/// An error that can be returned from an API endpoint
#[derive(Debug)]
pub enum ApiError {
/// A bad request error
BadRequest(String),
/// An internal server error
InternalError(String),
/// Wrong configuration parameters
ConfigError(String),
}
impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ApiError::BadRequest(msg) => write!(f, "Bad Request: {msg}"),
ApiError::InternalError(msg) => write!(f, "Internal Error: {msg}"),
ApiError::ConfigError(msg) => write!(f, "Configuration Error: {msg}"),
}
}
}
impl std::error::Error for ApiError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl ResponseError for ApiError {
fn error_response(&self) -> HttpResponse {
let error_response = ErrorResponse {
message: self.to_string(),
};
match self {
ApiError::BadRequest(_) => HttpResponse::BadRequest().json(error_response),
ApiError::InternalError(_) | ApiError::ConfigError(_) => {
HttpResponse::InternalServerError().json(error_response)
}
}
}
fn status_code(&self) -> StatusCode {
match self {
ApiError::BadRequest(_) => StatusCode::BAD_REQUEST,
ApiError::InternalError(_) | ApiError::ConfigError(_) => {
StatusCode::INTERNAL_SERVER_ERROR
}
}
}
}
impl From<std::io::Error> for ApiError {
fn from(err: std::io::Error) -> Self {
ApiError::InternalError(err.to_string())
}
}
impl From<actix_multipart::MultipartError> for ApiError {
fn from(err: actix_multipart::MultipartError) -> Self {
ApiError::InternalError(err.to_string())
}
}
impl From<crate::core::errors::ParserError> for ApiError {
fn from(err: crate::core::errors::ParserError) -> Self {
ApiError::InternalError(err.to_string())
}
}
impl From<std::env::VarError> for ApiError {
fn from(err: std::env::VarError) -> Self {
ApiError::ConfigError(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
// Test each error variant's Display implementation
let bad_request = ApiError::BadRequest("invalid request".to_string());
let internal_error = ApiError::InternalError("server error".to_string());
assert_eq!(bad_request.to_string(), "Bad Request: invalid request");
assert_eq!(internal_error.to_string(), "Internal Error: server error");
}
#[test]
fn test_status_codes() {
// Test each error variant's status code
assert_eq!(
ApiError::BadRequest("test".to_string()).status_code(),
StatusCode::BAD_REQUEST
);
assert_eq!(
ApiError::InternalError("test".to_string()).status_code(),
StatusCode::INTERNAL_SERVER_ERROR
);
}
}