Skip to content

Commit 1b3c8e5

Browse files
committed
fix(rpc-service): handle 405 and 429 status codes without triggering circuit breaker
- Add proper handling for 405 (Method Not Allowed) status code - Add proper handling for 429 (Too Many Requests) status code - Return appropriate JSON-RPC error responses instead of throwing errors - Include retry delay information in 429 error response
1 parent b2b6d21 commit 1b3c8e5

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

packages/network-controller/src/rpc-service/rpc-service.ts

+31-17
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,10 @@ export class RpcService implements AbstractRpcService {
333333
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
334334
* @param fetchOptions - An options bag for {@link fetch} which further
335335
* 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.
339338
* @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).
342340
*/
343341
async request<Params extends JsonRpcParams, Result extends Json>(
344342
jsonRpcRequest: JsonRpcRequest<Params> & { method: 'eth_getBlockByNumber' },
@@ -357,12 +355,10 @@ export class RpcService implements AbstractRpcService {
357355
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
358356
* @param fetchOptions - An options bag for {@link fetch} which further
359357
* 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.
363360
* @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).
366362
*/
367363
async request<Params extends JsonRpcParams, Result extends Json>(
368364
jsonRpcRequest: JsonRpcRequest<Params>,
@@ -465,12 +461,10 @@ export class RpcService implements AbstractRpcService {
465461
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
466462
* @param fetchOptions - The options for `fetch`; will be combined with the
467463
* 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.
471466
* @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).
474468
*/
475469
async #executePolicy<
476470
Params extends JsonRpcParams,
@@ -484,11 +478,31 @@ export class RpcService implements AbstractRpcService {
484478
const response = await this.#fetch(this.endpointUrl, fetchOptions);
485479

486480
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+
};
488489
}
489490

490491
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+
};
492506
}
493507

494508
if (response.status === 503 || response.status === 504) {

0 commit comments

Comments
 (0)