Refactor more stuff

This commit is contained in:
Tobias Reisinger 2023-12-04 23:59:26 +01:00
parent 5a7b2de0ea
commit 9394a1ae52
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
15 changed files with 167 additions and 86 deletions
emgauwa-lib/src/errors

View file

@ -1,17 +1,22 @@
use std::fmt::{Debug, Display, Formatter};
use actix::MailboxError;
use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use crate::errors::{ApiError, DatabaseError};
use crate::types::ControllerUid;
#[derive(Debug)]
pub enum EmgauwaError {
Api(ApiError),
Uid(uuid::Error),
Serialization(serde_json::Error),
Database(DatabaseError),
Internal(String),
Connection(ControllerUid),
}
impl EmgauwaError {
@ -21,6 +26,8 @@ impl EmgauwaError {
EmgauwaError::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
EmgauwaError::Database(err) => err.get_code(),
EmgauwaError::Uid(_) => StatusCode::BAD_REQUEST,
EmgauwaError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
EmgauwaError::Connection(_) => StatusCode::GATEWAY_TIMEOUT,
}
}
}
@ -32,6 +39,8 @@ impl From<&EmgauwaError> for String {
EmgauwaError::Serialization(_) => String::from("error during (de-)serialization"),
EmgauwaError::Database(err) => String::from(err),
EmgauwaError::Uid(_) => String::from("the uid is in a bad format"),
EmgauwaError::Internal(_) => String::from("general error"),
EmgauwaError::Connection(_) => String::from("the target controller is not connected"),
}
}
}
@ -66,6 +75,12 @@ impl From<uuid::Error> for EmgauwaError {
}
}
impl From<MailboxError> for EmgauwaError {
fn from(value: MailboxError) -> Self {
EmgauwaError::Internal(value.to_string())
}
}
impl From<&EmgauwaError> for HttpResponse {
fn from(err: &EmgauwaError) -> Self {
HttpResponse::build(err.get_code()).json(err)
@ -90,12 +105,6 @@ impl Display for EmgauwaError {
}
}
impl Debug for EmgauwaError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", String::from(self))
}
}
impl actix_web::error::ResponseError for EmgauwaError {
fn status_code(&self) -> StatusCode {
self.get_code()