/home/runner/work/creditcoin/creditcoin/pallets/creditcoin/src/ocw/errors.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use core::str::Utf8Error; |
2 | | |
3 | | use super::rpc::errors::RpcError; |
4 | | use alloc::string::FromUtf8Error; |
5 | | use codec::{Decode, Encode, MaxEncodedLen}; |
6 | | use scale_info::TypeInfo; |
7 | | use sp_runtime::offchain::storage::StorageRetrievalError; |
8 | | |
9 | 2 | #[derive(Debug)] |
10 | | pub enum OffchainError { |
11 | | InvalidTask(VerificationFailureCause), |
12 | | NoRpcUrl(RpcUrlError), |
13 | | RpcError(RpcError), |
14 | | IncorrectChainId, |
15 | | InvalidData, |
16 | | } |
17 | | |
18 | | pub type VerificationResult<T> = Result<T, OffchainError>; |
19 | | |
20 | 31 | #[derive(D5 ebug5 , Clone5 , Copy, PartialEq, Eq, E9 ncode9 , D9 ecode, TypeInfo0 , MaxEncodedLen0 )] |
21 | 0 | pub enum VerificationFailureCause { |
22 | | TaskNonexistent, |
23 | | TaskFailed, |
24 | | TaskPending, |
25 | | TaskUnconfirmed, |
26 | | TaskInFuture, |
27 | | IncorrectContract, |
28 | | MissingReceiver, |
29 | | MissingSender, |
30 | | AbiMismatch, |
31 | | IncorrectInputLength, |
32 | | EmptyInput, |
33 | | IncorrectInputType, |
34 | | IncorrectAmount, |
35 | | IncorrectNonce, |
36 | | IncorrectReceiver, |
37 | | IncorrectSender, |
38 | | InvalidAddress, |
39 | | UnsupportedMethod, |
40 | | TransactionNotFound, |
41 | | } |
42 | | |
43 | | impl VerificationFailureCause { |
44 | 5 | pub fn is_fatal(self) -> bool { |
45 | 5 | use VerificationFailureCause::*; |
46 | 5 | match self { |
47 | | TaskFailed | IncorrectContract | MissingSender | MissingReceiver | AbiMismatch |
48 | | | IncorrectInputLength | IncorrectInputType | IncorrectAmount | IncorrectNonce |
49 | | | InvalidAddress | UnsupportedMethod | TaskInFuture | IncorrectSender | EmptyInput |
50 | 3 | | IncorrectReceiver | TaskNonexistent | TransactionNotFound => true, |
51 | 2 | TaskPending | TaskUnconfirmed => false, |
52 | | } |
53 | 5 | } |
54 | | } |
55 | | |
56 | 1 | #[derive(Debug, PartialEq, Eq)] |
57 | | pub enum RpcUrlError { |
58 | | StorageFailure(StorageRetrievalError), |
59 | | InvalidUrl(FromUtf8Error), |
60 | | InvalidChain(Utf8Error), |
61 | | NoValue, |
62 | | } |
63 | | |
64 | | macro_rules! _impl_from_error { |
65 | | ($self_ty: ident, $err_ty: path, $variant: ident) => { |
66 | | impl From<$err_ty> for $self_ty { |
67 | 42 | fn from(err: $err_ty) -> Self { |
68 | 42 | $self_ty::$variant(err) |
69 | 42 | } |
70 | | } |
71 | | }; |
72 | | ($self_ty: ident, $($err_ty: path => $variant: ident),+ $(,)?) => { |
73 | | $( |
74 | | impl_from_error!($self_ty, $err_ty, $variant); |
75 | | )+ |
76 | | }; |
77 | | } |
78 | | |
79 | | pub(crate) use _impl_from_error as impl_from_error; |
80 | | |
81 | | impl_from_error!( |
82 | | OffchainError, |
83 | | RpcUrlError => NoRpcUrl, |
84 | | RpcError => RpcError, |
85 | | VerificationFailureCause => InvalidTask, |
86 | | ); |
87 | | impl_from_error!( |
88 | | RpcUrlError, |
89 | | StorageRetrievalError => StorageFailure, |
90 | | FromUtf8Error => InvalidUrl, |
91 | | ); |