Skip to content

Commit

Permalink
Merge pull request #2 from spiralover/feat/chrono-error-handler
Browse files Browse the repository at this point in the history
feat(chrono-error-handler): handle chrono parse error using AppMessage
  • Loading branch information
Ahmard authored May 31, 2024
2 parents 1f880ea + 4c72db9 commit dc88c48
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/enums/app_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub enum AppMessage {
#[cfg(feature = "feat-crypto")]
JwtError(jsonwebtoken::errors::Error),
FromUtf8Error(std::string::FromUtf8Error),
ChronoParseError(chrono::ParseError),
#[cfg(feature = "feat-rabbitmq")]
RabbitmqError(lapin::Error),
RedisError(redis::RedisError),
Expand Down Expand Up @@ -129,6 +130,7 @@ fn get_message(status: &AppMessage) -> String {
#[cfg(feature = "feat-base64")]
AppMessage::Base64Error(error) => error.to_string(),
AppMessage::FromUtf8Error(error) => error.to_string(),
AppMessage::ChronoParseError(error) => error.to_string(),
#[cfg(feature = "feat-ntex")]
AppMessage::BlockingError(error) => error.to_string(),
#[cfg(feature = "feat-ntex")]
Expand Down Expand Up @@ -253,6 +255,11 @@ pub fn send_response(status: &AppMessage) -> ntex::web::HttpResponse {
AppMessage::UnAuthorizedMessageString(message) => {
json_error_message_status(message, StatusCode::UNAUTHORIZED)
}
AppMessage::ChronoParseError(error) => {
let message = error.to_string();
log::error!("Failed To Parse DateTime: {}", message);
json_error_message_status(&message, StatusCode::BAD_REQUEST)
}
#[cfg(feature = "feat-validator")]
AppMessage::FormValidationError(e) => crate::helpers::responder::json_error(
e,
Expand Down Expand Up @@ -282,6 +289,7 @@ fn get_status_code(status: &AppMessage) -> StatusCode {
AppMessage::JwtError(_) => StatusCode::INTERNAL_SERVER_ERROR,
AppMessage::R2d2Error(_) => StatusCode::INTERNAL_SERVER_ERROR,
AppMessage::IoError(_msg) => StatusCode::INTERNAL_SERVER_ERROR,
AppMessage::ChronoParseError(_msg) => StatusCode::BAD_REQUEST,
AppMessage::SerdeError(_msg) => StatusCode::BAD_REQUEST,
AppMessage::SerdeError500(_msg) => StatusCode::INTERNAL_SERVER_ERROR,
#[cfg(feature = "reqwest")]
Expand Down Expand Up @@ -380,6 +388,12 @@ impl From<serde_json::Error> for AppMessage {
}
}

impl From<chrono::ParseError> for AppMessage {
fn from(value: chrono::ParseError) -> Self {
AppMessage::ChronoParseError(value)
}
}

impl From<std::string::FromUtf8Error> for AppMessage {
fn from(value: std::string::FromUtf8Error) -> Self {
AppMessage::FromUtf8Error(value)
Expand Down

0 comments on commit dc88c48

Please sign in to comment.