Split project (keep common)
This commit is contained in:
parent
9bc75b9627
commit
96a64cf90d
68 changed files with 32 additions and 3502 deletions
22
src/errors/api_error.rs
Normal file
22
src/errors/api_error.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use actix_web::http::StatusCode;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ApiError {
|
||||
ProtectedSchedule,
|
||||
}
|
||||
|
||||
impl ApiError {
|
||||
pub fn get_code(&self) -> StatusCode {
|
||||
match self {
|
||||
ApiError::ProtectedSchedule => StatusCode::FORBIDDEN,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&ApiError> for String {
|
||||
fn from(err: &ApiError) -> Self {
|
||||
match err {
|
||||
ApiError::ProtectedSchedule => String::from("the targeted schedule is protected"),
|
||||
}
|
||||
}
|
||||
}
|
||||
85
src/errors/database_error.rs
Normal file
85
src/errors/database_error.rs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
use actix_web::http::StatusCode;
|
||||
use actix_web::HttpResponse;
|
||||
use serde::ser::SerializeStruct;
|
||||
use serde::{Serialize, Serializer};
|
||||
use sqlx::migrate::MigrateError;
|
||||
use sqlx::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DatabaseError {
|
||||
DeleteError,
|
||||
InsertError,
|
||||
InsertGetError,
|
||||
NotFound,
|
||||
Protected,
|
||||
EmptyDataInsert,
|
||||
UpdateError,
|
||||
UpdateGetError,
|
||||
MigrationError(MigrateError),
|
||||
Unknown(Error),
|
||||
}
|
||||
|
||||
impl DatabaseError {
|
||||
pub fn get_code(&self) -> StatusCode {
|
||||
match self {
|
||||
DatabaseError::NotFound => StatusCode::NOT_FOUND,
|
||||
DatabaseError::Protected => StatusCode::FORBIDDEN,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for DatabaseError {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("error", 3)?;
|
||||
s.serialize_field("type", "database-error")?;
|
||||
s.serialize_field("code", &self.get_code().as_u16())?;
|
||||
s.serialize_field("description", &String::from(self))?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&DatabaseError> for String {
|
||||
fn from(err: &DatabaseError) -> Self {
|
||||
String::from(match err {
|
||||
DatabaseError::InsertError => "error on inserting into database",
|
||||
DatabaseError::InsertGetError => {
|
||||
"error on retrieving new entry from database (your entry was saved)"
|
||||
}
|
||||
DatabaseError::NotFound => "model was not found in database",
|
||||
DatabaseError::DeleteError => "error on deleting from database",
|
||||
DatabaseError::Protected => "model is protected",
|
||||
DatabaseError::UpdateError => "error on updating the model",
|
||||
DatabaseError::UpdateGetError => {
|
||||
"error on retrieving updated model from database (your entry was saved)"
|
||||
}
|
||||
DatabaseError::MigrationError(_) => "error on running migrations",
|
||||
DatabaseError::Unknown(_) => "unknown error",
|
||||
DatabaseError::EmptyDataInsert => "tried to insert empty data",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DatabaseError> for HttpResponse {
|
||||
fn from(err: DatabaseError) -> Self {
|
||||
HttpResponse::build(err.get_code()).json(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for DatabaseError {
|
||||
fn from(value: Error) -> Self {
|
||||
match value {
|
||||
Error::RowNotFound => DatabaseError::NotFound,
|
||||
_ => DatabaseError::Unknown(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MigrateError> for DatabaseError {
|
||||
fn from(value: MigrateError) -> Self {
|
||||
Self::MigrationError(value)
|
||||
}
|
||||
}
|
||||
161
src/errors/emgauwa_error.rs
Normal file
161
src/errors/emgauwa_error.rs
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
use std::error::Error;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
use std::io::ErrorKind;
|
||||
|
||||
use actix::MailboxError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::HttpResponse;
|
||||
use config::ConfigError;
|
||||
use rppal::gpio;
|
||||
use rppal_mcp23s17::Mcp23s17Error;
|
||||
use rppal_pfd::PiFaceDigitalError;
|
||||
use serde::ser::SerializeStruct;
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
use crate::errors::{ApiError, DatabaseError};
|
||||
use crate::types::EmgauwaUid;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum EmgauwaError {
|
||||
Api(ApiError),
|
||||
Uid(uuid::Error),
|
||||
Serialization(serde_json::Error),
|
||||
Database(DatabaseError),
|
||||
Other(String),
|
||||
Internal(String),
|
||||
Connection(EmgauwaUid),
|
||||
Hardware(String),
|
||||
}
|
||||
|
||||
impl EmgauwaError {
|
||||
fn get_code(&self) -> StatusCode {
|
||||
match self {
|
||||
EmgauwaError::Api(err) => err.get_code(),
|
||||
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,
|
||||
EmgauwaError::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
EmgauwaError::Hardware(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&EmgauwaError> for String {
|
||||
fn from(err: &EmgauwaError) -> Self {
|
||||
match err {
|
||||
EmgauwaError::Api(err) => String::from(err),
|
||||
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("internal error"),
|
||||
EmgauwaError::Connection(_) => String::from("the target controller is not connected"),
|
||||
EmgauwaError::Other(err) => format!("other error: {}", err),
|
||||
EmgauwaError::Hardware(err) => format!("hardware error: {}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ApiError> for EmgauwaError {
|
||||
fn from(value: ApiError) -> Self {
|
||||
EmgauwaError::Api(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DatabaseError> for EmgauwaError {
|
||||
fn from(value: DatabaseError) -> Self {
|
||||
EmgauwaError::Database(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for EmgauwaError {
|
||||
fn from(value: serde_json::Error) -> Self {
|
||||
EmgauwaError::Serialization(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for EmgauwaError {
|
||||
fn from(value: sqlx::Error) -> Self {
|
||||
EmgauwaError::Database(DatabaseError::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<uuid::Error> for EmgauwaError {
|
||||
fn from(value: uuid::Error) -> Self {
|
||||
EmgauwaError::Uid(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MailboxError> for EmgauwaError {
|
||||
fn from(value: MailboxError) -> Self {
|
||||
EmgauwaError::Internal(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ConfigError> for EmgauwaError {
|
||||
fn from(value: ConfigError) -> Self {
|
||||
Self::Other(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<gpio::Error> for EmgauwaError {
|
||||
fn from(value: gpio::Error) -> Self {
|
||||
Self::Hardware(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PiFaceDigitalError> for EmgauwaError {
|
||||
fn from(value: PiFaceDigitalError) -> Self {
|
||||
match value {
|
||||
PiFaceDigitalError::Mcp23s17Error { source } => match source {
|
||||
Mcp23s17Error::SpiError { source } => Self::Hardware(source.to_string()),
|
||||
_ => Self::Hardware(source.to_string()),
|
||||
},
|
||||
PiFaceDigitalError::GpioError { source } => Self::Hardware(source.to_string()),
|
||||
_ => Self::Hardware(value.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&EmgauwaError> for HttpResponse {
|
||||
fn from(err: &EmgauwaError) -> Self {
|
||||
HttpResponse::build(err.get_code()).json(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for EmgauwaError {}
|
||||
|
||||
impl From<EmgauwaError> for std::io::Error {
|
||||
fn from(value: EmgauwaError) -> Self {
|
||||
std::io::Error::new(ErrorKind::Other, value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for EmgauwaError {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("error", 2)?;
|
||||
s.serialize_field("code", &self.get_code().as_u16())?;
|
||||
s.serialize_field("description", &String::from(self))?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for EmgauwaError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}: {}", self.get_code(), String::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl actix_web::error::ResponseError for EmgauwaError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
self.get_code()
|
||||
}
|
||||
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
HttpResponse::from(self)
|
||||
}
|
||||
}
|
||||
7
src/errors/mod.rs
Normal file
7
src/errors/mod.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
mod api_error;
|
||||
mod database_error;
|
||||
mod emgauwa_error;
|
||||
|
||||
pub use api_error::ApiError;
|
||||
pub use database_error::DatabaseError;
|
||||
pub use emgauwa_error::EmgauwaError;
|
||||
Loading…
Add table
Add a link
Reference in a new issue