Skip to content

Commit

Permalink
src: lib: server: Specify the server errors API
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso authored and patrickelectric committed Dec 19, 2024
1 parent 21c8706 commit 0b0a8d4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/lib/server/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use actix_web::{http::StatusCode, ResponseError};

use paperclip::actix::api_v2_errors;
use validator::ValidationErrors;

pub type Result<T> = actix_web::Result<T, Error>;

#[allow(dead_code)]
#[api_v2_errors(
code = 400,
description = "Bad Request: The client's request contains invalid or malformed data.",
code = 404,
description = "Not Found: The requested path or entity does not exist.",
code = 500,
description = "Internal Server Error: An unexpected server error has occurred.",
code = 503,
description = "Service Unavailable: ."
)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Bad Request: {0}")]
BadRequest(String),

#[error("Not Found: {0}")]
NotFound(String),

#[error("Internal Server Error: {0}")]
Internal(String),

#[error("Service Unavailable: {0}")]
Unavailable(String),
}

impl ResponseError for Error {
fn status_code(&self) -> StatusCode {
match self {
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
Self::NotFound(_) => StatusCode::NOT_FOUND,
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::Unavailable(_) => StatusCode::SERVICE_UNAVAILABLE,
}
}
}

impl From<ValidationErrors> for Error {
fn from(error: ValidationErrors) -> Self {
Self::BadRequest(error.to_string())
}
}

impl From<actix_web_validator::Error> for Error {
fn from(error: actix_web_validator::Error) -> Self {
Self::BadRequest(error.to_string())
}
}
1 change: 1 addition & 0 deletions src/lib/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod error;
pub mod manager;
mod pages;

0 comments on commit 0b0a8d4

Please sign in to comment.