openzeppelin_relayer/models/rpc/error/
common_codes.rs

1/// JSON-RPC 2.0 error codes as defined in the specification.
2///
3/// These constants represent the standard error codes used in JSON-RPC 2.0 protocol.
4/// Each error code has a specific meaning and should be used consistently across
5/// all RPC implementations.
6pub struct RpcErrorCodes;
7
8impl RpcErrorCodes {
9    /// Parse error - Invalid JSON was received by the server.
10    pub const PARSE: i32 = -32700;
11
12    /// Invalid Request - The JSON sent is not a valid Request object.
13    pub const INVALID_REQUEST: i32 = -32600;
14
15    /// Method not found - The method does not exist / is not available.
16    pub const METHOD_NOT_FOUND: i32 = -32601;
17
18    /// Invalid params - Invalid method parameter(s).
19    pub const INVALID_PARAMS: i32 = -32602;
20
21    /// Internal error - Internal JSON-RPC error.
22    pub const INTERNAL_ERROR: i32 = -32603;
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    #[allow(clippy::manual_range_contains)]
31    fn test_rpc_error_codes_are_outside_of_reserved_range() {
32        let codes = vec![
33            RpcErrorCodes::PARSE,
34            RpcErrorCodes::INVALID_REQUEST,
35            RpcErrorCodes::METHOD_NOT_FOUND,
36            RpcErrorCodes::INVALID_PARAMS,
37            RpcErrorCodes::INTERNAL_ERROR,
38        ];
39
40        for code in codes {
41            assert!(
42                !(code >= -32099 && code <= -32000),
43                "Code {code} is part of the reserved range for implementation-defined server errors"
44            );
45        }
46    }
47
48    #[test]
49    fn test_rpc_error_codes_are_unique() {
50        let codes = vec![
51            RpcErrorCodes::PARSE,
52            RpcErrorCodes::INVALID_REQUEST,
53            RpcErrorCodes::METHOD_NOT_FOUND,
54            RpcErrorCodes::INVALID_PARAMS,
55            RpcErrorCodes::INTERNAL_ERROR,
56        ];
57
58        let mut unique_codes = codes.clone();
59        unique_codes.sort();
60        unique_codes.dedup();
61
62        assert_eq!(
63            codes.len(),
64            unique_codes.len(),
65            "All error codes should be unique"
66        );
67    }
68}