clean up error handling
parent
f5ff64b88d
commit
95ce2d4668
@ -0,0 +1,46 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Ærror {
|
||||
IOError(String),
|
||||
ParseError(String),
|
||||
ApplicationError(String),
|
||||
}
|
||||
|
||||
impl Ærror {
|
||||
pub fn new(message: String) -> Ærror {
|
||||
Ærror::ApplicationError(message)
|
||||
}
|
||||
|
||||
pub fn new_str(message: &str) -> Ærror {
|
||||
Ærror::ApplicationError(String::from(message))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Ærror {}
|
||||
|
||||
impl fmt::Display for Ærror {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Ærror::IOError(message) => formatter.write_fmt(format_args!("IO Error: {}", message)),
|
||||
Ærror::ParseError(message) => {
|
||||
formatter.write_fmt(format_args!("Parse Error: {}", message))
|
||||
}
|
||||
Ærror::ApplicationError(message) => {
|
||||
formatter.write_fmt(format_args!("Generic Error: {}", message))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Ærror {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
Ærror::IOError(error.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for Ærror {
|
||||
fn from(error: serde_json::Error) -> Self {
|
||||
Ærror::ParseError(error.to_string())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue