|
1 | 1 | use kos::chains::ChainError; |
2 | | -use std::{error, fmt, str}; |
3 | | -use wasm_bindgen::JsValue; |
4 | | - |
5 | | -/// Crypto error variants |
6 | | -#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
7 | | -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
8 | | -pub enum Error { |
9 | | - // Invalid string |
10 | | - InvalidString(String), |
11 | | - // JSON serialization error |
12 | | - JSONSerde(String), |
13 | | - // UnsupportedChain, |
14 | | - UnsupportedChain(String), |
15 | | - // InvalidMnemonic, |
16 | | - InvalidMnemonic(&'static str), |
17 | | - // InvalidPath, |
18 | | - InvalidPath(&'static str), |
19 | | - // InvalidPrivateKey, |
20 | | - InvalidPrivateKey(&'static str), |
21 | | - // InvalidPublicKey, |
22 | | - InvalidPublicKey(String), |
23 | | - // InvalidAddress, |
24 | | - InvalidAddress(String), |
25 | | - // InvalidChecksum, |
26 | | - InvalidChecksum(&'static str), |
27 | | - /// Invalid secp256k1 signature |
28 | | - InvalidSignature(&'static str), |
29 | | - /// Invalid secp256k1 signature message |
30 | | - InvalidMessage(String), |
31 | | - /// Out of preallocated memory |
32 | | - NotEnoughMemory(String), |
33 | | - /// Invalid Enum Variant |
34 | | - InvalidEnumVariant(String), |
35 | | - /// Invalid Len |
36 | | - InvalidLen(String), |
37 | | - /// InvalidNumberParse |
38 | | - InvalidNumberParse(String), |
39 | | - /// InvalidTransaction |
40 | | - InvalidTransaction(String), |
41 | | - /// WalletManagerError |
42 | | - WalletManager(String), |
43 | | - /// CipherError |
44 | | - Cipher(String), |
45 | | - /// TransportError |
46 | | - Transport(String), |
47 | | - /// DelegateError |
48 | | - Delegate(String), |
| 2 | +use std::{error, fmt}; |
| 3 | +use wasm_bindgen::prelude::*; |
| 4 | + |
| 5 | +// Rust-only error type with associated data |
| 6 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | +#[wasm_bindgen] |
| 8 | +pub struct KOSError { |
| 9 | + #[wasm_bindgen(skip)] |
| 10 | + pub message: String, |
49 | 11 | } |
50 | 12 |
|
51 | | -impl fmt::Display for Error { |
52 | | - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
53 | | - match self { |
54 | | - Error::InvalidString(e) => write!(f, "Invalid string: {e}"), |
55 | | - Error::JSONSerde(e) => write!(f, "JSON serialization: {e}"), |
56 | | - Error::UnsupportedChain(e) => write!(f, "Unsupported chain: {e}"), |
57 | | - Error::InvalidMnemonic(e) => write!(f, "Invalid mnemonic: {e}"), |
58 | | - Error::InvalidPath(e) => write!(f, "Invalid path: {e}"), |
59 | | - Error::InvalidPrivateKey(e) => write!(f, "Invalid private key: {e}"), |
60 | | - Error::InvalidPublicKey(e) => write!(f, "Invalid public key: {e}"), |
61 | | - Error::InvalidAddress(e) => write!(f, "Invalid address: {e}"), |
62 | | - Error::InvalidChecksum(e) => write!(f, "Invalid checksum: {e}"), |
63 | | - Error::InvalidSignature(e) => write!(f, "Invalid signature: {e}"), |
64 | | - Error::InvalidMessage(e) => write!(f, "Invalid message: {e}"), |
65 | | - Error::NotEnoughMemory(e) => write!(f, "Not enough memory: {e}"), |
66 | | - Error::InvalidEnumVariant(e) => write!(f, "Invalid Enum Variant error: {e}"), |
67 | | - Error::InvalidLen(e) => write!(f, "Invalid Len: {e}"), |
68 | | - Error::InvalidNumberParse(e) => write!(f, "Invalid number parse: {e}"), |
69 | | - Error::InvalidTransaction(e) => write!(f, "Invalid transaction: {e}"), |
70 | | - Error::WalletManager(e) => write!(f, "WalletManager error: {e}"), |
71 | | - Error::Cipher(e) => write!(f, "Cipher error: {e}"), |
72 | | - Error::Transport(e) => write!(f, "Transport error: {e}"), |
73 | | - Error::Delegate(e) => write!(f, "Delegate error: {e}"), |
| 13 | +#[wasm_bindgen] |
| 14 | +impl KOSError { |
| 15 | + #[wasm_bindgen(constructor)] |
| 16 | + pub fn new(message: String) -> KOSError { |
| 17 | + KOSError { message } |
| 18 | + } |
| 19 | + |
| 20 | + #[wasm_bindgen(js_name = getMessage)] |
| 21 | + pub fn get_message(&self) -> String { |
| 22 | + self.message.clone() |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl KOSError { |
| 27 | + pub fn unsupported_chain(id: String) -> Self { |
| 28 | + KOSError { |
| 29 | + message: format!("UnsupportedChainError: Unsupported chain {id}"), |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + pub fn kos_delegate(msg: String) -> Self { |
| 34 | + KOSError { |
| 35 | + message: format!("KOSDelegateError: {msg}"), |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + pub fn hex_decode(msg: String) -> Self { |
| 40 | + KOSError { |
| 41 | + message: format!("HexDecodeError: {msg}"), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn kos_number(msg: String) -> Self { |
| 46 | + KOSError { |
| 47 | + message: format!("KOSNumberError: {msg}"), |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + pub fn invalid_string(msg: String) -> Self { |
| 52 | + KOSError { |
| 53 | + message: format!("Invalid string: {msg}"), |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + pub fn json_serde(msg: String) -> Self { |
| 58 | + KOSError { |
| 59 | + message: format!("JSON serialization: {msg}"), |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + pub fn invalid_mnemonic(msg: String) -> Self { |
| 64 | + KOSError { |
| 65 | + message: format!("Invalid mnemonic: {msg}"), |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + pub fn wallet_manager(msg: String) -> Self { |
| 70 | + KOSError { |
| 71 | + message: format!("WalletManager error: {msg}"), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + pub fn cipher(msg: String) -> Self { |
| 76 | + KOSError { |
| 77 | + message: format!("Cipher error: {msg}"), |
74 | 78 | } |
75 | 79 | } |
76 | 80 | } |
77 | 81 |
|
78 | | -impl From<serde_json::Error> for Error { |
79 | | - fn from(e: serde_json::Error) -> Self { |
80 | | - Self::JSONSerde(e.to_string()) |
| 82 | +impl fmt::Display for KOSError { |
| 83 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 84 | + write!(f, "{}", self.message) |
81 | 85 | } |
82 | 86 | } |
83 | 87 |
|
84 | | -impl error::Error for Error { |
85 | | - fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
86 | | - None |
| 88 | +impl From<serde_json::Error> for KOSError { |
| 89 | + fn from(e: serde_json::Error) -> Self { |
| 90 | + KOSError::json_serde(e.to_string()) |
87 | 91 | } |
88 | 92 | } |
89 | 93 |
|
90 | | -impl From<Error> for JsValue { |
91 | | - fn from(e: Error) -> Self { |
92 | | - JsValue::from_str(&format!("{e}")) |
| 94 | +impl error::Error for KOSError { |
| 95 | + fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
| 96 | + None |
93 | 97 | } |
94 | 98 | } |
95 | 99 |
|
96 | | -impl From<hex::FromHexError> for Error { |
| 100 | +impl From<hex::FromHexError> for KOSError { |
97 | 101 | fn from(e: hex::FromHexError) -> Self { |
98 | | - Self::InvalidString(e.to_string()) |
| 102 | + KOSError::hex_decode(e.to_string()) |
99 | 103 | } |
100 | 104 | } |
101 | 105 |
|
102 | | -impl From<ChainError> for Error { |
| 106 | +impl From<ChainError> for KOSError { |
103 | 107 | fn from(err: ChainError) -> Self { |
104 | | - Error::Delegate(err.to_string()) |
| 108 | + KOSError::kos_delegate(err.to_string()) |
105 | 109 | } |
106 | 110 | } |
0 commit comments