Skip to content

Commit

Permalink
fix: add proper messages to internal server error (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
dracarys18 authored Jul 24, 2024
1 parent cabbd87 commit 6c773d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/core/datakey/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ pub async fn transfer_data_key(
req: TransferKeyRequest,
) -> errors::CustomResult<DataKeyCreateResponse, errors::ApplicationErrorResponse> {
let db = &state.db_pool;
let key = BASE64_ENGINE
.decode(req.key)
.change_context(errors::ApplicationErrorResponse::InternalServerError)?;
let key = <[u8; 32]>::try_from(key)
.map_err(|_| error_stack::report!(errors::ApplicationErrorResponse::InternalServerError))?;
let key = BASE64_ENGINE.decode(req.key).change_context(
errors::ApplicationErrorResponse::InternalServerError("Failed to decode the base64 key"),
)?;
let key = <[u8; 32]>::try_from(key).map_err(|_| {
error_stack::report!(errors::ApplicationErrorResponse::InternalServerError(
"Invalid key found"
))
})?;
let key = Key {
version: Version::default(),
identifier: req.identifier.clone(),
Expand Down
38 changes: 30 additions & 8 deletions src/errors/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use super::SwitchError;
use axum::response::{IntoResponse, Response};
use hyper::StatusCode;

use error_stack::ResultExt;

pub type ApiResponseResult<T> = Result<T, ApiErrorContainer>;

pub struct ApiErrorContainer {
Expand Down Expand Up @@ -44,13 +42,15 @@ struct ApiErrorResponse<'a> {

#[derive(Debug, thiserror::Error)]
pub enum ApplicationErrorResponse {
#[error("Something Went Wrong")]
InternalServerError,
#[error("Internal Server Error Occurred - {0}")]
InternalServerError(&'static str),
#[error("The resource was not found in the {0}")]
NotFound(&'static str),
#[error("Invalid request provided {0}")]
ParsingFailed(String),
#[error("Unique violation occured. Please try to create the data with another key/identifier")]
#[error(
"Unique violation occurred. Please try to create the data with another key/identifier"
)]
UniqueViolation,
}

Expand All @@ -69,7 +69,27 @@ impl<T> SwitchError<T, ApplicationErrorResponse> for super::CustomResult<T, Pars

impl<T> SwitchError<T, ApplicationErrorResponse> for super::CustomResult<T, super::CryptoError> {
fn switch(self) -> super::CustomResult<T, ApplicationErrorResponse> {
self.change_context(ApplicationErrorResponse::InternalServerError)
self.map_err(|err| {
let new_err = match err.current_context() {
super::CryptoError::EncryptionFailed(_) => {
ApplicationErrorResponse::InternalServerError("Encryption failed")
}
super::CryptoError::DecryptionFailed(_) => {
ApplicationErrorResponse::InternalServerError("Decryption failed")
}
super::CryptoError::KeyGeneration => {
ApplicationErrorResponse::InternalServerError("Key generation failed")
}
super::CryptoError::InvalidKey => {
ApplicationErrorResponse::InternalServerError("Invalid key detected")
}
super::CryptoError::KeyGetFailed => {
ApplicationErrorResponse::InternalServerError("Failed to get the key")
}
_ => ApplicationErrorResponse::InternalServerError("Unexpected error occurred"),
};
err.change_context(new_err)
})
}
}

Expand All @@ -81,7 +101,9 @@ impl<T> SwitchError<T, ApplicationErrorResponse> for super::CustomResult<T, supe
super::DatabaseError::ConnectionError(_)
| super::DatabaseError::NotNullViolation
| super::DatabaseError::InvalidValue
| super::DatabaseError::Others => ApplicationErrorResponse::InternalServerError,
| super::DatabaseError::Others => {
ApplicationErrorResponse::InternalServerError("Database error occurred")
}
super::DatabaseError::UniqueViolation => ApplicationErrorResponse::UniqueViolation,
};
err.change_context(new_err)
Expand All @@ -98,7 +120,7 @@ impl<T> ToContainerError<T> for super::CustomResult<T, ApplicationErrorResponse>
impl IntoResponse for ApiErrorContainer {
fn into_response(self) -> Response {
match self.error.current_context() {
err @ ApplicationErrorResponse::InternalServerError => (
err @ ApplicationErrorResponse::InternalServerError(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(ApiErrorResponse {
error_message: err.to_string(),
Expand Down

0 comments on commit 6c773d3

Please sign in to comment.