Replace expect usage with Result
This commit is contained in:
parent
9394a1ae52
commit
b3228ea6b5
11 changed files with 135 additions and 95 deletions
emgauwa-lib/src/errors
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue