Skip to content

Commit 6c773d3

Browse files
authored
fix: add proper messages to internal server error (#20)
1 parent cabbd87 commit 6c773d3

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/core/datakey/transfer.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ pub async fn transfer_data_key(
1919
req: TransferKeyRequest,
2020
) -> errors::CustomResult<DataKeyCreateResponse, errors::ApplicationErrorResponse> {
2121
let db = &state.db_pool;
22-
let key = BASE64_ENGINE
23-
.decode(req.key)
24-
.change_context(errors::ApplicationErrorResponse::InternalServerError)?;
25-
let key = <[u8; 32]>::try_from(key)
26-
.map_err(|_| error_stack::report!(errors::ApplicationErrorResponse::InternalServerError))?;
22+
let key = BASE64_ENGINE.decode(req.key).change_context(
23+
errors::ApplicationErrorResponse::InternalServerError("Failed to decode the base64 key"),
24+
)?;
25+
let key = <[u8; 32]>::try_from(key).map_err(|_| {
26+
error_stack::report!(errors::ApplicationErrorResponse::InternalServerError(
27+
"Invalid key found"
28+
))
29+
})?;
2730
let key = Key {
2831
version: Version::default(),
2932
identifier: req.identifier.clone(),

src/errors/app.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use super::SwitchError;
22
use axum::response::{IntoResponse, Response};
33
use hyper::StatusCode;
44

5-
use error_stack::ResultExt;
6-
75
pub type ApiResponseResult<T> = Result<T, ApiErrorContainer>;
86

97
pub struct ApiErrorContainer {
@@ -44,13 +42,15 @@ struct ApiErrorResponse<'a> {
4442

4543
#[derive(Debug, thiserror::Error)]
4644
pub enum ApplicationErrorResponse {
47-
#[error("Something Went Wrong")]
48-
InternalServerError,
45+
#[error("Internal Server Error Occurred - {0}")]
46+
InternalServerError(&'static str),
4947
#[error("The resource was not found in the {0}")]
5048
NotFound(&'static str),
5149
#[error("Invalid request provided {0}")]
5250
ParsingFailed(String),
53-
#[error("Unique violation occured. Please try to create the data with another key/identifier")]
51+
#[error(
52+
"Unique violation occurred. Please try to create the data with another key/identifier"
53+
)]
5454
UniqueViolation,
5555
}
5656

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

7070
impl<T> SwitchError<T, ApplicationErrorResponse> for super::CustomResult<T, super::CryptoError> {
7171
fn switch(self) -> super::CustomResult<T, ApplicationErrorResponse> {
72-
self.change_context(ApplicationErrorResponse::InternalServerError)
72+
self.map_err(|err| {
73+
let new_err = match err.current_context() {
74+
super::CryptoError::EncryptionFailed(_) => {
75+
ApplicationErrorResponse::InternalServerError("Encryption failed")
76+
}
77+
super::CryptoError::DecryptionFailed(_) => {
78+
ApplicationErrorResponse::InternalServerError("Decryption failed")
79+
}
80+
super::CryptoError::KeyGeneration => {
81+
ApplicationErrorResponse::InternalServerError("Key generation failed")
82+
}
83+
super::CryptoError::InvalidKey => {
84+
ApplicationErrorResponse::InternalServerError("Invalid key detected")
85+
}
86+
super::CryptoError::KeyGetFailed => {
87+
ApplicationErrorResponse::InternalServerError("Failed to get the key")
88+
}
89+
_ => ApplicationErrorResponse::InternalServerError("Unexpected error occurred"),
90+
};
91+
err.change_context(new_err)
92+
})
7393
}
7494
}
7595

@@ -81,7 +101,9 @@ impl<T> SwitchError<T, ApplicationErrorResponse> for super::CustomResult<T, supe
81101
super::DatabaseError::ConnectionError(_)
82102
| super::DatabaseError::NotNullViolation
83103
| super::DatabaseError::InvalidValue
84-
| super::DatabaseError::Others => ApplicationErrorResponse::InternalServerError,
104+
| super::DatabaseError::Others => {
105+
ApplicationErrorResponse::InternalServerError("Database error occurred")
106+
}
85107
super::DatabaseError::UniqueViolation => ApplicationErrorResponse::UniqueViolation,
86108
};
87109
err.change_context(new_err)
@@ -98,7 +120,7 @@ impl<T> ToContainerError<T> for super::CustomResult<T, ApplicationErrorResponse>
98120
impl IntoResponse for ApiErrorContainer {
99121
fn into_response(self) -> Response {
100122
match self.error.current_context() {
101-
err @ ApplicationErrorResponse::InternalServerError => (
123+
err @ ApplicationErrorResponse::InternalServerError(_) => (
102124
StatusCode::INTERNAL_SERVER_ERROR,
103125
axum::Json(ApiErrorResponse {
104126
error_message: err.to_string(),

0 commit comments

Comments
 (0)