1mod dispatch_error;
8
9use core::fmt::Debug;
10
11pub use dispatch_error::{
13 ArithmeticError, DispatchError, ModuleError, RawModuleError, TokenError, TransactionalError,
14};
15
16pub use crate::metadata::Metadata;
18pub use scale_decode::Error as DecodeError;
19pub use scale_encode::Error as EncodeError;
20pub use subxt_metadata::TryFromError as MetadataTryFromError;
21
22#[derive(Debug, thiserror::Error)]
26#[non_exhaustive]
27pub enum Error {
28 #[error("Io error: {0}")]
30 Io(#[from] std::io::Error),
31 #[error("Scale codec error: {0}")]
33 Codec(#[from] codec::Error),
34 #[error("Rpc error: {0}")]
36 Rpc(#[from] RpcError),
37 #[error("Serde json error: {0}")]
39 Serialization(#[from] serde_json::error::Error),
40 #[error("Metadata: {0}")]
42 Metadata(#[from] MetadataError),
43 #[error("Metadata: {0}")]
45 MetadataDecoding(#[from] MetadataTryFromError),
46 #[error("Runtime error: {0:?}")]
48 Runtime(#[from] DispatchError),
49 #[error("Error decoding into dynamic value: {0}")]
51 Decode(#[from] DecodeError),
52 #[error("Error encoding from dynamic value: {0}")]
54 Encode(#[from] EncodeError),
55 #[error("Transaction error: {0}")]
57 Transaction(#[from] TransactionError),
58 #[error("Block error: {0}")]
60 Block(#[from] BlockError),
61 #[error("Error encoding storage address: {0}")]
63 StorageAddress(#[from] StorageAddressError),
64 #[error("An error occurred but it could not be decoded: {0:?}")]
66 Unknown(Vec<u8>),
67 #[error("Other error: {0}")]
69 Other(String),
70}
71
72impl<'a> From<&'a str> for Error {
73 fn from(error: &'a str) -> Self {
74 Error::Other(error.into())
75 }
76}
77
78impl From<String> for Error {
79 fn from(error: String) -> Self {
80 Error::Other(error)
81 }
82}
83
84#[derive(Debug, thiserror::Error)]
87#[non_exhaustive]
88pub enum RpcError {
89 #[error("RPC error: {0}")]
93 ClientError(Box<dyn std::error::Error + Send + Sync + 'static>),
94 #[error("RPC error: subscription dropped.")]
96 SubscriptionDropped,
97}
98
99#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
101#[non_exhaustive]
102pub enum BlockError {
103 #[error("Could not find a block with hash {0} (perhaps it was on a non-finalized fork?)")]
105 NotFound(String),
106 #[error("Extrinsic type ID cannot be resolved with the provided metadata. Make sure this is a valid metadata")]
108 MissingType,
109 #[error("Unsupported extrinsic version, only version 4 is supported currently")]
111 UnsupportedVersion(u8),
113 #[error("Cannot decode extrinsic: {0}")]
115 DecodingError(codec::Error),
116}
117
118impl BlockError {
119 pub fn not_found(hash: impl AsRef<[u8]>) -> BlockError {
121 let hash = format!("0x{}", hex::encode(hash));
122 BlockError::NotFound(hash)
123 }
124}
125
126#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
128#[non_exhaustive]
129pub enum TransactionError {
130 #[error("The finality subscription expired")]
133 FinalityTimeout,
134 #[error("The block containing the transaction can no longer be found (perhaps it was on a non-finalized fork?)")]
137 BlockNotFound,
138 #[error("The transaction is no longer valid")]
140 Invalid,
141 #[error("The transaction was replaced by a transaction with the same (sender, nonce) pair but with higher priority.")]
143 Usurped,
144 #[error("The transaction was dropped from the pool because of a limit.")]
146 Dropped,
147}
148
149#[derive(Clone, Debug, thiserror::Error)]
151#[non_exhaustive]
152pub enum StorageAddressError {
153 #[error("Storage map type must be a composite type")]
155 MapTypeMustBeTuple,
156 #[error("Storage lookup requires {expected} keys but got {actual} keys")]
158 WrongNumberOfKeys {
159 actual: usize,
161 expected: usize,
163 },
164 #[error("Storage entry in metadata does not have the correct number of hashers to fields")]
166 WrongNumberOfHashers {
167 hashers: usize,
169 fields: usize,
171 },
172}
173
174#[derive(Clone, Debug, PartialEq, thiserror::Error)]
176#[non_exhaustive]
177pub enum MetadataError {
178 #[error("The DispatchError type isn't available")]
180 DispatchErrorNotFound,
181 #[error("Type with ID {0} not found")]
183 TypeNotFound(u32),
184 #[error("Pallet with index {0} not found")]
186 PalletIndexNotFound(u8),
187 #[error("Pallet with name {0} not found")]
189 PalletNameNotFound(String),
190 #[error("Variant with index {0} not found")]
192 VariantIndexNotFound(u8),
193 #[error("Constant with name {0} not found")]
195 ConstantNameNotFound(String),
196 #[error("Call with name {0} not found")]
198 CallNameNotFound(String),
199 #[error("Runtime trait with name {0} not found")]
201 RuntimeTraitNotFound(String),
202 #[error("Runtime method with name {0} not found")]
204 RuntimeMethodNotFound(String),
205 #[error("Call type not found in pallet with index {0}")]
207 CallTypeNotFoundInPallet(u8),
208 #[error("Event type not found in pallet with index {0}")]
210 EventTypeNotFoundInPallet(u8),
211 #[error("Storage details not found in pallet with name {0}")]
213 StorageNotFoundInPallet(String),
214 #[error("Storage entry {0} not found")]
216 StorageEntryNotFound(String),
217 #[error("The generated code is not compatible with the node")]
219 IncompatibleCodegen,
220}
221
222#[doc(hidden)]
224pub trait RootError: Sized {
225 fn root_error(
227 pallet_bytes: &[u8],
229 pallet_name: &str,
230 metadata: &Metadata,
231 ) -> Result<Self, Error>;
232}