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)
}
}