1use crate::{
9 backend::error::ApiError, core::viviswap::ViviswapError, types::error::TypeError, user::error::UserKvStorageError,
10 wallet::error::WalletError,
11};
12use serde::{Serialize, ser::Serializer};
13use std::fmt::Display;
14
15pub type Result<T> = core::result::Result<T, Error>;
17
18#[derive(thiserror::Error, Serialize)]
20#[serde(tag = "type", content = "error", rename_all = "camelCase")]
21pub enum Error {
22 #[error("No config is set. Please use the set_config function.")]
24 MissingConfig,
25
26 #[error("No network is set. Please use the set_network function.")]
28 MissingNetwork,
29
30 #[error("Network with id {0} is unavailable.")]
32 NetworkUnavailable(String),
33
34 #[error("Chain ID cannot be empty for EVM network type.")]
36 EmptyChainIdForEvm,
37
38 #[error("Error while setting the configuration: {0}")]
40 SetConfig(String),
41
42 #[error("User is not initialized")]
44 UserNotInitialized,
45
46 #[error("User repositry is not initialized")]
48 UserRepoNotInitialized,
49
50 #[error("Decimal error: {0}")]
52 #[serde(serialize_with = "display_string")]
53 Decimal(rust_decimal::Error),
54
55 #[error("User repository error: {0}")]
57 #[serde(serialize_with = "display_string")]
58 UserRepository(#[from] UserKvStorageError),
59
60 #[error("User is already kyc verified")]
62 UserAlreadyKycVerified,
63
64 #[error("Unauthorized: Missing Access Token")]
66 MissingAccessToken,
67
68 #[error("Chain id is not defined")]
70 ChainIdNotDefined,
71
72 #[error("ParseChainIdError: {0}")]
74 ParseChainIdError(String),
75
76 #[error("Parse error: {0}")]
78 Parse(String),
79
80 #[error("CryptoAmount: {0}")]
82 #[serde(serialize_with = "debug_string")]
83 CryptoAmount(#[from] etopay_wallet::types::CryptoAmountError),
84
85 #[cfg(not(target_arch = "wasm32"))]
87 #[serde(serialize_with = "display_string")]
88 #[error("LoggerInit error: {0}")]
89 LoggerInit(fern_logger::Error),
90
91 #[error("BackendApi error: {0}")]
93 #[serde(serialize_with = "debug_string")]
94 BackendApi(#[from] ApiError),
95
96 #[error("Type error: {0}")]
98 #[serde(serialize_with = "debug_string")]
99 Type(#[from] TypeError),
100
101 #[error("Wallet error: {0}")]
103 #[serde(serialize_with = "debug_string")]
104 Wallet(#[from] WalletError),
105
106 #[error("WalletImplError: {0}")]
108 #[serde(serialize_with = "debug_string")]
109 WalletImplError(#[from] etopay_wallet::WalletError),
110
111 #[error("Viviswap error: {0}")]
113 #[serde(serialize_with = "debug_string")]
114 Viviswap(ViviswapError),
115}
116
117impl From<rust_decimal::Error> for Error {
118 fn from(value: rust_decimal::Error) -> Self {
119 Self::Decimal(value)
120 }
121}
122
123impl std::fmt::Debug for Error {
124 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125 error_chain_fmt(self, f)
126 }
127}
128
129fn error_chain_fmt(e: &impl std::error::Error, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131 writeln!(f, "{}\n", e)?;
132 let mut current = e.source();
133 while let Some(cause) = current {
134 writeln!(f, "Caused by:\n\t{}", cause)?;
135 current = cause.source();
136 }
137 Ok(())
138}
139
140fn display_string<T, S>(value: &T, serializer: S) -> std::result::Result<S::Ok, S::Error>
142where
143 T: Display,
144 S: Serializer,
145{
146 value.to_string().serialize(serializer)
147}
148
149fn debug_string<T, S>(value: &T, serializer: S) -> std::result::Result<S::Ok, S::Error>
150where
151 T: std::fmt::Debug,
152 S: Serializer,
153{
154 format!("{:?}", value).serialize(serializer)
155}