@@ -333,12 +333,10 @@ export class RpcService implements AbstractRpcService {
333
333
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
334
334
* @param fetchOptions - An options bag for {@link fetch} which further
335
335
* specifies the request.
336
- * @returns The decoded JSON-RPC response from the endpoint.
337
- * @throws A "method not found" error if the response status is 405.
338
- * @throws A rate limiting error if the response HTTP status is 429.
336
+ * @returns The decoded JSON-RPC response from the endpoint. For 405 status, returns a JSON-RPC error response with code -32601.
337
+ * For 429 status, returns a JSON-RPC error response with code -32005.
339
338
* @throws A timeout error if the response HTTP status is 503 or 504.
340
- * @throws A generic error if the response HTTP status is not 2xx but also not
341
- * 405, 429, 503, or 504.
339
+ * @throws A generic error if the response HTTP status is not 2xx and not one of the specifically handled status codes (405, 429, 503, 504).
342
340
*/
343
341
async request < Params extends JsonRpcParams , Result extends Json > (
344
342
jsonRpcRequest : JsonRpcRequest < Params > & { method : 'eth_getBlockByNumber' } ,
@@ -357,12 +355,10 @@ export class RpcService implements AbstractRpcService {
357
355
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
358
356
* @param fetchOptions - An options bag for {@link fetch} which further
359
357
* specifies the request.
360
- * @returns The decoded JSON-RPC response from the endpoint.
361
- * @throws A "method not found" error if the response status is 405.
362
- * @throws A rate limiting error if the response HTTP status is 429.
358
+ * @returns The decoded JSON-RPC response from the endpoint. For 405 status, returns a JSON-RPC error response with code -32601.
359
+ * For 429 status, returns a JSON-RPC error response with code -32005.
363
360
* @throws A timeout error if the response HTTP status is 503 or 504.
364
- * @throws A generic error if the response HTTP status is not 2xx but also not
365
- * 405, 429, 503, or 504.
361
+ * @throws A generic error if the response HTTP status is not 2xx and not one of the specifically handled status codes (405, 429, 503, 504).
366
362
*/
367
363
async request < Params extends JsonRpcParams , Result extends Json > (
368
364
jsonRpcRequest : JsonRpcRequest < Params > ,
@@ -465,12 +461,10 @@ export class RpcService implements AbstractRpcService {
465
461
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
466
462
* @param fetchOptions - The options for `fetch`; will be combined with the
467
463
* fetch options passed to the constructor
468
- * @returns The decoded JSON-RPC response from the endpoint.
469
- * @throws A "method not found" error if the response status is 405.
470
- * @throws A rate limiting error if the response HTTP status is 429.
464
+ * @returns The decoded JSON-RPC response from the endpoint. For 405 status, returns a JSON-RPC error response with code -32601.
465
+ * For 429 status, returns a JSON-RPC error response with code -32005.
471
466
* @throws A timeout error if the response HTTP status is 503 or 504.
472
- * @throws A generic error if the response HTTP status is not 2xx but also not
473
- * 405, 429, 503, or 504.
467
+ * @throws A generic error if the response HTTP status is not 2xx and not one of the specifically handled status codes (405, 429, 503, 504).
474
468
*/
475
469
async #executePolicy<
476
470
Params extends JsonRpcParams ,
@@ -484,11 +478,31 @@ export class RpcService implements AbstractRpcService {
484
478
const response = await this . #fetch( this . endpointUrl , fetchOptions ) ;
485
479
486
480
if ( response . status === 405 ) {
487
- throw rpcErrors . methodNotFound ( ) ;
481
+ return {
482
+ id : jsonRpcRequest . id ,
483
+ jsonrpc : jsonRpcRequest . jsonrpc ,
484
+ error : {
485
+ code : - 32601 ,
486
+ message : 'Method not supported by this RPC endpoint' ,
487
+ } ,
488
+ } ;
488
489
}
489
490
490
491
if ( response . status === 429 ) {
491
- throw rpcErrors . internal ( { message : 'Request is being rate limited.' } ) ;
492
+ const retryAfter = response . headers . get ( 'Retry-After' ) ;
493
+ const retryDelay = retryAfter ? parseInt ( retryAfter , 10 ) * 1000 : 1000 ;
494
+
495
+ return {
496
+ id : jsonRpcRequest . id ,
497
+ jsonrpc : jsonRpcRequest . jsonrpc ,
498
+ error : {
499
+ code : - 32005 ,
500
+ message : 'Request rate limit exceeded' ,
501
+ data : {
502
+ retryAfter : retryDelay ,
503
+ } ,
504
+ } ,
505
+ } ;
492
506
}
493
507
494
508
if ( response . status === 503 || response . status === 504 ) {
0 commit comments