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
141
142
143
144
145
146
use std::fmt::Display;

use thiserror::Error;
use workflow_websocket::client::error::Error as WebSocketError;
use wasm_bindgen::JsValue;
use workflow_core::channel::{RecvError,SendError};
use crate::asynchronous::error::RpcResponseError;
use serde::*;
// use borsh::*;

#[derive(Error, Debug)] // , BorshSerialize, BorshDeserialize)]
pub enum Error {

    #[error("Invalid URL {0}")]
    InvalidUrl(String),
    /// Underlying WebSocket error
    #[error("WebSocket error: {0}")]
    WebSocketError(#[from] WebSocketError),
    /// RPC call timeout
    #[error("RPC request timeout")]
    Timeout,
    /// Unable to send shutdown message to receiver
    #[error("Receiver ctl failure")]
    ReceiverCtl,
    /// Received message is smaller than the minimum header size
    #[error("Invalid header size")]
    HeaderSize,
    /// RPC call succeeded but no data was received in success response
    #[error("RPC: no data in success response")]
    NoDataInSuccessResponse,
    /// RPC call failed but no data was received in error response
    #[error("RPC: no data in error response")]
    NoDataInErrorResponse,
    /// Unable to deserialize response data
    #[error("RPC: error deserializing response data")]
    ErrorDeserializingResponseData,
    /// Response produced an unknown status code
    #[error("RPC: status code {0}")]
    StatusCode(u32),
    /// RPC call executed successfully but produced an error response
    #[error("RPC: response error {0:?}")]
    RpcCall(RpcResponseError),
    /// Unable to serialize borsh data    
    #[error("RPC: borsh serialization error")]
    BorshSerialize,
    /// Unable to deserialize borsh data
    #[error("RPC: borsh deserialization error")]
    BorshDeserialize,
    /// Unable to serialize serde data    
    #[error("RPC: serde serialization error")]
    SerdeSerialize(String), //#[from] dyn serde::de::Error),
    /// Unable to deserialize serde data
    #[error("RPC: serde deserialization error")]
    SerdeDeserialize(String),
    /// RPC call succeeded, but error occurred deserializing borsh response
    #[error("RPC: borsh error deserializing response: {0}")]
    BorshResponseDeserialize(String),

    #[error("RPC: channel receive error")]
    ChannelRecvError,

    #[error("RPC: channel send error")]
    ChannelSendError,

    #[error("Utf8 error: {0}")]
    Utf8Error(#[from] std::str::Utf8Error),

    #[error("SerdeJSON error: {0}")]
    SerdeJSON(#[from] serde_json::Error),
}

/// Transform Error into JsValue containing the error message
impl Into<JsValue> for Error {
    fn into(self) -> JsValue {
        JsValue::from(format!("{}",self).to_string())
    }
}

impl From<RecvError> for Error {
    fn from(_: RecvError) -> Self {
        Error::ChannelRecvError
    }
}

impl<T> From<SendError<T>> for Error {
    fn from(_: SendError<T>) -> Self {
        Error::ChannelSendError
    }
}

// impl ser::Error for Error {
//     fn custom<T: Display>(msg: T) -> Self {
//         Error::SerdeSerialize(msg.to_string())
//     }
// }

// impl de::Error for Error {
//     fn custom<T: Display>(msg: T) -> Self {
//         Error::SerdeDeserialize(msg.to_string())
//     }
// }


// impl From<dyn serde::ser::Error> for Error
// where dyn serde::ser::Error: Sized
// {
//     fn from(e: dyn serde::ser::Error) -> Error {
//         Error::SerdeSerialize(e.to_string())
//     }
//     // fn custom<T: Display>(msg: T) -> Self {
//     //     Error::Message(msg.to_string())
//     // }
// }

// impl From<dyn serde::de::Error> for Error {
//     fn from(e: dyn serde::de::Error) -> Error {
//         Error::SerdeDeserialize(e.to_string())
//     }
//     // fn custom<T: Display>(msg: T) -> Self {
//     //     Error::Message(msg.to_string())
//     // }
// }

impl de::Error for Error {
    fn custom<T: Display>(msg: T) -> Error {
        Error::SerdeDeserialize(msg.to_string())
    }
}

// impl From<serde_json::Error> for Error {
//     fn from(err: serde_json::Error) -> Error {
//         Error::SerdeDeserialize(err.to_string())
//     }
// }

impl ser::Error for Error {
    fn custom<T: Display>(msg: T) -> Error {
        Error::SerdeSerialize(msg.to_string())
    }
}

// impl From<std::str::Utf8Error> for Error {
//     fn from(e: std::str::Utf8Error) -> Error {
//         Error::Utf8Error(e)
//     }
// }