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 {} is part of the reserved range for implementation-defined server errors",
44                code
45            );
46        }
47    }
48
49    #[test]
50    fn test_rpc_error_codes_are_unique() {
51        let codes = vec![
52            RpcErrorCodes::PARSE,
53            RpcErrorCodes::INVALID_REQUEST,
54            RpcErrorCodes::METHOD_NOT_FOUND,
55            RpcErrorCodes::INVALID_PARAMS,
56            RpcErrorCodes::INTERNAL_ERROR,
57        ];
58
59        let mut unique_codes = codes.clone();
60        unique_codes.sort();
61        unique_codes.dedup();
62
63        assert_eq!(
64            codes.len(),
65            unique_codes.len(),
66            "All error codes should be unique"
67        );
68    }
69}