Skip to main content

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(transparent)]
46    CompressedParents(#[from] kaspa_consensus_core::errors::header::CompressedParentsError),
47
48    #[error("Error converting property `{0}`: {1}")]
49    Convert(&'static str, String),
50
51    #[error("Error processing JSON: {0}")]
52    SerdeJson(String),
53
54    #[error("Transaction input is missing UTXO entry")]
55    MissingUtxoEntry,
56
57    #[error(transparent)]
58    PopulateGenesisCovenants(#[from] kaspa_consensus_core::errors::tx::PopulateGenesisCovenantsError),
59}
60
61impl Error {
62    pub fn custom<T: Into<String>>(msg: T) -> Self {
63        Error::Custom(msg.into())
64    }
65
66    pub fn convert<S: std::fmt::Display>(prop: &'static str, msg: S) -> Self {
67        Self::Convert(prop, msg.to_string())
68    }
69}
70
71impl From<String> for Error {
72    fn from(err: String) -> Self {
73        Self::Custom(err)
74    }
75}
76
77impl From<&str> for Error {
78    fn from(err: &str) -> Self {
79        Self::Custom(err.to_string())
80    }
81}
82
83impl From<Error> for JsValue {
84    fn from(value: Error) -> Self {
85        match value {
86            Error::JsValue(js_error_data) => js_error_data.into(),
87            _ => JsValue::from(value.to_string()),
88        }
89    }
90}
91
92impl From<JsValue> for Error {
93    fn from(err: JsValue) -> Self {
94        Self::JsValue(err.into())
95    }
96}
97
98impl From<JsError> for Error {
99    fn from(err: JsError) -> Self {
100        Self::JsValue(err.into())
101    }
102}
103
104impl From<serde_json::Error> for Error {
105    fn from(err: serde_json::Error) -> Self {
106        Self::SerdeJson(err.to_string())
107    }
108}
109
110impl From<serde_wasm_bindgen::Error> for Error {
111    fn from(err: serde_wasm_bindgen::Error) -> Self {
112        Self::SerdeWasmBindgen(JsValue::from(err).into())
113    }
114}