race_api/
error.rs

1use borsh::{BorshDeserialize, BorshSerialize};
2use thiserror::Error;
3
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7use crate::types::{DecisionId, RandomId};
8
9#[derive(Error, Debug, BorshDeserialize, BorshSerialize, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub enum Error {
12    #[error("Player already joined: {0}")]
13    PlayerAlreadyJoined(String),
14
15    #[error("Position Occupied: {0}")]
16    PositionOccupied(usize),
17
18    #[error("Game is full: {0}")]
19    GameIsFull(u32),
20
21    #[error("Server queue is full: {0}")]
22    ServerQueueIsFull(u32),
23
24    #[error("No enough players")]
25    NoEnoughPlayers,
26
27    #[error("Server already joined: {0}")]
28    ServerAlreadyJoined(String),
29
30    #[error("Player not in game")]
31    PlayerNotInGame,
32
33    #[error("Malformed game event")]
34    MalformedData(String),
35
36    #[error("Malformed address")]
37    MalformedAddress,
38
39    #[error("Invalid randomness assignment")]
40    InvalidRandomnessAssignment,
41
42    #[error("Invalid randomness revealing")]
43    InvalidRandomnessRevealing,
44
45    #[error("Invalid random id")]
46    InvalidRandomId,
47
48    #[error("Custom error")]
49    Custom(String),
50
51    #[error("Server account not found")]
52    ServerAccountNotFound,
53
54    #[error("Player profile not found")]
55    PlayerProfileNotFound,
56
57    #[error("Game account not found")]
58    GameAccountNotFound,
59
60    #[error("Game bundle not found")]
61    GameBundleNotFound,
62
63    #[error("Server account exists")]
64    ServerAccountExists,
65
66    #[error("Registration not found")]
67    RegistrationNotFound,
68
69    #[error("Rpc error: {0}")]
70    RpcError(String),
71
72    #[error("Invalid chain name")]
73    InvalidChainName,
74
75    #[error("Invalid player address")]
76    InvalidPlayerAddress,
77
78    #[error("Invalid player status")]
79    InvalidPlayerStatus,
80
81    #[error("Game not loaded")]
82    GameNotLoaded,
83
84    #[error("Malformed endpoint")]
85    MalformedEndpoint,
86
87    #[error("Malformed game bundle")]
88    MalformedGameBundle,
89
90    #[error("Malformed game account")]
91    MalformedGameAccount,
92
93    #[error("Deserialize error")]
94    DeserializeError,
95
96    #[error("Config missing")]
97    ConfigMissing,
98
99    #[error("Transactor config missing")]
100    TransactorConfigMissing,
101
102    #[error("Can't leave")]
103    CantLeave,
104
105    #[error("Randomization error: {0}")]
106    RandomizationError(String),
107
108    #[error("Crypto error: {0}")]
109    CryptoError(String),
110
111    #[error("Duplicated event dispatching")]
112    DuplicatedEventDispatching,
113
114    #[error("Invalid amount")]
115    InvalidAmount,
116
117    #[error("Not allowed in custom handler")]
118    NotAllowedInCustomHandler,
119
120    #[error("Game not served")]
121    GameNotServed,
122
123    #[error("Game is not empty")]
124    GameIsNotEmpty,
125
126    #[error("Can't find transactor")]
127    CantFindTransactor,
128
129    #[error("Invalid transactor address")]
130    InvalidTransactorAddress,
131
132    #[error("Failed to load on-chain server account, please register first")]
133    ServerAccountMissing,
134
135    #[error("Initialization transport failed: {0}")]
136    TransportError(String),
137
138    #[error("Initializing instruction failed: {0}")]
139    InitInstructionFailed(String),
140
141    #[error("Initialize rpc client error: {0}")]
142    InitializeRpcClientError(String),
143
144    #[error("Internal error: {0}")]
145    InternalError(String),
146
147    #[error("Missing secret")]
148    MissingSecret,
149
150    #[error("Invalid secret")]
151    InvalidSecret,
152
153    #[error("Invalid decrypted value: {0}")]
154    InvalidDecryptedValue(String),
155
156    #[error("Decryption failed")]
157    DecryptionFailed,
158
159    #[error("Invalid key index")]
160    InvalidKeyIndex,
161
162    #[error("Invalid ciphertexts size, expect: {0}, got: {1}")]
163    InvalidCiphertextsSize(u32, u32),
164
165    #[error("Invalid max players")]
166    InvalidMaxPlayers,
167
168    #[error("JSON parse error")]
169    JsonParseError,
170
171    #[error("Signature verification failed")]
172    SignatureVerificationFailed,
173
174    #[error("Invalid Settle: {0}")]
175    InvalidSettle(String),
176
177    #[error("IO Error: {0}")]
178    IoError(String),
179
180    #[error("Not supported in validator mode")]
181    NotSupportedInValidatorMode,
182
183    #[error("Invalid voter: {0}")]
184    InvalidVoter(String),
185
186    #[error("Invalid votee: {0}")]
187    InvalidVotee(String),
188
189    #[error("Duplicated vote")]
190    DuplicatedVote,
191
192    #[error("Transaction expired")]
193    TransactionExpired,
194
195    #[error("Event ignored")]
196    EventIgnored,
197
198    #[error("Wallet not connected")]
199    WalletNotConnected,
200
201    #[error("Invalid custom event")]
202    InvalidCustomEvent,
203
204    #[error("Invalid decision id")]
205    InvalidDecisionId,
206
207    #[error("Answer not available")]
208    AnswerNotAvailable,
209
210    #[error("Missing decision secret: {0}")]
211    MissingDecisionSecret(DecisionId),
212
213    #[error("Invalid decision answer")]
214    InvalidDecisionAnswer,
215
216    #[error("Invalid decision owner")]
217    InvalidDecisionOwner,
218
219    #[error("Invalid decision status")]
220    InvalidDecisionStatus,
221
222    #[error("Duplicated secret share")]
223    DuplicatedSecretShare,
224
225    #[error("Serialization error")]
226    SerializationError,
227
228    #[error("Wasm initialization error: {0}")]
229    WasmInitializationError(String),
230
231    #[error("Wasm execution error: {0}")]
232    WasmExecutionError(String),
233
234    #[error("Wasm memory overflow")]
235    WasmMemoryOverflow,
236
237    #[error("Invalid checkpoint")]
238    InvalidCheckpoint,
239
240    #[error("Duplicated initialization")]
241    DuplicatedInitialization,
242
243    #[error("Randomness is not revealed")]
244    RandomnessNotRevealed,
245
246    #[error("Random state not found: {0}")]
247    RandomStateNotFound(RandomId),
248
249    #[error("Wasm execution error: {0}")]
250    HandleError(HandleError),
251
252    #[error("Invalid recipient slot params")]
253    InvalidRecipientSlotParams,
254
255    #[error("Cannot settle or transfer without checkpoint")]
256    SettleWithoutCheckpoint,
257}
258
259#[cfg(feature = "serde")]
260impl From<serde_json::Error> for Error {
261    fn from(e: serde_json::Error) -> Self {
262        Error::MalformedData(e.to_string())
263    }
264}
265
266impl From<std::io::Error> for Error {
267    fn from(e: std::io::Error) -> Self {
268        Error::IoError(e.to_string())
269    }
270}
271
272pub type Result<T> = std::result::Result<T, Error>;
273
274#[derive(Error, Debug, BorshDeserialize, BorshSerialize, Clone, PartialEq, Eq)]
275#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
276pub enum HandleError {
277    #[error("Custom error: {0}")]
278    Custom(String),
279
280    #[error("No enough players")]
281    NoEnoughPlayers,
282
283    #[error("Invalid player")]
284    InvalidPlayer,
285
286    #[error("Can't leave game")]
287    CantLeave,
288
289    #[error("Invalid amount")]
290    InvalidAmount,
291
292    #[error("Malformed game account data")]
293    MalformedGameAccountData,
294
295    #[error("Malformed checkpoint data")]
296    MalformedCheckpointData,
297
298    #[error("Malformed custom event")]
299    MalformedCustomEvent,
300
301    #[error("Serialization error")]
302    SerializationError,
303
304    #[error("No enough servers")]
305    NoEnoughServers,
306
307    #[error("Internal error: {message:?}")]
308    InternalError { message: String },
309}
310
311impl From<crate::error::Error> for HandleError {
312    fn from(value: crate::error::Error) -> Self {
313        HandleError::InternalError {
314            message: value.to_string(),
315        }
316    }
317}
318
319impl From<HandleError> for Error {
320    fn from(value: HandleError) -> Self {
321        Error::HandleError(value)
322    }
323}
324
325pub type HandleResult<T> = std::result::Result<T, HandleError>;