Some more error handling

This commit is contained in:
2021-05-20 20:00:14 +02:00
parent 813660c022
commit b77c863065
6 changed files with 161 additions and 59 deletions

View File

@ -1,41 +1,113 @@
pub enum ErrorKind {
use std::fmt;
use std::borrow::Borrow;
#[derive(Copy, Clone)]
pub enum Kind {
InvalidEndpointError,
JsonParseError,
DatabaseConnectionError,
DatabaseError,
HttpRequestParseError,
IoError,
}
#[derive(Copy, Clone)]
pub enum Class {
ClientError,
ServerError,
}
pub struct Error {
kind: ErrorKind,
kind: Kind,
msg: Option<String>,
desc: Option<String>,
class: Class,
}
impl Error {
pub fn new(kind: ErrorKind) -> Error {
Error { kind, desc: None }
pub fn new(kind: Kind, class: Class) -> Self {
Error {
kind,
msg: None,
desc: None,
class,
}
}
pub fn class(&self) -> &Class {
&self.class
}
pub fn set_msg(mut self, msg: String) -> Self {
self.msg = Some(msg);
self
}
pub fn msg(&self) -> &str {
match &self.msg {
Some(msg) => msg.as_str(),
None => match self.kind {
Kind::InvalidEndpointError => "Invalid endpoint",
Kind::JsonParseError => "Unable to parse JSON data",
Kind::DatabaseConnectionError => "Unable to connect to database",
Kind::DatabaseError => "Database error",
Kind::HttpRequestParseError => "Unable to parse http request",
Kind::IoError => "IO error",
},
}
}
pub fn set_desc(mut self, desc: String) -> Self {
self.desc = Some(desc);
self
}
pub fn desc(&self) -> Option<&str> {
match &self.desc {
Some(desc) => Some(desc.as_str()),
None => None,
}
}
}
impl ToString for Error {
fn to_string(&self) -> String {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut error = match self.kind {
ErrorKind::InvalidEndpointError => "invalid endpoint",
ErrorKind::JsonParseError => "unable to parse JSON data",
ErrorKind::DatabaseError => "unable to connect to database",
Kind::InvalidEndpointError => "invalid endpoint",
Kind::JsonParseError => "unable to parse json data",
Kind::DatabaseConnectionError => "unable to connect to database",
Kind::DatabaseError => "database error",
Kind::HttpRequestParseError => "unable to parse http request",
Kind::IoError => "io error",
}
.to_string();
if let Some(desc) = &self.desc {
error += ": ";
error += desc;
}
error
write!(f, "{}", error);
Ok(())
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error {
kind: Kind::IoError,
msg: Some(error.to_string()),
desc: Some(error.to_string()),
class: Class::ClientError,
}
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Error {
kind: ErrorKind::JsonParseError,
kind: Kind::JsonParseError,
msg: Some("Unable to parse JSON data".to_string()),
desc: Some(error.to_string()),
class: Class::ClientError,
}
}
}
@ -43,8 +115,23 @@ impl From<serde_json::Error> for Error {
impl From<r2d2::Error> for Error {
fn from(error: r2d2::Error) -> Self {
Error {
kind: ErrorKind::DatabaseError,
kind: Kind::DatabaseConnectionError,
msg: Some("Unable to connect to database".to_string()),
desc: Some(error.to_string()),
class: Class::ServerError,
}
}
}
impl From<r2d2_postgres::postgres::Error> for Error {
fn from(error: r2d2_postgres::postgres::Error) -> Self {
// format: "db error: ERROR ..."
let msg = error.to_string().split(":").skip(1).collect::<String>();
Error {
kind: Kind::DatabaseError,
msg: Some("Database error".to_string()),
desc: Some(msg.trim().to_string()),
class: Class::ServerError,
}
}
}