diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..7131269 --- /dev/null +++ b/src/error.rs @@ -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"), + } + } +}