use activitystreams_vocabulary::Error as ActivityStreamsError;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
Vocabulary(String),
Commit(String),
Content(String),
Hash(String),
Activity(String),
Actor(String),
Object(String),
ActivityStreams(ActivityStreamsError),
Sql(String),
Io(String),
Uuid(String),
Crypto(String),
Http(String),
Db(String),
}
impl Error {
pub fn vocabulary<I: Into<String>>(err: I) -> Self {
Self::Vocabulary(err.into())
}
pub fn commit<I: Into<String>>(err: I) -> Self {
Self::Commit(err.into())
}
pub fn content<I: Into<String>>(err: I) -> Self {
Self::Content(err.into())
}
pub fn hash<I: Into<String>>(err: I) -> Self {
Self::Hash(err.into())
}
pub fn activity<I: Into<String>>(err: I) -> Self {
Self::Activity(err.into())
}
pub fn actor<I: Into<String>>(err: I) -> Self {
Self::Actor(err.into())
}
pub fn object<I: Into<String>>(err: I) -> Self {
Self::Object(err.into())
}
pub fn activity_streams<I: Into<ActivityStreamsError>>(err: I) -> Self {
Self::ActivityStreams(err.into())
}
pub fn io<I: Into<String>>(err: I) -> Self {
Self::Io(err.into())
}
pub fn sql<I: Into<String>>(err: I) -> Self {
Self::Sql(err.into())
}
pub fn uuid<I: Into<String>>(err: I) -> Self {
Self::Uuid(err.into())
}
pub fn crypto<I: Into<String>>(err: I) -> Self {
Self::Crypto(err.into())
}
pub fn http<I: Into<String>>(err: I) -> Self {
Self::Http(err.into())
}
pub fn db<I: Into<String>>(err: I) -> Self {
Self::Db(err.into())
}
}
impl From<ActivityStreamsError> for Error {
fn from(err: ActivityStreamsError) -> Self {
Self::ActivityStreams(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::Io(err.to_string())
}
}
impl From<sqlx::Error> for Error {
fn from(err: sqlx::Error) -> Self {
Self::sql(err.to_string())
}
}
impl From<sqlx::migrate::MigrateError> for Error {
fn from(err: sqlx::migrate::MigrateError) -> Self {
Self::sql(err.to_string())
}
}
impl From<uuid::Error> for Error {
fn from(err: uuid::Error) -> Self {
Self::uuid(err.to_string())
}
}
impl From<tokio::task::JoinError> for Error {
fn from(err: tokio::task::JoinError) -> Self {
Self::io(format!("async join error: {err}"))
}
}
impl From<argon2::Error> for Error {
fn from(err: argon2::Error) -> Self {
Self::crypto(err.to_string())
}
}
impl From<chacha20::Error> for Error {
fn from(err: chacha20::Error) -> Self {
Self::crypto(err.to_string())
}
}
impl From<sha3::digest::InvalidLength> for Error {
fn from(err: sha3::digest::InvalidLength) -> Self {
Self::crypto(err.to_string())
}
}
impl From<tokio::sync::TryLockError> for Error {
fn from(err: tokio::sync::TryLockError) -> Self {
Self::io(err.to_string())
}
}
impl From<http::header::ToStrError> for Error {
fn from(err: http::header::ToStrError) -> Self {
Self::io(err.to_string())
}
}
impl From<httpsig::prelude::HttpSigError> for Error {
fn from(err: httpsig::prelude::HttpSigError) -> Self {
Self::crypto(format!("httpsig: {err}"))
}
}
impl From<rsa::pkcs1::Error> for Error {
fn from(err: rsa::pkcs1::Error) -> Self {
Self::crypto(format!("pkcs1: {err}"))
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Self {
Self::io(err.to_string())
}
}
impl From<std::str::Utf8Error> for Error {
fn from(err: std::str::Utf8Error) -> Self {
Self::io(err.to_string())
}
}
impl From<axum::Error> for Error {
fn from(err: axum::Error) -> Self {
Self::http(format!("axum: {err}"))
}
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Self::http(format!("reqwest: {err}"))
}
}
impl From<http::Error> for Error {
fn from(err: http::Error) -> Self {
Self::http(format!("{err}"))
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Self::http(format!("json: {err}"))
}
}
impl From<ecdsa::Error> for Error {
fn from(err: ecdsa::Error) -> Self {
Self::crypto(format!("ecdsa: {err}"))
}
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Vocabulary(err) => write!(f, "vocabulary: {err}"),
Self::Commit(err) => write!(f, "commit: {err}"),
Self::Content(err) => write!(f, "content: {err}"),
Self::Hash(err) => write!(f, "hash: {err}"),
Self::Activity(err) => write!(f, "activity: {err}"),
Self::Actor(err) => write!(f, "actor: {err}"),
Self::Object(err) => write!(f, "object: {err}"),
Self::ActivityStreams(err) => write!(f, "activitystreams_vocabulary: {err}"),
Self::Sql(err) => write!(f, "sql: {err}"),
Self::Io(err) => write!(f, "io: {err}"),
Self::Uuid(err) => write!(f, "uuid: {err}"),
Self::Crypto(err) => write!(f, "crypto: {err}"),
Self::Http(err) => write!(f, "http: {err}"),
Self::Db(err) => write!(f, "db: {err}"),
}
}
}
impl core::error::Error for Error {}
pub type Result<T> = core::result::Result<T, Error>;