1use crate::ContractError::{CustomError, DecodeError, DeserializationError, SerializationError};
2use cosmwasm_std::{DivideByZeroError, OverflowError, StdError};
3use std::num::ParseIntError;
4use std::str::ParseBoolError;
5use thiserror::Error;
6
7#[derive(Error, Debug, PartialEq)]
8pub enum ContractError {
9 #[error("{0}")]
10 Std(#[from] StdError),
11
12 #[error("Unauthorized")]
13 Unauthorized {},
14
15 #[error("Funds array in message does not match funds array in job.")]
16 FundsMismatch {},
17
18 #[error("Reward provided is smaller than minimum")]
19 RewardTooSmall {},
20
21 #[error("Name must be at least 1 character long")]
22 NameTooShort {},
23
24 #[error("Name cannot exceed 280 characters")]
25 NameTooLong {},
26
27 #[error("Attempting to distribute more rewards than received from the action")]
28 DistributingMoreRewardThanReceived {},
29
30 #[error("Invalid arguments")]
31 InvalidArguments {},
32
33 #[error("Account does not exist")]
34 AccountDoesNotExist {},
35
36 #[error("Account already exists")]
37 AccountAlreadyExists {},
38
39 #[error("Account cannot create an account")]
40 AccountCannotCreateAccount {},
41
42 #[error("Job already finished")]
43 JobAlreadyFinished {},
44
45 #[error("Job already exists")]
46 JobAlreadyExists {},
47
48 #[error("Job does not exist")]
49 JobDoesNotExist {},
50
51 #[error("Job not active")]
52 JobNotActive {},
53
54 #[error("Cancellation fee too high")]
55 CancellationFeeTooHigh {},
56
57 #[error("Creation fee too high")]
58 CreationFeeTooHigh {},
59
60 #[error("Custom Error val: {val:?}")]
61 CustomError { val: String },
62 #[error("Error deserializing data")]
65 DeserializationError {},
66
67 #[error("Error serializing data")]
68 SerializationError {},
69
70 #[error("Error decoding JSON result")]
71 DecodeError {},
72
73 #[error("Max eviction fee smaller than minimum eviction fee.")]
74 MaxFeeUnderMinFee {},
75
76 #[error("Max eviction time smaller than minimum eviction time.")]
77 MaxTimeUnderMinTime {},
78
79 #[error("Job reward smaller than eviction fee.")]
80 RewardSmallerThanFee {},
81
82 #[error("Eviction period not elapsed.")]
83 EvictionPeriodNotElapsed {},
84}
85
86impl From<serde_json_wasm::de::Error> for ContractError {
87 fn from(_: serde_json_wasm::de::Error) -> Self {
88 DeserializationError {}
89 }
90}
91
92impl From<serde_json_wasm::ser::Error> for ContractError {
93 fn from(_: serde_json_wasm::ser::Error) -> Self {
94 SerializationError {}
95 }
96}
97
98impl From<json_codec_wasm::DecodeError> for ContractError {
99 fn from(_: json_codec_wasm::DecodeError) -> Self {
100 DecodeError {}
101 }
102}
103
104impl From<base64::DecodeError> for ContractError {
105 fn from(_: base64::DecodeError) -> Self {
106 DecodeError {}
107 }
108}
109
110impl From<String> for ContractError {
111 fn from(val: String) -> Self {
112 CustomError { val }
113 }
114}
115
116impl From<ParseIntError> for ContractError {
117 fn from(_: ParseIntError) -> Self {
118 CustomError {
119 val: "Parse int error".to_string(),
120 }
121 }
122}
123
124impl From<ParseBoolError> for ContractError {
125 fn from(_: ParseBoolError) -> Self {
126 CustomError {
127 val: "Parse bool error".to_string(),
128 }
129 }
130}
131
132impl From<DivideByZeroError> for ContractError {
133 fn from(_: DivideByZeroError) -> Self {
134 CustomError {
135 val: "ERROR: Division by zero".to_string(),
136 }
137 }
138}
139
140impl From<OverflowError> for ContractError {
141 fn from(_: OverflowError) -> Self {
142 CustomError {
143 val: "ERROR: Overflow error".to_string(),
144 }
145 }
146}
147
148pub fn map_contract_error(e: &str) -> String {
149 if e.contains("wasm") {
150 if e.contains("code: 28") {
151 "No such code ID."
152 } else if e.contains("code: 27") {
153 "Max query stack size exceeded."
154 } else if e.contains("code: 22") {
155 "No such contract at requested address."
156 } else if e.contains("code: 21") {
157 "Invalid event from contract."
158 } else if e.contains("code: 20") {
159 "Unknown message from the contract."
160 } else if e.contains("code: 19") {
161 "Unpinning contract failed."
162 } else if e.contains("code: 18") {
163 "Pinning contract failed."
164 } else if e.contains("code: 17") {
165 "Unsupported action for this contract."
166 } else if e.contains("code: 16") {
167 "Maximum IBC channels reached."
168 } else if e.contains("code: 15") {
169 "Content is duplicated."
170 } else if e.contains("code: 14") {
171 "Content is invalid in this context."
172 } else if e.contains("code: 13") {
173 "Content exceeds limit."
174 } else if e.contains("code: 12") {
175 "Empty content."
176 } else if e.contains("code: 11") {
177 "Migrate wasm contract failed."
178 } else if e.contains("code: 10") {
179 "Invalid CosmosMsg from the called contract."
180 } else if e.contains("code: 9") {
181 "Query wasm contract failed."
182 } else if e.contains("code: 8") {
183 "Entry not found in store."
184 } else if e.contains("code: 7") {
185 "Invalid genesis file."
186 } else if e.contains("code: 6") {
187 "Insufficient gas."
188 } else if e.contains("code: 5") {
189 "Execute wasm contract failed. Common causes include insufficient CW20 Funds, permission errors on CW721 assets, and malformed contract messages."
190 } else if e.contains("code: 4") {
191 "Instantiate wasm contract failed."
192 } else if e.contains("code: 3") {
193 "Contract account already exists."
194 } else if e.contains("code: 2") {
195 "Create wasm contract failed."
196 } else {
197 "Undefined error."
198 }
199 } else if e.contains("sdk") {
200 if e.contains("code: 41") {
201 "Invalid gas limit."
202 } else if e.contains("code: 40") {
203 "Error in app.toml."
204 } else if e.contains("code: 39") {
205 "Internal IO error."
206 } else if e.contains("code: 38") {
207 "Not found: Entity does not exist in state."
208 } else if e.contains("code: 37") {
209 "Feature not supported."
210 } else if e.contains("code: 36") {
211 "Conflict error."
212 } else if e.contains("code: 35") {
213 "Internal logic error."
214 } else if e.contains("code: 34") {
215 "Failed unpacking protobuf msg."
216 } else if e.contains("code: 33") {
217 "Failed packing protobuf msg."
218 } else if e.contains("code: 32") {
219 "Incorrect account sequence."
220 } else if e.contains("code: 31") {
221 "Unknown extension options."
222 } else if e.contains("code: 30") {
223 "Tx timeout height."
224 } else if e.contains("code: 29") {
225 "Invalid type."
226 } else if e.contains("code: 28") {
227 "Invalid chain-id."
228 } else if e.contains("code: 27") {
229 "Invalid version."
230 } else if e.contains("code: 26") {
231 "invalid height."
232 } else if e.contains("code: 25") {
233 "Invalid gas adjustment."
234 } else if e.contains("code: 24") {
235 "Tx indended signer does not match the given signer."
236 } else if e.contains("code: 23") {
237 "Invalid account password."
238 } else if e.contains("code: 22") {
239 "Key not found."
240 } else if e.contains("code: 21") {
241 "Tx too large."
242 } else if e.contains("code: 20") {
243 "Mempool is full."
244 } else if e.contains("code: 19") {
245 "Tx already in mempool."
246 } else if e.contains("code: 18") {
247 "Invalid request."
248 } else if e.contains("code: 17") {
249 "Failed to unmarshal JSON bytes."
250 } else if e.contains("code: 16") {
251 "Failed to marshal JSON bytes."
252 } else if e.contains("code: 15") {
253 "No signatures supplied."
254 } else if e.contains("code: 14") {
255 "Maximum number of signatures exceeded."
256 } else if e.contains("code: 13") {
257 "Insufficient fee."
258 } else if e.contains("code: 12") {
259 "Memo too large."
260 } else if e.contains("code: 11") {
261 "Out of gas."
262 } else if e.contains("code: 10") {
263 "Invalid coins."
264 } else if e.contains("code: 9") {
265 "Unknown address."
266 } else if e.contains("code: 8") {
267 "Invalid pubkey."
268 } else if e.contains("code: 7") {
269 "Invalid address."
270 } else if e.contains("code: 6") {
271 "Unknown request."
272 } else if e.contains("code: 5") {
273 "Invalid funds. Ensure that sufficient native tokens are being supplied for the job."
274 } else if e.contains("code: 4") {
275 "Unauthorized SDK request."
276 } else if e.contains("code: 3") {
277 "Invalid sequence."
278 } else if e.contains("code: 2") {
279 "Tx parse error."
280 } else {
281 "Undefined error."
282 }
283 } else {
284 "Undefined error."
285 }.to_string()
286}