1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#[cfg(feature = "std")]
use thiserror::Error;

#[cfg(not(feature = "std"))]
use alloc::{fmt, format, string::String};

#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum Error {
  #[cfg_attr(feature = "std", error("Std io error: {0}"))]
  #[cfg(feature = "std")]
  StdIo(std::io::Error),

  #[cfg_attr(feature = "std", error("Json error: {0}"))]
  Json(serde_json::Error),

  #[cfg_attr(feature = "std", error("hex error: {0}"))]
  Hex(hex::FromHexError),

  #[cfg_attr(feature = "std", error("http error: {0}"))]
  Http(http::Error),

  #[cfg_attr(feature = "std", error("http uri error: {0}"))]
  HttpUri(http::uri::InvalidUri),

  #[cfg_attr(feature = "std", error("parity-scale-codec error: {0}"))]
  ParityScaleCodec(codec::Error),

  #[cfg_attr(feature = "std", error("sp-core crypto secret error: {0}"))]
  SecretStringError(String),

  #[cfg_attr(
    feature = "std",
    error("Call API incompatible with connected chain: {0}")
  )]
  IncompatibleCall(String),

  #[cfg_attr(feature = "std", error("Schema failed to parse: {0}"))]
  SchemaParseFailed(String),

  #[cfg_attr(feature = "std", error("Metadata failed to parse: {0}"))]
  MetadataParseFailed(String),

  #[cfg_attr(feature = "std", error("ExtrinsicError: {0}"))]
  ExtrinsicError(String),

  #[cfg_attr(feature = "std", error("RpcClient: {0}"))]
  RpcClient(String),

  #[cfg_attr(feature = "std", error("Decode type failed: {0}"))]
  DecodeTypeFailed(String),

  #[cfg_attr(feature = "std", error("Signing transaction failed: {0}"))]
  SigningTransactionFailed(String),

  #[cfg_attr(feature = "std", error("Jsonrpsee error: {0}"))]
  Jsonrpsee(jsonrpsee::core::Error),

  #[cfg_attr(
    feature = "std",
    error("The signer's account {0} doesn't match the transaction's account: {1}")
  )]
  WrongSignerAccount(String, String),
}

#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
  fn from(e: std::io::Error) -> Self {
    Self::StdIo(e)
  }
}

impl From<serde_json::Error> for Error {
  fn from(e: serde_json::Error) -> Self {
    Self::Json(e)
  }
}

impl From<hex::FromHexError> for Error {
  fn from(e: hex::FromHexError) -> Self {
    Self::Hex(e)
  }
}

impl From<http::Error> for Error {
  fn from(e: http::Error) -> Self {
    Self::Http(e)
  }
}

impl From<http::uri::InvalidUri> for Error {
  fn from(e: http::uri::InvalidUri) -> Self {
    Self::HttpUri(e)
  }
}

impl From<codec::Error> for Error {
  fn from(e: codec::Error) -> Self {
    Self::ParityScaleCodec(e)
  }
}

impl From<jsonrpsee::core::Error> for Error {
  fn from(e: jsonrpsee::core::Error) -> Self {
    Self::Jsonrpsee(e)
  }
}

impl From<sp_core::crypto::SecretStringError> for Error {
  fn from(e: sp_core::crypto::SecretStringError) -> Self {
    Self::SecretStringError(format!("{e:?}"))
  }
}

impl From<subxt_signer::sr25519::Error> for Error {
  fn from(e: subxt_signer::sr25519::Error) -> Self {
    Self::SecretStringError(format!("{e:?}"))
  }
}

impl From<subxt_signer::ecdsa::Error> for Error {
  fn from(e: subxt_signer::ecdsa::Error) -> Self {
    Self::SecretStringError(format!("{e:?}"))
  }
}

impl From<subxt_signer::SecretUriError> for Error {
  fn from(e: subxt_signer::SecretUriError) -> Self {
    Self::SecretStringError(format!("{e:?}"))
  }
}

#[cfg(not(feature = "std"))]
impl fmt::Display for Error {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "{:?}", self)
  }
}

pub type Result<T, E = Error> = core::result::Result<T, E>;