HomeDisk/types/src/errors/mod.rs

76 lines
2.7 KiB
Rust
Raw Normal View History

mod auth;
2022-04-24 19:31:50 +00:00
mod fs;
pub use auth::Error as AuthError;
2022-04-24 19:31:50 +00:00
pub use fs::Error as FsError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)]
2022-04-29 19:33:44 +00:00
#[serde(tag = "error", content = "error_message", rename_all = "kebab-case")]
pub enum ServerError {
#[error("auth error: {0}")]
AuthError(#[from] AuthError),
2022-04-24 19:31:50 +00:00
#[error("fs error: {0}")]
FsError(#[from] FsError),
#[error("too may requests, please slow down")]
TooManyRequests,
#[error("missing json content type")]
MissingJsonContentType,
#[error("error deserialize json")]
JsonDataError,
#[error("json syntax error")]
JsonSyntaxError,
#[error("failed to extract the request body")]
BytesRejection,
#[error("unexcepted error")]
Other(String),
}
#[cfg(feature = "axum")]
impl axum::response::IntoResponse for ServerError {
fn into_response(self) -> axum::response::Response {
use axum::http::StatusCode;
let status = match self {
Self::AuthError(ref err) => match err {
AuthError::UserNotFound => StatusCode::BAD_REQUEST,
AuthError::UserAlreadyExists => StatusCode::NOT_ACCEPTABLE,
2022-04-30 19:56:06 +00:00
AuthError::UsernameTooShort => StatusCode::NOT_ACCEPTABLE,
AuthError::UsernameTooLong => StatusCode::NOT_ACCEPTABLE,
AuthError::PasswordTooShort => StatusCode::NOT_ACCEPTABLE,
AuthError::TokenGenerate => StatusCode::INTERNAL_SERVER_ERROR,
AuthError::InvalidToken => StatusCode::BAD_REQUEST,
2022-05-01 20:44:28 +00:00
AuthError::UnknownError(_) => StatusCode::INTERNAL_SERVER_ERROR,
},
2022-04-24 19:31:50 +00:00
Self::FsError(ref err) => match err {
FsError::MultipartError => StatusCode::BAD_REQUEST,
2022-04-24 19:31:50 +00:00
FsError::FileAlreadyExists => StatusCode::BAD_REQUEST,
FsError::CreateFile(_) => StatusCode::INTERNAL_SERVER_ERROR,
2022-04-24 19:31:50 +00:00
FsError::WriteFile(_) => StatusCode::INTERNAL_SERVER_ERROR,
FsError::Base64(_) => StatusCode::BAD_REQUEST,
2022-04-24 20:07:41 +00:00
FsError::ReadDir(_) => StatusCode::BAD_REQUEST,
2022-04-24 19:31:50 +00:00
FsError::UnknowError(_) => StatusCode::INTERNAL_SERVER_ERROR,
},
Self::TooManyRequests => StatusCode::TOO_MANY_REQUESTS,
Self::MissingJsonContentType => StatusCode::BAD_REQUEST,
Self::JsonDataError => StatusCode::BAD_REQUEST,
Self::JsonSyntaxError => StatusCode::BAD_REQUEST,
Self::BytesRejection => StatusCode::INTERNAL_SERVER_ERROR,
Self::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let mut response = axum::Json(self).into_response();
*response.status_mut() = status;
response
}
}