-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb49b12
commit 9c278d7
Showing
6 changed files
with
252 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use std::fmt::{self, Display}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use tokio::io; | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; | ||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] | ||
pub struct Error { | ||
pub message: String, | ||
pub kind: ErrorKind, | ||
pub status: Option<u16>, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] | ||
pub enum ErrorKind { | ||
NotFound, | ||
Serialization, | ||
Deserialization, | ||
Authentication, | ||
Other, | ||
} | ||
|
||
impl Error { | ||
pub fn not_found(message: impl Display) -> Self { | ||
Self { | ||
message: message.to_string(), | ||
kind: ErrorKind::NotFound, | ||
status: None, | ||
} | ||
} | ||
pub fn serialization(message: impl Display) -> Self { | ||
Self { | ||
message: message.to_string(), | ||
kind: ErrorKind::Serialization, | ||
status: None, | ||
} | ||
} | ||
pub fn deserialization(message: impl Display) -> Self { | ||
Self { | ||
message: message.to_string(), | ||
kind: ErrorKind::Deserialization, | ||
status: None, | ||
} | ||
} | ||
pub fn authentication(message: impl Display) -> Self { | ||
Self { | ||
message: message.to_string(), | ||
kind: ErrorKind::Authentication, | ||
status: None, | ||
} | ||
} | ||
pub fn other(message: impl Display) -> Self { | ||
Self { | ||
message: message.to_string(), | ||
kind: ErrorKind::Other, | ||
status: None, | ||
} | ||
} | ||
} | ||
|
||
impl Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", self.message) | ||
} | ||
} | ||
impl std::error::Error for Error {} | ||
|
||
impl From<std::io::Error> for Error { | ||
fn from(value: std::io::Error) -> Self { | ||
let kind = match value.kind() { | ||
io::ErrorKind::NotFound => ErrorKind::NotFound, | ||
_ => ErrorKind::Other, | ||
}; | ||
Self { | ||
message: format!("{value}"), | ||
status: None, | ||
kind, | ||
} | ||
} | ||
} | ||
impl From<serde_json::Error> for Error { | ||
fn from(value: serde_json::Error) -> Self { | ||
Self { | ||
message: format!("{value}"), | ||
kind: ErrorKind::Deserialization, | ||
status: None, | ||
} | ||
} | ||
} | ||
impl From<toml::de::Error> for Error { | ||
fn from(value: toml::de::Error) -> Self { | ||
Self { | ||
message: value.message().to_string(), | ||
kind: ErrorKind::Deserialization, | ||
status: None, | ||
} | ||
} | ||
} | ||
impl From<toml::ser::Error> for Error { | ||
fn from(value: toml::ser::Error) -> Self { | ||
Self { | ||
message: format!("{value}"), | ||
kind: ErrorKind::Serialization, | ||
status: None, | ||
} | ||
} | ||
} | ||
#[cfg(feature = "keyring")] | ||
impl From<keyring::Error> for Error { | ||
fn from(value: keyring::Error) -> Self { | ||
Self { | ||
message: format!("{value}"), | ||
kind: ErrorKind::Authentication, | ||
status: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.