kaspa_consensus_client/
error.rs

1//! The [`Error`](enum@Error) enum used by this crate
2
3use thiserror::Error;
4use wasm_bindgen::{JsError, JsValue};
5use workflow_wasm::jserror::JsErrorData;
6
7#[derive(Debug, Error, Clone)]
8pub enum Error {
9    #[error("{0}")]
10    Custom(String),
11
12    #[error(transparent)]
13    JsValue(JsErrorData),
14
15    #[error(transparent)]
16    Wasm(#[from] workflow_wasm::error::Error),
17
18    #[error(transparent)]
19    ScriptBuilder(#[from] kaspa_txscript::script_builder::ScriptBuilderError),
20
21    #[error("{0}")]
22    ParseInt(#[from] std::num::ParseIntError),
23
24    #[error(transparent)]
25    FasterHex(#[from] faster_hex::Error),
26
27    #[error("invalid transaction outpoint: {0}")]
28    InvalidTransactionOutpoint(String),
29
30    #[error(transparent)]
31    Secp256k1(#[from] secp256k1::Error),
32
33    #[error(transparent)]
34    Sign(#[from] kaspa_consensus_core::sign::Error),
35
36    #[error(transparent)]
37    SerdeWasmBindgen(JsErrorData),
38
39    #[error(transparent)]
40    Address(#[from] kaspa_addresses::AddressError),
41
42    #[error(transparent)]
43    NetworkType(#[from] kaspa_consensus_core::network::NetworkTypeError),
44
45    #[error("Error converting property `{0}`: {1}")]
46    Convert(&'static str, String),
47
48    #[error("Error processing JSON: {0}")]
49    SerdeJson(String),
50
51    #[error("Transaction input is missing UTXO entry")]
52    MissingUtxoEntry,
53}
54
55impl Error {
56    pub fn custom<T: Into<String>>(msg: T) -> Self {
57        Error::Custom(msg.into())
58    }
59
60    pub fn convert<S: std::fmt::Display>(prop: &'static str, msg: S) -> Self {
61        Self::Convert(prop, msg.to_string())
62    }
63}
64
65impl From<String> for Error {
66    fn from(err: String) -> Self {
67        Self::Custom(err)
68    }
69}
70
71impl From<&str> for Error {
72    fn from(err: &str) -> Self {
73        Self::Custom(err.to_string())
74    }
75}
76
77impl From<Error> for JsValue {
78    fn from(value: Error) -> Self {
79        match value {
80            Error::JsValue(js_error_data) => js_error_data.into(),
81            _ => JsValue::from(value.to_string()),
82        }
83    }
84}
85
86impl From<JsValue> for Error {
87    fn from(err: JsValue) -> Self {
88        Self::JsValue(err.into())
89    }
90}
91
92impl From<JsError> for Error {
93    fn from(err: JsError) -> Self {
94        Self::JsValue(err.into())
95    }
96}
97
98impl From<serde_json::Error> for Error {
99    fn from(err: serde_json::Error) -> Self {
100        Self::SerdeJson(err.to_string())
101    }
102}
103
104impl From<serde_wasm_bindgen::Error> for Error {
105    fn from(err: serde_wasm_bindgen::Error) -> Self {
106        Self::SerdeWasmBindgen(JsValue::from(err).into())
107    }
108}