From fe1334d97f0569913d4dda0538cd87541fdff3d9 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Fri, 30 Aug 2024 08:15:18 -0400 Subject: [PATCH] added custom error handling --- src/error.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/error.rs 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"), + } + } +}