aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/src/error.rs
blob: 48d0139d94bf6ee123ea0e43fb73657d55ce01c0 (plain)
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
use thiserror::Error as ThisError;

pub type Result<T> = std::result::Result<T, Error>;

/// Represents karyon's jsonrpc Error.
#[derive(ThisError, Debug)]
pub enum Error {
    #[error(transparent)]
    IO(#[from] std::io::Error),

    #[error("Call Error: code: {0} msg: {1}")]
    CallError(i32, String),

    #[error("Subscribe Error: code: {0} msg: {1}")]
    SubscribeError(i32, String),

    #[error("Invalid Message Error: {0}")]
    InvalidMsg(&'static str),

    #[error(transparent)]
    ParseJSON(#[from] serde_json::Error),

    #[error("Unsupported Protocol: {0}")]
    UnsupportedProtocol(String),

    #[error("Tls config is required")]
    TLSConfigRequired,

    #[error("Receive close message from connection: {0}")]
    CloseConnection(String),

    #[error("Subscription not found: {0}")]
    SubscriptionNotFound(String),

    #[error("Subscription exceeds the maximum buffer size")]
    SubscriptionBufferFull,

    #[error("Subscription closed")]
    SubscriptionClosed,

    #[error("ClientDisconnected")]
    ClientDisconnected,

    #[error(transparent)]
    ChannelRecv(#[from] async_channel::RecvError),

    #[error("Channel send  Error: {0}")]
    ChannelSend(String),

    #[error("Unexpected Error: {0}")]
    General(&'static str),

    #[error(transparent)]
    KaryonCore(#[from] karyon_core::error::Error),

    #[error(transparent)]
    KaryonNet(#[from] karyon_net::Error),
}

impl<T> From<async_channel::SendError<T>> for Error {
    fn from(error: async_channel::SendError<T>) -> Self {
        Error::ChannelSend(error.to_string())
    }
}

pub type RPCResult<T> = std::result::Result<T, RPCError>;

/// Represents RPC Error.
#[derive(ThisError, Debug)]
pub enum RPCError {
    #[error("Custom Error:  code: {0} msg: {1}")]
    CustomError(i32, &'static str),

    #[error("Invalid Params: {0}")]
    InvalidParams(&'static str),

    #[error("Invalid Request: {0}")]
    InvalidRequest(&'static str),

    #[error("Parse Error: {0}")]
    ParseError(String),

    #[error("Internal Error")]
    InternalError,
}

impl From<serde_json::Error> for RPCError {
    fn from(error: serde_json::Error) -> Self {
        RPCError::ParseError(error.to_string())
    }
}