Skip to main content

json_rpc/
error_code.rs

1/// Standard JSON-RPC 2.0 error codes.
2///
3/// The range from -32768 to -32000 is reserved for pre-defined errors.
4/// The range from -32000 to -32099 is reserved for server errors.
5/// All other codes are available for application-defined errors.
6pub struct ErrorCode;
7
8impl ErrorCode {
9    /// Invalid JSON was received by the server.
10    /// An error occurred on the server while parsing the JSON text.
11    pub const PARSE_ERROR: i32 = -32700;
12
13    /// The JSON sent is not a valid Request object.
14    pub const INVALID_REQUEST: i32 = -32600;
15
16    /// The method does not exist / is not available.
17    pub const METHOD_NOT_FOUND: i32 = -32601;
18
19    /// Invalid method parameter(s).
20    pub const INVALID_PARAMS: i32 = -32602;
21
22    /// Internal JSON-RPC error.
23    pub const INTERNAL_ERROR: i32 = -32603;
24
25    /// The lowest reserved server error code.
26    pub const SERVER_ERROR_MIN: i32 = -32099;
27
28    /// The highest reserved server error code.
29    pub const SERVER_ERROR_MAX: i32 = -32000;
30
31    /// Returns `true` if the given code is in the reserved pre-defined error range.
32    pub fn is_reserved(code: i32) -> bool {
33        (-32768..=-32000).contains(&code)
34    }
35
36    /// Returns `true` if the given code is in the server error range.
37    pub fn is_server_error(code: i32) -> bool {
38        (-32099..=-32000).contains(&code)
39    }
40
41    /// Returns a human-readable default message for a given standard error code.
42    pub fn default_message(code: i32) -> &'static str {
43        match code {
44            Self::PARSE_ERROR => "Parse error",
45            Self::INVALID_REQUEST => "Invalid Request",
46            Self::METHOD_NOT_FOUND => "Method not found",
47            Self::INVALID_PARAMS => "Invalid params",
48            Self::INTERNAL_ERROR => "Internal error",
49            _ => "Server error",
50        }
51    }
52}