2022-04-18 12:13:15 +00:00
|
|
|
mod auth;
|
2022-04-24 19:31:50 +00:00
|
|
|
mod fs;
|
2022-04-18 12:13:15 +00:00
|
|
|
|
|
|
|
pub use auth::Error as AuthError;
|
2022-04-24 19:31:50 +00:00
|
|
|
pub use fs::Error as FsError;
|
2022-04-18 12:13:15 +00:00
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)]
|
|
|
|
#[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),
|
|
|
|
|
2022-04-18 12:13:15 +00:00
|
|
|
#[error("too may requests, please slow down")]
|
|
|
|
TooManyRequests,
|
2022-04-23 19:21:04 +00:00
|
|
|
|
|
|
|
#[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),
|
2022-04-18 12:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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-21 18:54:53 +00:00
|
|
|
AuthError::TokenGenerate => StatusCode::INTERNAL_SERVER_ERROR,
|
2022-04-23 21:48:20 +00:00
|
|
|
AuthError::InvalidToken => StatusCode::BAD_REQUEST,
|
2022-04-24 19:31:50 +00:00
|
|
|
AuthError::UnknowError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
2022-04-18 12:13:15 +00:00
|
|
|
},
|
2022-04-24 19:31:50 +00:00
|
|
|
Self::FsError(ref err) => match err {
|
|
|
|
FsError::FileAlreadyExists => StatusCode::BAD_REQUEST,
|
|
|
|
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,
|
2022-04-23 19:21:04 +00:00
|
|
|
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,
|
2022-04-18 12:13:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut response = axum::Json(self).into_response();
|
|
|
|
*response.status_mut() = status;
|
|
|
|
|
|
|
|
response
|
|
|
|
}
|
|
|
|
}
|