Skip to main content

kaspa_grpc_client/
error.rs

1use kaspa_notify::error::Error as NotifyError;
2use kaspa_rpc_core::RpcError;
3use thiserror::Error;
4
5pub type BoxedStdError = Box<dyn std::error::Error + Sync + std::marker::Send + 'static>;
6
7#[derive(Debug, Error)]
8pub enum Error {
9    #[error("Error: {0}")]
10    String(String),
11
12    #[error("GRPC invalid address schema {0}")]
13    GrpcAddressSchema(String),
14
15    #[error("GRPC client error {0}")]
16    TonicStatus(#[from] tonic::Status),
17
18    /// RPC call timeout
19    #[error("RPC request timeout")]
20    Timeout,
21
22    #[error("Endpoint connection error: {0}")]
23    EndpointConnectionError(#[from] tonic::transport::Error),
24
25    #[error("Notify error: {0}")]
26    NotifyError(#[from] kaspa_notify::error::Error),
27
28    #[error("RPC: channel receive error")]
29    ChannelRecvError,
30
31    #[error("RPC: channel send error")]
32    ChannelSendError,
33
34    #[error("Missing request payload")]
35    MissingRequestPayload,
36
37    #[error("Missing response payload")]
38    MissingResponsePayload,
39
40    #[error("Not connected to server")]
41    NotConnected,
42}
43
44impl From<Error> for RpcError {
45    fn from(value: Error) -> Self {
46        RpcError::General(value.to_string())
47    }
48}
49
50impl From<BoxedStdError> for Error {
51    fn from(err: BoxedStdError) -> Self {
52        Error::String(err.to_string())
53    }
54}
55
56impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
57    fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
58        Error::ChannelSendError
59    }
60}
61
62impl From<tokio::sync::oneshot::error::RecvError> for Error {
63    fn from(_: tokio::sync::oneshot::error::RecvError) -> Self {
64        Error::ChannelRecvError
65    }
66}
67
68impl<T> From<async_channel::SendError<T>> for Error {
69    fn from(_: async_channel::SendError<T>) -> Self {
70        Error::ChannelSendError
71    }
72}
73
74impl<T> From<async_channel::TrySendError<T>> for Error {
75    fn from(_: async_channel::TrySendError<T>) -> Self {
76        Error::ChannelSendError
77    }
78}
79
80impl From<async_channel::RecvError> for Error {
81    fn from(_: async_channel::RecvError) -> Self {
82        Error::ChannelRecvError
83    }
84}
85
86impl From<Error> for NotifyError {
87    fn from(err: Error) -> Self {
88        match err {
89            Error::String(message) => NotifyError::General(message),
90            Error::NotifyError(err) => err,
91            Error::ChannelRecvError => NotifyError::ChannelRecvError,
92            Error::ChannelSendError => NotifyError::ChannelSendError,
93            Error::TonicStatus(err) => NotifyError::General(format!("{err}")),
94            _ => NotifyError::General(format!("{err}")),
95        }
96    }
97}
98
99pub type Result<T> = std::result::Result<T, Error>;