1use crate::script_builder;
2use thiserror::Error;
3use wasm_bindgen::{JsError, JsValue};
4use workflow_wasm::jserror::JsErrorData;
5
6#[derive(Debug, Error, Clone)]
7pub enum Error {
8 #[error("{0}")]
9 Custom(String),
10
11 #[error(transparent)]
12 JsValue(JsErrorData),
13
14 #[error(transparent)]
15 Wasm(#[from] workflow_wasm::error::Error),
16
17 #[error(transparent)]
18 ScriptBuilder(#[from] script_builder::ScriptBuilderError),
19
20 #[error("{0}")]
21 ParseInt(#[from] std::num::ParseIntError),
22
23 #[error(transparent)]
24 SerdeWasmBindgen(JsErrorData),
25
26 #[error(transparent)]
27 NetworkType(#[from] kaspa_consensus_core::network::NetworkTypeError),
28
29 #[error("Error converting property `{0}`: {1}")]
30 Convert(&'static str, String),
31
32 #[error("Error processing JSON: {0}")]
33 SerdeJson(String),
34}
35
36impl Error {
37 pub fn custom<T: Into<String>>(msg: T) -> Self {
38 Error::Custom(msg.into())
39 }
40
41 pub fn convert<S: std::fmt::Display>(prop: &'static str, msg: S) -> Self {
42 Self::Convert(prop, msg.to_string())
43 }
44}
45
46impl From<String> for Error {
47 fn from(err: String) -> Self {
48 Self::Custom(err)
49 }
50}
51
52impl From<&str> for Error {
53 fn from(err: &str) -> Self {
54 Self::Custom(err.to_string())
55 }
56}
57
58impl From<Error> for JsValue {
59 fn from(value: Error) -> Self {
60 match value {
61 Error::JsValue(js_error_data) => js_error_data.into(),
62 _ => JsValue::from(value.to_string()),
63 }
64 }
65}
66
67impl From<JsValue> for Error {
68 fn from(err: JsValue) -> Self {
69 Self::JsValue(err.into())
70 }
71}
72
73impl From<JsError> for Error {
74 fn from(err: JsError) -> Self {
75 Self::JsValue(err.into())
76 }
77}
78
79impl From<serde_json::Error> for Error {
80 fn from(err: serde_json::Error) -> Self {
81 Self::SerdeJson(err.to_string())
82 }
83}
84
85impl From<serde_wasm_bindgen::Error> for Error {
86 fn from(err: serde_wasm_bindgen::Error) -> Self {
87 Self::SerdeWasmBindgen(JsValue::from(err).into())
88 }
89}