2022-02-08 04:04:45 +00:00
|
|
|
use actix_web::http::StatusCode;
|
2022-02-04 19:36:54 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
|
|
|
pub enum ErrorCode {
|
|
|
|
NotFound,
|
|
|
|
DatabaseError,
|
2022-02-05 06:11:24 +00:00
|
|
|
UnAuthorized,
|
2022-02-04 19:36:54 +00:00
|
|
|
Internal,
|
2022-02-08 04:04:45 +00:00
|
|
|
NoSetup,
|
2022-02-04 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Error {
|
|
|
|
code: ErrorCode,
|
|
|
|
message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
2022-02-05 06:11:24 +00:00
|
|
|
pub fn new(code: ErrorCode, message: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
code,
|
|
|
|
message: message.into(),
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 19:36:54 +00:00
|
|
|
pub fn not_found() -> Self {
|
|
|
|
Self {
|
|
|
|
code: ErrorCode::NotFound,
|
|
|
|
message: "Resource not found".to_string(),
|
|
|
|
}
|
|
|
|
}
|
2022-02-05 06:11:24 +00:00
|
|
|
pub fn unauthorized() -> Self {
|
|
|
|
Self {
|
|
|
|
code: ErrorCode::NotFound,
|
|
|
|
message: "Unauthorzed".to_string(),
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 21:34:33 +00:00
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(source: std::io::Error) -> Self {
|
|
|
|
Self {
|
|
|
|
code: ErrorCode::Internal,
|
|
|
|
message: source.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 19:36:54 +00:00
|
|
|
impl From<&str> for Error {
|
|
|
|
fn from(s: &str) -> Self {
|
|
|
|
Error {
|
|
|
|
code: ErrorCode::Internal,
|
|
|
|
message: s.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 04:04:45 +00:00
|
|
|
impl actix_web::error::ResponseError for Error {
|
|
|
|
fn status_code(&self) -> actix_web::http::StatusCode {
|
|
|
|
match self.code {
|
|
|
|
ErrorCode::UnAuthorized => StatusCode::UNAUTHORIZED,
|
2022-02-12 08:03:27 +00:00
|
|
|
ErrorCode::NoSetup => StatusCode::UPGRADE_REQUIRED,
|
2022-02-08 04:04:45 +00:00
|
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 19:36:54 +00:00
|
|
|
|
|
|
|
impl From<sea_orm::DbErr> for Error {
|
|
|
|
fn from(e: sea_orm::DbErr) -> Self {
|
|
|
|
Self {
|
|
|
|
code: ErrorCode::DatabaseError,
|
|
|
|
message: e.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 21:23:31 +00:00
|
|
|
impl From<sqlx::Error> for Error {
|
|
|
|
fn from(e: sqlx::Error) -> Self {
|
|
|
|
Self {
|
|
|
|
code: ErrorCode::DatabaseError,
|
|
|
|
message: e.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<sqlx::migrate::MigrateError> for Error {
|
|
|
|
fn from(e: sqlx::migrate::MigrateError) -> Self {
|
|
|
|
Self {
|
|
|
|
code: ErrorCode::DatabaseError,
|
|
|
|
message: e.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 19:36:54 +00:00
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.message)
|
|
|
|
}
|
|
|
|
}
|