jsonrpc_lite/
error.rs

1//! JSON-RPC 2.0 errors
2use std::error;
3use std::result;
4use std::fmt;
5use std::string::ToString;
6
7use serde_json::Value;
8
9/// Error Code
10#[derive(Clone, PartialEq, Debug)]
11pub enum ErrorCode {
12    /// Invalid JSON was received by the server.
13    /// An error occurred on the server while parsing the JSON text.
14    ParseError,
15    /// The JSON sent is not a valid Request object.
16    InvalidRequest,
17    /// The method does not exist / is not available.
18    MethodNotFound,
19    /// Invalid method parameter(s).
20    InvalidParams,
21    /// Internal JSON-RPC error.
22    InternalError,
23    /// Reserved for implementation-defined server-errors.
24    ServerError(i64),
25}
26
27impl ErrorCode {
28    pub fn code(&self) -> i64 {
29        match *self {
30            ErrorCode::ParseError => -32700,
31            ErrorCode::InvalidRequest => -32600,
32            ErrorCode::MethodNotFound => -32601,
33            ErrorCode::InvalidParams => -32602,
34            ErrorCode::InternalError => -32603,
35            ErrorCode::ServerError(code) => code,
36        }
37    }
38
39    pub fn as_str(&self) -> &'static str {
40        match *self {
41            ErrorCode::ParseError => "Parse error",
42            ErrorCode::InvalidRequest => "Invalid request",
43            ErrorCode::MethodNotFound => "Method not found",
44            ErrorCode::InvalidParams => "Invalid params",
45            ErrorCode::InternalError => "Internal error",
46            ErrorCode::ServerError(_) => "Server error",
47        }
48    }
49}
50
51impl ToString for ErrorCode {
52    fn to_string(&self) -> String {
53        String::from(self.as_str())
54    }
55}
56
57/// Error Object
58#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
59pub struct Error {
60    pub code: i64,
61    pub message: String,
62    #[serde(default)]
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub data: Option<Value>,
65}
66
67impl Error {
68    pub fn new(code: ErrorCode) -> Self {
69        Error {
70            code: code.code(),
71            message: code.to_string(),
72            data: None,
73        }
74    }
75
76    pub fn parse_error() -> Self {
77        Self::new(ErrorCode::ParseError)
78    }
79
80    pub fn invalid_request() -> Self {
81        Self::new(ErrorCode::InvalidRequest)
82    }
83
84    pub fn method_not_found() -> Self {
85        Self::new(ErrorCode::MethodNotFound)
86    }
87
88    pub fn invalid_params() -> Self {
89        Self::new(ErrorCode::InvalidParams)
90    }
91
92    pub fn internal_error() -> Self {
93        Self::new(ErrorCode::InternalError)
94    }
95}
96
97impl error::Error for Error {
98    fn description(&self) -> &str {
99        &self.message
100    }
101}
102
103impl fmt::Display for Error {
104    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105        fmt::Display::fmt(&::serde_json::to_string(&self).unwrap(), f)
106    }
107}
108
109pub type Result<T> = result::Result<T, Error>;