Skip to content

Commit b25f071

Browse files
fix(errors): Entry not found error as 4xx instead of 5xx (#7712)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
1 parent 11f7804 commit b25f071

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

crates/storage_impl/src/errors.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ pub enum StorageError {
4141

4242
impl From<error_stack::Report<RedisError>> for StorageError {
4343
fn from(err: error_stack::Report<RedisError>) -> Self {
44-
Self::RedisError(err)
44+
match err.current_context() {
45+
RedisError::NotFound => Self::ValueNotFound("redis value not found".to_string()),
46+
RedisError::JsonSerializationFailed => Self::SerializationFailed,
47+
RedisError::JsonDeserializationFailed => Self::DeserializationFailed,
48+
_ => Self::RedisError(err),
49+
}
4550
}
4651
}
4752

@@ -53,7 +58,15 @@ impl From<diesel::result::Error> for StorageError {
5358

5459
impl From<error_stack::Report<DatabaseError>> for StorageError {
5560
fn from(err: error_stack::Report<DatabaseError>) -> Self {
56-
Self::DatabaseError(err)
61+
match err.current_context() {
62+
DatabaseError::DatabaseConnectionError => Self::DatabaseConnectionError,
63+
DatabaseError::NotFound => Self::ValueNotFound(String::from("db value not found")),
64+
DatabaseError::UniqueViolation => Self::DuplicateValue {
65+
entity: "db entity",
66+
key: None,
67+
},
68+
_ => Self::DatabaseError(err),
69+
}
5770
}
5871
}
5972

@@ -72,6 +85,7 @@ impl StorageError {
7285
Self::DatabaseError(err) => {
7386
matches!(err.current_context(), DatabaseError::UniqueViolation,)
7487
}
88+
Self::DuplicateValue { .. } => true,
7589
_ => false,
7690
}
7791
}

0 commit comments

Comments
 (0)