Started reimplementation of the Rest api in actix-web
This commit is contained in:
60
backend-actix/src/request/error.rs
Normal file
60
backend-actix/src/request/error.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
use actix_web::{ResponseError, error::BlockingError};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use validator::ValidationErrors;
|
||||
|
||||
use crate::schema::error::DatabaseError;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ApiError {
|
||||
pub error: String
|
||||
}
|
||||
|
||||
impl Display for ApiError {
|
||||
// This trait requires `fmt` with this exact signature.
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
write!(f, "{}", self.error)
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseError for ApiError { }
|
||||
|
||||
impl From<DatabaseError> for ApiError {
|
||||
fn from(value: DatabaseError) -> Self {
|
||||
ApiError {
|
||||
error: value.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BlockingError> for ApiError {
|
||||
fn from(value: BlockingError) -> Self {
|
||||
ApiError {
|
||||
error: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for ApiError {
|
||||
fn from(value: serde_json::Error) -> Self {
|
||||
ApiError {
|
||||
error: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<jsonwebtoken::errors::Error> for ApiError {
|
||||
fn from(value: jsonwebtoken::errors::Error) -> Self {
|
||||
ApiError {
|
||||
error: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ValidationErrors> for ApiError {
|
||||
fn from(value: ValidationErrors) -> Self {
|
||||
ApiError {
|
||||
error: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user