axum_jrpc/
error.rs

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
use super::Value;

use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Constants for [error object](https://www.jsonrpc.org/specification#error_object)
pub const INVALID_REQUEST: i32 = -32600;
pub const METHOD_NOT_FOUND: i32 = -32601;
pub const INVALID_PARAMS: i32 = -32602;
pub const INTERNAL_ERROR: i32 = -32603;
pub const PARSE_ERROR: i32 = -32700;

#[derive(Debug, Clone, Copy)]
pub enum JsonRpcErrorReason {
    ParseError,
    InvalidRequest,
    MethodNotFound,
    InvalidParams,
    InternalError,
    /// -32000 to -32099
    ServerError(i32),
    /// All other space
    ApplicationError(i32),
}

impl std::fmt::Display for JsonRpcErrorReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            JsonRpcErrorReason::ParseError => write!(f, "Parse error"),
            JsonRpcErrorReason::InvalidRequest => write!(f, "Invalid Request"),
            JsonRpcErrorReason::MethodNotFound => write!(f, "Method not found"),
            JsonRpcErrorReason::InvalidParams => write!(f, "Invalid params"),
            JsonRpcErrorReason::InternalError => write!(f, "Internal error"),
            JsonRpcErrorReason::ServerError(code) => write!(f, "Server error: {}", code),
            JsonRpcErrorReason::ApplicationError(code) => {
                write!(f, "Application error: {}", code)
            }
        }
    }
}

impl From<JsonRpcErrorReason> for i32 {
    fn from(reason: JsonRpcErrorReason) -> i32 {
        match reason {
            JsonRpcErrorReason::ParseError => PARSE_ERROR,
            JsonRpcErrorReason::InvalidRequest => INVALID_REQUEST,
            JsonRpcErrorReason::MethodNotFound => METHOD_NOT_FOUND,
            JsonRpcErrorReason::InvalidParams => INVALID_PARAMS,
            JsonRpcErrorReason::InternalError => INTERNAL_ERROR,
            JsonRpcErrorReason::ServerError(code) | JsonRpcErrorReason::ApplicationError(code) => {
                code
            }
        }
    }
}

impl JsonRpcErrorReason {
    fn new(code: i32) -> Self {
        match code {
            PARSE_ERROR => Self::ParseError,
            INVALID_REQUEST => Self::InvalidRequest,
            METHOD_NOT_FOUND => Self::MethodNotFound,
            INVALID_PARAMS => Self::InvalidParams,
            INTERNAL_ERROR => Self::InternalError,
            -32099..=-32000 => Self::ServerError(code),
            _ => Self::ApplicationError(code),
        }
    }
}

#[derive(Debug, Error, Clone, Serialize, Deserialize, PartialEq)]
pub struct JsonRpcError {
    code: i32,
    message: String,
    data: Value,
}

impl JsonRpcError {
    pub fn new(code: JsonRpcErrorReason, message: String, data: Value) -> Self {
        Self {
            code: code.into(),
            message,
            data,
        }
    }
}

impl std::fmt::Display for JsonRpcError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}: {}",
            JsonRpcErrorReason::new(self.code),
            self.message
        )
    }
}

#[cfg(feature = "anyhow_error")]
impl From<anyhow::Error> for JsonRpcError {
    fn from(error: anyhow::Error) -> Self {
        let message = error.to_string();
        let data = Value::default();
        Self {
            code: 1,
            message,
            data,
        }
    }
}

impl JsonRpcError {
    pub fn error_reason(&self) -> JsonRpcErrorReason {
        JsonRpcErrorReason::new(self.code)
    }

    pub fn code(&self) -> i32 {
        self.code
    }
}