68 lines
2.2 KiB
Rust
68 lines
2.2 KiB
Rust
use axum::{
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum ApiError {
|
|
// bucket errors
|
|
BucketNotFound,
|
|
BucketAlreadyExists,
|
|
|
|
// object errors
|
|
ObjectNotFound,
|
|
|
|
// request errors
|
|
InvalidArgument(String),
|
|
InvalidBucketName,
|
|
|
|
// auth errors
|
|
AuthorizationFailed,
|
|
MissingAuthHeader,
|
|
|
|
// server errors
|
|
InternalError(String),
|
|
|
|
// not implemented yet
|
|
NotImplemented,
|
|
}
|
|
|
|
impl std::fmt::Display for ApiError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
match self {
|
|
//TODO: Add Api Error messages
|
|
ApiError::AuthorizationFailed => write!(f, "AuthorizationFailed"),
|
|
ApiError::BucketAlreadyExists => write!(f, "BucketAlreadyExists"),
|
|
ApiError::BucketNotFound => write!(f, "BucketNotFound"),
|
|
ApiError::InternalError(message) => write!(f, "InternalError: {}", message),
|
|
ApiError::InvalidArgument(message) => write!(f, "InvalidArgument: {}", message),
|
|
ApiError::InvalidBucketName => write!(f, "InvalidBucketName"),
|
|
ApiError::MissingAuthHeader => write!(f, "MissingAuthHeader"),
|
|
ApiError::NotImplemented => write!(f, "NotImplemented"),
|
|
ApiError::ObjectNotFound => write!(f, "ObjectNotFound"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ApiError {
|
|
fn status_code(&self) -> StatusCode {
|
|
match self {
|
|
ApiError::BucketNotFound => StatusCode::NOT_FOUND,
|
|
ApiError::ObjectNotFound => StatusCode::NOT_FOUND,
|
|
ApiError::BucketAlreadyExists => StatusCode::CONFLICT,
|
|
ApiError::InvalidArgument(_) => StatusCode::BAD_REQUEST,
|
|
ApiError::InvalidBucketName => StatusCode::BAD_REQUEST,
|
|
ApiError::AuthorizationFailed => StatusCode::FORBIDDEN,
|
|
ApiError::MissingAuthHeader => StatusCode::UNAUTHORIZED,
|
|
ApiError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
|
ApiError::NotImplemented => StatusCode::NOT_IMPLEMENTED,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl IntoResponse for ApiError {
|
|
fn into_response(self) -> Response {
|
|
(self.status_code(), self.to_string()).into_response()
|
|
}
|
|
}
|