Skip to content

Commit 80218d0

Browse files
refactor(router): make error_type generic in domain_models inorder to avoid conversion of errors in storage_impl (#7537)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
1 parent aedf460 commit 80218d0

38 files changed

+380
-428
lines changed

crates/analytics/src/opensearch.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use common_utils::{
1010
types::TimeRange,
1111
};
1212
use error_stack::ResultExt;
13-
use hyperswitch_domain_models::errors::{StorageError, StorageResult};
1413
use opensearch::{
1514
auth::Credentials,
1615
cert::CertificateValidation,
@@ -24,7 +23,7 @@ use opensearch::{
2423
MsearchParts, OpenSearch, SearchParts,
2524
};
2625
use serde_json::{json, Map, Value};
27-
use storage_impl::errors::ApplicationError;
26+
use storage_impl::errors::{ApplicationError, StorageError, StorageResult};
2827
use time::PrimitiveDateTime;
2928

3029
use super::{health_check::HealthCheck, query::QueryResult, types::QueryExecutionError};
Original file line numberDiff line numberDiff line change
@@ -1,51 +1 @@
11
pub mod api_error_response;
2-
use diesel_models::errors::DatabaseError;
3-
4-
pub type StorageResult<T> = error_stack::Result<T, StorageError>;
5-
6-
#[derive(Debug, thiserror::Error)]
7-
pub enum StorageError {
8-
#[error("Initialization Error")]
9-
InitializationError,
10-
// TODO: deprecate this error type to use a domain error instead
11-
#[error("DatabaseError: {0:?}")]
12-
DatabaseError(error_stack::Report<DatabaseError>),
13-
#[error("ValueNotFound: {0}")]
14-
ValueNotFound(String),
15-
#[error("DuplicateValue: {entity} already exists {key:?}")]
16-
DuplicateValue {
17-
entity: &'static str,
18-
key: Option<String>,
19-
},
20-
#[error("Timed out while trying to connect to the database")]
21-
DatabaseConnectionError,
22-
#[error("KV error")]
23-
KVError,
24-
#[error("Serialization failure")]
25-
SerializationFailed,
26-
#[error("MockDb error")]
27-
MockDbError,
28-
#[error("Kafka error")]
29-
KafkaError,
30-
#[error("Customer with this id is Redacted")]
31-
CustomerRedacted,
32-
#[error("Deserialization failure")]
33-
DeserializationFailed,
34-
#[error("Error while encrypting data")]
35-
EncryptionError,
36-
#[error("Error while decrypting data from database")]
37-
DecryptionError,
38-
// TODO: deprecate this error type to use a domain error instead
39-
#[error("RedisError: {0:?}")]
40-
RedisError(String),
41-
}
42-
43-
impl StorageError {
44-
pub fn is_db_not_found(&self) -> bool {
45-
match self {
46-
Self::DatabaseError(err) => matches!(err.current_context(), DatabaseError::NotFound),
47-
Self::ValueNotFound(_) => true,
48-
_ => false,
49-
}
50-
}
51-
}

crates/hyperswitch_domain_models/src/payment_methods.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use crate::{
2525
type_encryption::OptionalEncryptableJsonType,
2626
};
2727
use crate::{
28-
errors,
2928
mandates::{self, CommonMandateReference},
3029
merchant_key_store::MerchantKeyStore,
3130
type_encryption::{crypto_operation, AsyncLift, CryptoOperation},
@@ -699,6 +698,7 @@ impl super::behaviour::Conversion for PaymentMethodSession {
699698

700699
#[async_trait::async_trait]
701700
pub trait PaymentMethodInterface {
701+
type Error;
702702
#[cfg(all(
703703
any(feature = "v1", feature = "v2"),
704704
not(feature = "payment_methods_v2")
@@ -709,7 +709,7 @@ pub trait PaymentMethodInterface {
709709
key_store: &MerchantKeyStore,
710710
payment_method_id: &str,
711711
storage_scheme: MerchantStorageScheme,
712-
) -> CustomResult<PaymentMethod, errors::StorageError>;
712+
) -> CustomResult<PaymentMethod, Self::Error>;
713713

714714
#[cfg(all(feature = "v2", feature = "customer_v2"))]
715715
async fn find_payment_method(
@@ -718,7 +718,7 @@ pub trait PaymentMethodInterface {
718718
key_store: &MerchantKeyStore,
719719
payment_method_id: &id_type::GlobalPaymentMethodId,
720720
storage_scheme: MerchantStorageScheme,
721-
) -> CustomResult<PaymentMethod, errors::StorageError>;
721+
) -> CustomResult<PaymentMethod, Self::Error>;
722722

723723
#[cfg(all(
724724
any(feature = "v1", feature = "v2"),
@@ -730,7 +730,7 @@ pub trait PaymentMethodInterface {
730730
key_store: &MerchantKeyStore,
731731
locker_id: &str,
732732
storage_scheme: MerchantStorageScheme,
733-
) -> CustomResult<PaymentMethod, errors::StorageError>;
733+
) -> CustomResult<PaymentMethod, Self::Error>;
734734

735735
#[cfg(all(
736736
any(feature = "v1", feature = "v2"),
@@ -743,7 +743,7 @@ pub trait PaymentMethodInterface {
743743
customer_id: &id_type::CustomerId,
744744
merchant_id: &id_type::MerchantId,
745745
limit: Option<i64>,
746-
) -> CustomResult<Vec<PaymentMethod>, errors::StorageError>;
746+
) -> CustomResult<Vec<PaymentMethod>, Self::Error>;
747747

748748
// Need to fix this once we start moving to v2 for payment method
749749
#[cfg(all(feature = "v2", feature = "customer_v2"))]
@@ -753,7 +753,7 @@ pub trait PaymentMethodInterface {
753753
key_store: &MerchantKeyStore,
754754
id: &id_type::GlobalCustomerId,
755755
limit: Option<i64>,
756-
) -> CustomResult<Vec<PaymentMethod>, errors::StorageError>;
756+
) -> CustomResult<Vec<PaymentMethod>, Self::Error>;
757757

758758
#[cfg(all(
759759
any(feature = "v1", feature = "v2"),
@@ -769,7 +769,7 @@ pub trait PaymentMethodInterface {
769769
status: common_enums::PaymentMethodStatus,
770770
limit: Option<i64>,
771771
storage_scheme: MerchantStorageScheme,
772-
) -> CustomResult<Vec<PaymentMethod>, errors::StorageError>;
772+
) -> CustomResult<Vec<PaymentMethod>, Self::Error>;
773773

774774
#[cfg(all(feature = "v2", feature = "customer_v2"))]
775775
#[allow(clippy::too_many_arguments)]
@@ -782,7 +782,7 @@ pub trait PaymentMethodInterface {
782782
status: common_enums::PaymentMethodStatus,
783783
limit: Option<i64>,
784784
storage_scheme: MerchantStorageScheme,
785-
) -> CustomResult<Vec<PaymentMethod>, errors::StorageError>;
785+
) -> CustomResult<Vec<PaymentMethod>, Self::Error>;
786786

787787
#[cfg(all(
788788
any(feature = "v1", feature = "v2"),
@@ -793,21 +793,21 @@ pub trait PaymentMethodInterface {
793793
customer_id: &id_type::CustomerId,
794794
merchant_id: &id_type::MerchantId,
795795
status: common_enums::PaymentMethodStatus,
796-
) -> CustomResult<i64, errors::StorageError>;
796+
) -> CustomResult<i64, Self::Error>;
797797

798798
async fn get_payment_method_count_by_merchant_id_status(
799799
&self,
800800
merchant_id: &id_type::MerchantId,
801801
status: common_enums::PaymentMethodStatus,
802-
) -> CustomResult<i64, errors::StorageError>;
802+
) -> CustomResult<i64, Self::Error>;
803803

804804
async fn insert_payment_method(
805805
&self,
806806
state: &keymanager::KeyManagerState,
807807
key_store: &MerchantKeyStore,
808808
payment_method: PaymentMethod,
809809
storage_scheme: MerchantStorageScheme,
810-
) -> CustomResult<PaymentMethod, errors::StorageError>;
810+
) -> CustomResult<PaymentMethod, Self::Error>;
811811

812812
async fn update_payment_method(
813813
&self,
@@ -816,23 +816,23 @@ pub trait PaymentMethodInterface {
816816
payment_method: PaymentMethod,
817817
payment_method_update: PaymentMethodUpdate,
818818
storage_scheme: MerchantStorageScheme,
819-
) -> CustomResult<PaymentMethod, errors::StorageError>;
819+
) -> CustomResult<PaymentMethod, Self::Error>;
820820

821821
#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
822822
async fn delete_payment_method(
823823
&self,
824824
state: &keymanager::KeyManagerState,
825825
key_store: &MerchantKeyStore,
826826
payment_method: PaymentMethod,
827-
) -> CustomResult<PaymentMethod, errors::StorageError>;
827+
) -> CustomResult<PaymentMethod, Self::Error>;
828828

829829
#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
830830
async fn find_payment_method_by_fingerprint_id(
831831
&self,
832832
state: &keymanager::KeyManagerState,
833833
key_store: &MerchantKeyStore,
834834
fingerprint_id: &str,
835-
) -> CustomResult<PaymentMethod, errors::StorageError>;
835+
) -> CustomResult<PaymentMethod, Self::Error>;
836836

837837
#[cfg(all(
838838
any(feature = "v1", feature = "v2"),
@@ -844,7 +844,7 @@ pub trait PaymentMethodInterface {
844844
key_store: &MerchantKeyStore,
845845
merchant_id: &id_type::MerchantId,
846846
payment_method_id: &str,
847-
) -> CustomResult<PaymentMethod, errors::StorageError>;
847+
) -> CustomResult<PaymentMethod, Self::Error>;
848848
}
849849

850850
#[cfg(feature = "v2")]

crates/hyperswitch_domain_models/src/payments/payment_attempt.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ use crate::{
5757

5858
#[async_trait::async_trait]
5959
pub trait PaymentAttemptInterface {
60+
type Error;
6061
#[cfg(feature = "v1")]
6162
async fn insert_payment_attempt(
6263
&self,
6364
payment_attempt: PaymentAttemptNew,
6465
storage_scheme: storage_enums::MerchantStorageScheme,
65-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
66+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
6667

6768
#[cfg(feature = "v2")]
6869
async fn insert_payment_attempt(
@@ -71,15 +72,15 @@ pub trait PaymentAttemptInterface {
7172
merchant_key_store: &MerchantKeyStore,
7273
payment_attempt: PaymentAttempt,
7374
storage_scheme: storage_enums::MerchantStorageScheme,
74-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
75+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
7576

7677
#[cfg(feature = "v1")]
7778
async fn update_payment_attempt_with_attempt_id(
7879
&self,
7980
this: PaymentAttempt,
8081
payment_attempt: PaymentAttemptUpdate,
8182
storage_scheme: storage_enums::MerchantStorageScheme,
82-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
83+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
8384

8485
#[cfg(feature = "v2")]
8586
async fn update_payment_attempt(
@@ -89,7 +90,7 @@ pub trait PaymentAttemptInterface {
8990
this: PaymentAttempt,
9091
payment_attempt: PaymentAttemptUpdate,
9192
storage_scheme: storage_enums::MerchantStorageScheme,
92-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
93+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
9394

9495
#[cfg(feature = "v1")]
9596
async fn find_payment_attempt_by_connector_transaction_id_payment_id_merchant_id(
@@ -98,31 +99,31 @@ pub trait PaymentAttemptInterface {
9899
payment_id: &id_type::PaymentId,
99100
merchant_id: &id_type::MerchantId,
100101
storage_scheme: storage_enums::MerchantStorageScheme,
101-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
102+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
102103

103104
#[cfg(feature = "v1")]
104105
async fn find_payment_attempt_last_successful_attempt_by_payment_id_merchant_id(
105106
&self,
106107
payment_id: &id_type::PaymentId,
107108
merchant_id: &id_type::MerchantId,
108109
storage_scheme: storage_enums::MerchantStorageScheme,
109-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
110+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
110111

111112
#[cfg(feature = "v1")]
112113
async fn find_payment_attempt_last_successful_or_partially_captured_attempt_by_payment_id_merchant_id(
113114
&self,
114115
payment_id: &id_type::PaymentId,
115116
merchant_id: &id_type::MerchantId,
116117
storage_scheme: storage_enums::MerchantStorageScheme,
117-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
118+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
118119

119120
#[cfg(feature = "v1")]
120121
async fn find_payment_attempt_by_merchant_id_connector_txn_id(
121122
&self,
122123
merchant_id: &id_type::MerchantId,
123124
connector_txn_id: &str,
124125
storage_scheme: storage_enums::MerchantStorageScheme,
125-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
126+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
126127

127128
#[cfg(feature = "v2")]
128129
async fn find_payment_attempt_by_profile_id_connector_transaction_id(
@@ -132,7 +133,7 @@ pub trait PaymentAttemptInterface {
132133
profile_id: &id_type::ProfileId,
133134
connector_transaction_id: &str,
134135
_storage_scheme: storage_enums::MerchantStorageScheme,
135-
) -> CustomResult<PaymentAttempt, errors::StorageError>;
136+
) -> CustomResult<PaymentAttempt, Self::Error>;
136137

137138
#[cfg(feature = "v1")]
138139
async fn find_payment_attempt_by_payment_id_merchant_id_attempt_id(
@@ -141,15 +142,15 @@ pub trait PaymentAttemptInterface {
141142
merchant_id: &id_type::MerchantId,
142143
attempt_id: &str,
143144
storage_scheme: storage_enums::MerchantStorageScheme,
144-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
145+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
145146

146147
#[cfg(feature = "v1")]
147148
async fn find_payment_attempt_by_attempt_id_merchant_id(
148149
&self,
149150
attempt_id: &str,
150151
merchant_id: &id_type::MerchantId,
151152
storage_scheme: storage_enums::MerchantStorageScheme,
152-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
153+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
153154

154155
#[cfg(feature = "v2")]
155156
async fn find_payment_attempt_by_id(
@@ -158,7 +159,7 @@ pub trait PaymentAttemptInterface {
158159
merchant_key_store: &MerchantKeyStore,
159160
attempt_id: &id_type::GlobalAttemptId,
160161
storage_scheme: storage_enums::MerchantStorageScheme,
161-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
162+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
162163

163164
#[cfg(feature = "v2")]
164165
async fn find_payment_attempts_by_payment_intent_id(
@@ -167,31 +168,31 @@ pub trait PaymentAttemptInterface {
167168
payment_id: &id_type::GlobalPaymentId,
168169
merchant_key_store: &MerchantKeyStore,
169170
storage_scheme: common_enums::MerchantStorageScheme,
170-
) -> error_stack::Result<Vec<PaymentAttempt>, errors::StorageError>;
171+
) -> error_stack::Result<Vec<PaymentAttempt>, Self::Error>;
171172

172173
#[cfg(feature = "v1")]
173174
async fn find_payment_attempt_by_preprocessing_id_merchant_id(
174175
&self,
175176
preprocessing_id: &str,
176177
merchant_id: &id_type::MerchantId,
177178
storage_scheme: storage_enums::MerchantStorageScheme,
178-
) -> error_stack::Result<PaymentAttempt, errors::StorageError>;
179+
) -> error_stack::Result<PaymentAttempt, Self::Error>;
179180

180181
#[cfg(feature = "v1")]
181182
async fn find_attempts_by_merchant_id_payment_id(
182183
&self,
183184
merchant_id: &id_type::MerchantId,
184185
payment_id: &id_type::PaymentId,
185186
storage_scheme: storage_enums::MerchantStorageScheme,
186-
) -> error_stack::Result<Vec<PaymentAttempt>, errors::StorageError>;
187+
) -> error_stack::Result<Vec<PaymentAttempt>, Self::Error>;
187188

188189
#[cfg(all(feature = "v1", feature = "olap"))]
189190
async fn get_filters_for_payments(
190191
&self,
191192
pi: &[PaymentIntent],
192193
merchant_id: &id_type::MerchantId,
193194
storage_scheme: storage_enums::MerchantStorageScheme,
194-
) -> error_stack::Result<PaymentListFilters, errors::StorageError>;
195+
) -> error_stack::Result<PaymentListFilters, Self::Error>;
195196

196197
#[cfg(all(feature = "v1", feature = "olap"))]
197198
#[allow(clippy::too_many_arguments)]
@@ -207,7 +208,7 @@ pub trait PaymentAttemptInterface {
207208
card_network: Option<Vec<storage_enums::CardNetwork>>,
208209
card_discovery: Option<Vec<storage_enums::CardDiscovery>>,
209210
storage_scheme: storage_enums::MerchantStorageScheme,
210-
) -> error_stack::Result<i64, errors::StorageError>;
211+
) -> error_stack::Result<i64, Self::Error>;
211212

212213
#[cfg(all(feature = "v2", feature = "olap"))]
213214
#[allow(clippy::too_many_arguments)]
@@ -222,7 +223,7 @@ pub trait PaymentAttemptInterface {
222223
merchant_connector_id: Option<id_type::MerchantConnectorAccountId>,
223224
card_network: Option<storage_enums::CardNetwork>,
224225
storage_scheme: storage_enums::MerchantStorageScheme,
225-
) -> error_stack::Result<i64, errors::StorageError>;
226+
) -> error_stack::Result<i64, Self::Error>;
226227
}
227228

228229
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]

0 commit comments

Comments
 (0)