1+ use  crate :: presentation:: transaction; 
2+ use  alloy:: hex; 
3+ use  std:: fmt; 
4+ use  alloy_primitives:: private:: alloy_rlp; 
5+ use  crate :: presentation:: json_rpc_errors:: InvalidInputError :: { InvalidJson ,  MissingGasPrice ,  InvalidTransactionSignature ,  InvalidUint ,  UnableToRLPDecode } ; 
6+ use  crate :: presentation:: json_rpc_errors:: InvalidParamsError :: InvalidHex ; 
7+ use  crate :: presentation:: json_rpc_errors:: Rejection :: FeeTooHigh ; 
8+ 
19// Source: https://github.com/MetaMask/rpc-errors/blob/main/src/errors.ts 
210#[ derive( Debug ,  Clone ,  PartialEq ) ]  
3- pub  enum  JsonRpcErrorCode  { 
4-     InvalidRequest  = -32600 , 
5-     MethodNotFound  = -32601 , 
6-     InvalidParams  = -32602 , 
7-     InternalError  = -32603 , 
8-     ParseError  = -32700 , 
9-     InvalidInput  = -32000 , 
10-     ResourceNotFound  = -32001 , 
11-     ResourceUnavailable  = -32002 , 
12-     TransactionRejected  = -32003 , 
13-     MethodNotSupported  = -32004 , 
14-     LimitExceeded  = -32005 , 
15-     ServerError  = -32099 ,  // We'll use this as the base for server errors 
16- } 
17- 
18- impl  JsonRpcErrorCode  { 
19-     // TODO - bring back if needed 
20-     // pub fn message(&self) -> &'static str { 
21-     //     match self { 
22-     //         Self::InvalidRequest => "Invalid request", 
23-     //         Self::MethodNotFound => "Method not found", 
24-     //         Self::InvalidParams => "Invalid params", 
25-     //         Self::InternalError => "Internal error", 
26-     //         Self::ParseError => "Parse error", 
27-     //         Self::InvalidInput => "Invalid input", 
28-     //         Self::ResourceNotFound => "Resource not found", 
29-     //         Self::ResourceUnavailable => "Resource unavailable", 
30-     //         Self::TransactionRejected => "Transaction rejected", 
31-     //         Self::MethodNotSupported => "Method not supported", 
32-     //         Self::LimitExceeded => "Limit exceeded", 
33-     //         Self::ServerError => "Server error", 
34-     //     } 
35-     // } 
36- } 
37- 
38- impl  From < i32 >  for  JsonRpcErrorCode  { 
39-     fn  from ( value :  i32 )  -> Self  { 
40-         match  value { 
41-             -32700  => Self :: ParseError , 
42-             -32600  => Self :: InvalidRequest , 
43-             -32601  => Self :: MethodNotFound , 
44-             -32602  => Self :: InvalidParams , 
45-             -32603  => Self :: InternalError , 
46-             -32000  => Self :: InvalidInput , 
47-             -32001  => Self :: ResourceNotFound , 
48-             -32002  => Self :: ResourceUnavailable , 
49-             -32003  => Self :: TransactionRejected , 
50-             -32004  => Self :: MethodNotSupported , 
51-             -32005  => Self :: LimitExceeded , 
52-             _ if  ( -32099 ..=-32000 ) . contains ( & value)  => Self :: ServerError , 
53-             _ => Self :: InternalError ,  // Default case 
11+ pub  enum  Error  { 
12+     // Parent errors with a JSON-RPC error code mapping 
13+     InvalidRequest , 
14+     MethodNotFound ( String ) , 
15+     InvalidParams ( InvalidParamsError ) , 
16+     Internal , 
17+     Parse , 
18+     InvalidInput ( InvalidInputError ) , 
19+     ResourceNotFound , 
20+     ResourceUnavailable , 
21+     TransactionRejected ( Rejection ) , 
22+     MethodNotSupported , 
23+     LimitExceeded , 
24+     Server , 
25+ } 
26+ 
27+ #[ derive( Debug ,  Clone ,  PartialEq ) ]  
28+ pub  enum  Rejection  { 
29+     FeeTooHigh , 
30+ } 
31+ 
32+ #[ derive( Debug ,  Clone ,  PartialEq ) ]  
33+ pub  enum  InvalidParamsError  { 
34+     BadSignature , 
35+     NonceTooLow , 
36+     InvalidHex , 
37+     NotAnArray , 
38+     WrongParamCount ( usize ) , 
39+     MissingParam , 
40+     NotHexEncoded 
41+ } 
42+ 
43+ #[ derive( Debug ,  Clone ,  PartialEq ) ]  
44+ pub  enum  InvalidInputError  { 
45+     InvalidJson , 
46+     InvalidUint , 
47+     InvalidTransactionSignature , 
48+     MissingGasPrice , 
49+     UnableToRLPDecode 
50+ } 
51+ 
52+ impl  fmt:: Display  for  InvalidParamsError  { 
53+     fn  fmt ( & self ,  f :  & mut  fmt:: Formatter < ' _ > )  -> fmt:: Result  { 
54+         match  self  { 
55+             InvalidParamsError :: BadSignature  => write ! ( f,  "bad signature" ) , 
56+             InvalidParamsError :: NonceTooLow  => write ! ( f,  "nonce too low" ) , 
57+             InvalidHex  => write ! ( f,  "invalid hex" ) , 
58+             InvalidParamsError :: NotAnArray  => write ! ( f,  "params must be an array" ) , 
59+             InvalidParamsError :: WrongParamCount ( _)  => write ! ( f,  "wrong number of params" ) , 
60+             InvalidParamsError :: MissingParam  => write ! ( f,  "missing param" ) , 
61+             InvalidParamsError :: NotHexEncoded  => write ! ( f,  "not a hex encoded string" ) , 
62+         } 
63+     } 
64+ } 
65+ 
66+ impl  fmt:: Display  for  InvalidInputError  { 
67+     fn  fmt ( & self ,  f :  & mut  fmt:: Formatter < ' _ > )  -> fmt:: Result  { 
68+         match  self  { 
69+             InvalidJson  => write ! ( f,  "invalid JSON" ) , 
70+             InvalidUint  => write ! ( f,  "invalid uint" ) , 
71+             InvalidTransactionSignature  => write ! ( f,  "invalid transaction signature" ) , 
72+             MissingGasPrice  => write ! ( f,  "transaction missing gas price" ) , 
73+             UnableToRLPDecode  => write ! ( f,  "unable to RLP decode" ) , 
74+         } 
75+     } 
76+ } 
77+ 
78+ impl  fmt:: Display  for  Error  { 
79+     fn  fmt ( & self ,  f :  & mut  fmt:: Formatter < ' _ > )  -> fmt:: Result  { 
80+         match  self  { 
81+             Error :: InvalidRequest  => write ! ( f,  "invalid request" , ) , 
82+             Error :: MethodNotFound ( m)  => write ! ( f,  "method not found: {}" ,  m) , 
83+             Error :: InvalidParams ( m)  => write ! ( f,  "invalid params: {}" ,  m) , 
84+             Error :: Internal  => write ! ( f,  "internal error" ) , 
85+             Error :: Parse  => write ! ( f,  "parse error" ) , 
86+             Error :: InvalidInput ( m)  => write ! ( f,  "invalid input: {}" ,  m) , 
87+             Error :: ResourceNotFound  => write ! ( f,  "resource not found" ,  ) , 
88+             Error :: ResourceUnavailable  => write ! ( f,  "resource unavailable" , ) , 
89+             Error :: TransactionRejected ( m)  => write ! ( f,  "transaction rejected: {}" ,  m) , 
90+             Error :: MethodNotSupported  => write ! ( f,  "method not supported" ) , 
91+             Error :: LimitExceeded  => write ! ( f,  "limit exceeded" ) , 
92+             Error :: Server  => write ! ( f,  "server error" ) , 
93+         } 
94+     } 
95+ } 
96+ 
97+ impl  fmt:: Display  for  Rejection  { 
98+     fn  fmt ( & self ,  f :  & mut  fmt:: Formatter < ' _ > )  -> fmt:: Result  { 
99+         match  self  { 
100+             FeeTooHigh  => write ! ( f,  "transaction fee too high" ) , 
54101        } 
55102    } 
56103} 
57104
58- impl  JsonRpcErrorCode  { 
59-     pub   fn  code ( & self )  -> i32  { 
60-         self . to_owned ( )   as   i32 
105+ impl  From < serde_json :: Error >   for   Error  { 
106+     fn  from ( _ :  serde_json :: Error )  -> Self  { 
107+         Error :: InvalidInput ( InvalidJson ) 
61108    } 
62109} 
110+ 
111+ impl  From < hex:: FromHexError >  for  Error  { 
112+     fn  from ( _:  hex:: FromHexError )  -> Self  { 
113+         Error :: InvalidParams ( InvalidHex ) 
114+     } 
115+ } 
116+ 
117+ impl  From < alloy_rlp:: Error >  for  Error  { 
118+     fn  from ( _:  alloy_rlp:: Error )  -> Self  { 
119+         Error :: InvalidInput ( UnableToRLPDecode ) 
120+     } 
121+ } 
122+ 
123+ impl  From < alloy_primitives:: SignatureError >  for  Error  { 
124+     fn  from ( _:  alloy_primitives:: SignatureError )  -> Self  { 
125+         Error :: InvalidInput ( InvalidTransactionSignature ) 
126+     } 
127+ } 
128+ 
129+ impl  From < transaction:: TransactionFeeTooHigh >  for  Error  { 
130+     fn  from ( _:  transaction:: TransactionFeeTooHigh )  -> Self  { 
131+         Error :: TransactionRejected ( FeeTooHigh ) 
132+     } 
133+ } 
134+ 
135+ impl < T >  From < alloy_primitives:: ruint:: ToUintError < T > >  for  Error  { 
136+     fn  from ( _:  alloy_primitives:: ruint:: ToUintError < T > )  -> Self  { 
137+         Error :: InvalidInput ( InvalidUint ) 
138+     } 
139+ } 
0 commit comments