openzeppelin_relayer/models/rpc/error/
openzeppelin_codes.rs

1/// Custom OpenZeppelin error codes for extended JSON-RPC functionality.
2///
3/// These error codes extend the standard JSON-RPC 2.0 error codes with
4/// OpenZeppelin-specific error conditions. All codes start from -33000
5/// to avoid conflicts with standard JSON-RPC codes (-32000 to -32099)
6/// and other common extensions.
7pub struct OpenZeppelinErrorCodes;
8
9impl OpenZeppelinErrorCodes {
10    /// Request timeout - The request took too long to complete.
11    pub const TIMEOUT: i32 = -33000;
12
13    /// Rate limited - Too many requests, client should back off.
14    pub const RATE_LIMITED: i32 = -33001;
15
16    /// Bad gateway - The upstream server returned an invalid response.
17    pub const BAD_GATEWAY: i32 = -33002;
18
19    /// Request error - Generic request error with additional HTTP status context.
20    pub const REQUEST_ERROR: i32 = -33003;
21
22    /// Network configuration error - Issues with network or provider configuration.
23    pub const NETWORK_CONFIGURATION: i32 = -33004;
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    #[allow(clippy::manual_range_contains)]
32    fn test_openzeppelin_error_codes_are_not_in_reserved_range() {
33        let codes = vec![
34            OpenZeppelinErrorCodes::TIMEOUT,
35            OpenZeppelinErrorCodes::RATE_LIMITED,
36            OpenZeppelinErrorCodes::BAD_GATEWAY,
37            OpenZeppelinErrorCodes::REQUEST_ERROR,
38            OpenZeppelinErrorCodes::NETWORK_CONFIGURATION,
39        ];
40
41        for code in codes {
42            assert!(
43                !(code >= -32099 && code <= -32000),
44                "Code {} is part of the reserved range for implementation-defined server errors",
45                code
46            );
47        }
48    }
49
50    #[test]
51    fn test_openzeppelin_error_codes_are_unique() {
52        let codes = vec![
53            OpenZeppelinErrorCodes::TIMEOUT,
54            OpenZeppelinErrorCodes::RATE_LIMITED,
55            OpenZeppelinErrorCodes::BAD_GATEWAY,
56            OpenZeppelinErrorCodes::REQUEST_ERROR,
57            OpenZeppelinErrorCodes::NETWORK_CONFIGURATION,
58        ];
59
60        let mut unique_codes = codes.clone();
61        unique_codes.sort();
62        unique_codes.dedup();
63
64        assert_eq!(
65            codes.len(),
66            unique_codes.len(),
67            "All error codes should be unique"
68        );
69    }
70
71    #[test]
72    fn test_openzeppelin_error_codes_start_from_33000() {
73        let codes = vec![
74            OpenZeppelinErrorCodes::TIMEOUT,
75            OpenZeppelinErrorCodes::RATE_LIMITED,
76            OpenZeppelinErrorCodes::BAD_GATEWAY,
77            OpenZeppelinErrorCodes::REQUEST_ERROR,
78            OpenZeppelinErrorCodes::NETWORK_CONFIGURATION,
79        ];
80
81        for code in codes {
82            assert!(
83                code <= -33000,
84                "All OpenZeppelin codes should be <= -33000, found: {}",
85                code
86            );
87        }
88    }
89}