Impelemented ApiError enum and implemented Display of error and status code return

This commit is contained in:
2026-03-15 11:29:08 +01:00
parent baa20499f0
commit d2292b8912

View File

@@ -1,3 +1,5 @@
use std::fmt::write;
use axum::{
response::{IntoResponse, Response},
http::StatusCode
@@ -29,8 +31,39 @@ pub enum ApiError {
impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match Self {
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()
}
}