Skip to main content

avail_rust_client/
error.rs

1/// Errors that originate from user input or validation problems.
2#[derive(thiserror::Error, Debug)]
3pub enum UserError {
4	/// Indicates SCALE or JSON decoding failed.
5	#[error("{0}")]
6	Decoding(String),
7	/// Indicates validation rules were violated.
8	#[error("{0}")]
9	ValidationFailed(String),
10	/// Catch-all for other user-facing errors.
11	#[error("{0}")]
12	Other(String),
13}
14
15/// Errors raised by the Avail client.
16#[derive(thiserror::Error, Debug)]
17pub enum Error {
18	/// Wraps lower-level RPC errors propagated from the transport layer.
19	#[error("{0}")]
20	RpcError(avail_rust_core::rpc::Error),
21	/// Wraps `UserError` variants.
22	#[error("{0}")]
23	User(UserError),
24	/// Catch-all for other error conditions.
25	#[error("{0}")]
26	Other(String),
27}
28
29impl From<avail_rust_core::rpc::Error> for Error {
30	/// Converts a core RPC error into the client error type.
31	fn from(value: avail_rust_core::rpc::Error) -> Self {
32		Self::RpcError(value)
33	}
34}
35
36impl From<UserError> for Error {
37	/// Wraps a `UserError` into the unified error type.
38	fn from(value: UserError) -> Self {
39		Self::User(value)
40	}
41}
42
43impl From<&str> for Error {
44	/// Converts a string slice into a generic error variant.
45	fn from(value: &str) -> Self {
46		Self::Other(value.to_owned())
47	}
48}
49
50impl From<codec::Error> for Error {
51	/// Converts SCALE codec errors into the client error type.
52	fn from(value: codec::Error) -> Self {
53		Self::Other(value.to_string())
54	}
55}