Impelemented ApiError enum and implemented Display of error and status code return
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::write;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
http::StatusCode
|
http::StatusCode
|
||||||
@@ -29,8 +31,39 @@ pub enum ApiError {
|
|||||||
|
|
||||||
impl std::fmt::Display for ApiError {
|
impl std::fmt::Display for ApiError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
match Self {
|
match self {
|
||||||
//TODO: Add Api Error messages
|
//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()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user