HomeDisk/types/src/errors/server.rs

79 lines
3.2 KiB
Rust
Raw Normal View History

2022-06-08 19:16:12 +00:00
use serde::{Deserialize, Serialize};
2022-06-23 09:52:48 +00:00
use thiserror::Error;
2022-06-08 19:16:12 +00:00
use super::{AuthError, FsError};
/// HTTP Server Error
2022-06-23 09:52:48 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Error)]
2022-06-08 19:16:12 +00:00
#[serde(tag = "error", content = "error_message", rename_all = "kebab-case")]
pub enum Error {
2022-06-11 08:19:47 +00:00
/// Auth error.
2022-06-08 22:02:20 +00:00
#[error("auth error - {0}")]
2022-06-08 19:16:12 +00:00
AuthError(#[from] AuthError),
2022-06-11 08:19:47 +00:00
/// File System Error.
2022-06-08 22:02:20 +00:00
#[error("fs error - {0}")]
2022-06-08 19:16:12 +00:00
FsError(#[from] FsError),
2022-06-11 08:19:47 +00:00
/// User sends too many requests.
2022-06-08 19:16:12 +00:00
#[error("too may requests, please slow down")]
TooManyRequests,
2022-06-11 08:19:47 +00:00
/// Missing Json in Content-Type Header.
2022-06-08 19:16:12 +00:00
#[error("missing json content type")]
MissingJsonContentType,
/// Failed to deserialize JSON.
2022-06-08 19:16:12 +00:00
#[error("error deserialize json")]
JsonDataError,
/// Syntax error in JSON.
2022-06-08 19:16:12 +00:00
#[error("json syntax error")]
JsonSyntaxError,
/// Failed to extract the Request body.
2022-06-08 19:16:12 +00:00
#[error("failed to extract the request body")]
BytesRejection,
/// Other error.
2022-06-08 22:02:20 +00:00
#[error("unknown error - {0}")]
2022-06-08 19:16:12 +00:00
Other(String),
}
#[cfg(feature = "axum")]
impl axum::response::IntoResponse for Error {
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,
2022-06-16 14:58:37 +00:00
AuthError::UserAlreadyExists => StatusCode::BAD_REQUEST,
AuthError::UsernameTooShort => StatusCode::BAD_REQUEST,
AuthError::UsernameTooLong => StatusCode::BAD_REQUEST,
AuthError::PasswordTooShort => StatusCode::BAD_REQUEST,
2022-06-08 19:16:12 +00:00
AuthError::TokenGenerate => StatusCode::INTERNAL_SERVER_ERROR,
AuthError::InvalidToken => StatusCode::BAD_REQUEST,
2022-06-11 08:19:47 +00:00
AuthError::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
2022-06-08 19:16:12 +00:00
},
Self::FsError(ref err) => match err {
FsError::FileAlreadyExists => StatusCode::BAD_REQUEST,
FsError::FileDoesNotExist => StatusCode::BAD_REQUEST,
FsError::MultipartError => StatusCode::BAD_REQUEST,
FsError::CreateFile(_) => StatusCode::INTERNAL_SERVER_ERROR,
FsError::CreateDirectory(_) => StatusCode::INTERNAL_SERVER_ERROR,
FsError::DeleteFile(_) => StatusCode::INTERNAL_SERVER_ERROR,
FsError::DeleteDirectory(_) => StatusCode::INTERNAL_SERVER_ERROR,
FsError::WriteFile(_) => StatusCode::INTERNAL_SERVER_ERROR,
FsError::Base64(_) => StatusCode::BAD_REQUEST,
2022-06-11 08:19:47 +00:00
FsError::ReadDirectory(_) => StatusCode::BAD_REQUEST,
FsError::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
2022-06-08 19:16:12 +00:00
},
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
}
}