added custom error handling
This commit is contained in:
parent
808c6a526c
commit
fe1334d97f
43
src/error.rs
Normal file
43
src/error.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ErrorKind {
|
||||||
|
BadRequest(String),
|
||||||
|
Conflict(String),
|
||||||
|
ForbiddenRequest(String),
|
||||||
|
NotFound(String),
|
||||||
|
UnprocessiableRequest(String),
|
||||||
|
JsonError(String),
|
||||||
|
Other,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for ErrorKind {}
|
||||||
|
|
||||||
|
impl fmt::Display for ErrorKind {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
ErrorKind::BadRequest(message) => {
|
||||||
|
write!(f, "Client error - please try again! {}", message)
|
||||||
|
}
|
||||||
|
ErrorKind::Conflict(message) => {
|
||||||
|
write!(f, "Client error - task is already underway! {}", message)
|
||||||
|
}
|
||||||
|
ErrorKind::ForbiddenRequest(message) => write!(
|
||||||
|
f,
|
||||||
|
"Client error - unauthorized. Please try again! {}",
|
||||||
|
message
|
||||||
|
),
|
||||||
|
ErrorKind::NotFound(message) => write!(f, "Client error - not found! {}", message),
|
||||||
|
ErrorKind::UnprocessiableRequest(message) => write!(
|
||||||
|
f,
|
||||||
|
"Client error - the request can't be processed. Please try again! {}",
|
||||||
|
message
|
||||||
|
),
|
||||||
|
ErrorKind::JsonError(message) => {
|
||||||
|
write!(f, "Client error - can't parse command! {}", message)
|
||||||
|
}
|
||||||
|
ErrorKind::Other => write!(f, "Other error that I did not anticipate"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user