141 lines
3.7 KiB
Rust
141 lines
3.7 KiB
Rust
#[derive(Debug)]
|
|
pub enum Error {
|
|
Generic(String),
|
|
UserCreate(String),
|
|
#[cfg(feature = "ssr")]
|
|
Sqlx(sqlx::Error),
|
|
#[cfg(feature = "ssr")]
|
|
TokioJoin(tokio::task::JoinError),
|
|
#[cfg(feature = "ssr")]
|
|
Pbkdf2(pbkdf2::password_hash::errors::Error),
|
|
#[cfg(feature = "ssr")]
|
|
Io(std::io::Error),
|
|
#[cfg(feature = "ssr")]
|
|
Axum(axum::Error),
|
|
AddrParse(std::net::AddrParseError),
|
|
Json(serde_json::Error),
|
|
}
|
|
|
|
impl std::fmt::Display for Error {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
Error::Generic(err) => {
|
|
write!(f, "generic error: {err}")
|
|
}
|
|
Error::UserCreate(err) => {
|
|
write!(f, "user create error: {err}")
|
|
}
|
|
#[cfg(feature = "ssr")]
|
|
Error::Sqlx(err) => {
|
|
write!(f, "sqlx error: {err:?}")
|
|
}
|
|
#[cfg(feature = "ssr")]
|
|
Error::TokioJoin(err) => {
|
|
write!(f, "tokio join error: {err:?}")
|
|
}
|
|
#[cfg(feature = "ssr")]
|
|
Error::Pbkdf2(err) => {
|
|
write!(f, "password hash error: {err:?}")
|
|
}
|
|
#[cfg(feature = "ssr")]
|
|
Error::Io(err) => {
|
|
write!(f, "io error: {err:?}")
|
|
}
|
|
Error::AddrParse(err) => {
|
|
write!(f, "ip address parse error: {err:?}")
|
|
}
|
|
Error::Json(err) => {
|
|
write!(f, "json encode/decode error: {err:?}")
|
|
}
|
|
#[cfg(feature = "ssr")]
|
|
Error::Axum(err) => {
|
|
write!(f, "axum error: {err:?}")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for Error {
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
match self {
|
|
#[cfg(feature = "ssr")]
|
|
Error::Sqlx(err) => Some(err),
|
|
#[cfg(feature = "ssr")]
|
|
Error::TokioJoin(err) => Some(err),
|
|
#[cfg(feature = "ssr")]
|
|
Error::Io(err) => Some(err),
|
|
Error::AddrParse(err) => Some(err),
|
|
Error::Json(err) => Some(err),
|
|
#[cfg(feature = "ssr")]
|
|
Error::Axum(err) => Some(err),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ssr")]
|
|
impl axum::response::IntoResponse for Error {
|
|
fn into_response(self) -> axum::response::Response {
|
|
(
|
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
format!("{:?}", self),
|
|
)
|
|
.into_response()
|
|
}
|
|
}
|
|
|
|
impl std::str::FromStr for Error {
|
|
type Err = Self;
|
|
|
|
fn from_str(err: &str) -> Result<Self, Self::Err> {
|
|
Ok(Self::Generic(err.to_string()))
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ssr")]
|
|
impl From<sqlx::Error> for Error {
|
|
fn from(err: sqlx::Error) -> Self {
|
|
Self::Sqlx(err)
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ssr")]
|
|
impl From<tokio::task::JoinError> for Error {
|
|
fn from(err: tokio::task::JoinError) -> Self {
|
|
Self::TokioJoin(err)
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ssr")]
|
|
impl From<pbkdf2::password_hash::errors::Error> for Error {
|
|
fn from(err: pbkdf2::password_hash::errors::Error) -> Self {
|
|
Self::Pbkdf2(err)
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ssr")]
|
|
impl From<std::io::Error> for Error {
|
|
fn from(err: std::io::Error) -> Self {
|
|
Self::Io(err)
|
|
}
|
|
}
|
|
|
|
impl From<std::net::AddrParseError> for Error {
|
|
fn from(err: std::net::AddrParseError) -> Self {
|
|
Self::AddrParse(err)
|
|
}
|
|
}
|
|
|
|
impl From<serde_json::Error> for Error {
|
|
fn from(err: serde_json::Error) -> Self {
|
|
Self::Json(err)
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ssr")]
|
|
impl From<axum::Error> for Error {
|
|
fn from(err: axum::Error) -> Self {
|
|
Self::Axum(err)
|
|
}
|
|
}
|