Replace expect usage with Result

This commit is contained in:
Tobias Reisinger 2023-12-05 01:42:19 +01:00
parent 9394a1ae52
commit b3228ea6b5
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
11 changed files with 135 additions and 95 deletions
emgauwa-lib/src/errors

View file

@ -2,6 +2,7 @@ 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)]
@ -13,7 +14,8 @@ pub enum DatabaseError {
Protected,
UpdateError,
UpdateGetError,
Unknown(String),
MigrationError(MigrateError),
Unknown(Error),
}
impl DatabaseError {
@ -53,6 +55,7 @@ impl From<&DatabaseError> for String {
DatabaseError::UpdateGetError => {
"error on retrieving updated model from database (your entry was saved)"
}
DatabaseError::MigrationError(_) => "error on running migrations",
DatabaseError::Unknown(_) => "unknown error",
})
}
@ -68,7 +71,13 @@ impl From<Error> for DatabaseError {
fn from(value: Error) -> Self {
match value {
Error::RowNotFound => DatabaseError::NotFound,
_ => DatabaseError::Unknown(value.to_string()),
_ => DatabaseError::Unknown(value),
}
}
}
impl From<MigrateError> for DatabaseError {
fn from(value: MigrateError) -> Self {
Self::MigrationError(value)
}
}

View file

@ -1,8 +1,11 @@
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 serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
@ -15,6 +18,7 @@ pub enum EmgauwaError {
Uid(uuid::Error),
Serialization(serde_json::Error),
Database(DatabaseError),
Other(String),
Internal(String),
Connection(ControllerUid),
}
@ -28,6 +32,7 @@ impl EmgauwaError {
EmgauwaError::Uid(_) => StatusCode::BAD_REQUEST,
EmgauwaError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
EmgauwaError::Connection(_) => StatusCode::GATEWAY_TIMEOUT,
EmgauwaError::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
@ -39,8 +44,9 @@ 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::Internal(_) => String::from("internal error"),
EmgauwaError::Connection(_) => String::from("the target controller is not connected"),
EmgauwaError::Other(err) => format!("other error: {}", err),
}
}
}
@ -81,12 +87,26 @@ impl From<MailboxError> for EmgauwaError {
}
}
impl From<ConfigError> for EmgauwaError {
fn from(value: ConfigError) -> Self {
Self::Other(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