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("NegativeAmount")]
82 NegativeAmount,
83
84 #[cfg(not(target_arch = "wasm32"))]
86 #[serde(serialize_with = "display_string")]
87 #[error("LoggerInit error: {0}")]
88 LoggerInit(fern_logger::Error),
89
90 #[error("BackendApi error: {0}")]
92 #[serde(serialize_with = "debug_string")]
93 BackendApi(#[from] ApiError),
94
95 #[error("Type error: {0}")]
97 #[serde(serialize_with = "debug_string")]
98 Type(#[from] TypeError),
99
100 #[error("Wallet error: {0}")]
102 #[serde(serialize_with = "debug_string")]
103 Wallet(#[from] WalletError),
104
105 #[error("Viviswap error: {0}")]
107 #[serde(serialize_with = "debug_string")]
108 Viviswap(ViviswapError),
109}
110
111impl From<rust_decimal::Error> for Error {
112 fn from(value: rust_decimal::Error) -> Self {
113 Self::Decimal(value)
114 }
115}
116
117impl std::fmt::Debug for Error {
118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 error_chain_fmt(self, f)
120 }
121}
122
123fn error_chain_fmt(e: &impl std::error::Error, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125 writeln!(f, "{}\n", e)?;
126 let mut current = e.source();
127 while let Some(cause) = current {
128 writeln!(f, "Caused by:\n\t{}", cause)?;
129 current = cause.source();
130 }
131 Ok(())
132}
133
134fn display_string<T, S>(value: &T, serializer: S) -> std::result::Result<S::Ok, S::Error>
136where
137 T: Display,
138 S: Serializer,
139{
140 value.to_string().serialize(serializer)
141}
142
143fn debug_string<T, S>(value: &T, serializer: S) -> std::result::Result<S::Ok, S::Error>
144where
145 T: std::fmt::Debug,
146 S: Serializer,
147{
148 format!("{:?}", value).serialize(serializer)
149}