Skip to content

Commit ca949ab

Browse files
authored
[QoS][JSONRPC] Generic error responses use JSONRPC error codes not in the reserved ranges (#454)
## Summary Standardize JSON-RPC error codes across the codebase by introducing dedicated constants and distinguishing between internal PATH errors and backend server errors ### Primary Changes: - Add new ResponseCodeBackendServerErr (-31002) constant to distinguish backend server errors from internal PATH errors - Update ResponseCodeDefaultInternalErr from -32000 to -31001 to use non-reserved JSON-RPC error codes - Replace hardcoded -32000 error codes throughout codebase with appropriate named constants ### Secondary Changes: - Update comments to reference new constant names and provide clearer error code documentation - Add TODO comment in evm/errors.go considering further error code granularity ## Issue Need to distinguish PATH generated JSONRPC error codes from endpoint/backend server JSONRPC error responses. - Issue or PR: #{ISSUE_OR_PR_NUMBER} ## Type of change Select one or more from the following: - [x] New feature, functionality or library - [ ] Bug fix - [ ] Code health or cleanup - [ ] Documentation - [ ] Other (specify) ## QoS Checklist ### E2E Validation & Tests - [ ] `make path_up` - [ ] `make test_e2e_evm_shannon` ### Observability - [ ] 1. `make path_up` - [ ] 2. Run the following E2E test: `make test_request__shannon_relay_util_100` - [ ] 3. View results in LocalNet's [PATH Relay Grafana Dashboard](http://localhost:3003/d/relays/path-service-requests) ## Sanity Checklist - [x] I have updated the GitHub Issue `assignees`, `reviewers`, `labels`, `project`, `iteration` and `milestone` - [ ] For docs, I have run `make docusaurus_start` - [ ] For code, I have run `make test_all` - [ ] For configurations, I have updated the documentation - [x] I added `TODO`s where applicable
1 parent dde8581 commit ca949ab

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

qos/cosmos/response_jsonrpc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313

1414
const (
1515
// error codes to use as the JSONRPC response's error code if the endpoint returns a malformed response.
16-
// -32000 Error code will result in returning a 500 HTTP Status Code to the client.
17-
errCodeUnmarshaling = -32000
18-
errCodeEmptyResponse = -32000
16+
// `jsonrpc.ResponseCodeBackendServerErr`, i.e. code -31002, will result in returning a 500 HTTP Status Code to the client.
17+
errCodeUnmarshaling = jsonrpc.ResponseCodeBackendServerErr
18+
errCodeEmptyResponse = jsonrpc.ResponseCodeBackendServerErr
1919

2020
// Error messages for JSONRPC response validation failures
2121
errMsgJSONRPCUnmarshaling = "the JSONRPC response returned by the endpoint is not valid"

qos/evm/errors.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"github.com/buildwithgrove/path/qos/jsonrpc"
77
)
88

9+
// TODO_CONSIDERATION(@adshmh): Is there any value in further breaking down error codes here to indicate different failures?
10+
//
911
// newErrResponseInternalErr creates a JSON-RPC error response when an internal error has occurred (e.g. reading HTTP request's body)
1012
// Marks the error as retryable to allow clients to safely retry their request.
1113
func newErrResponseInternalErr(requestID jsonrpc.ID, err error) jsonrpc.Response {
1214
return jsonrpc.GetErrorResponse(
1315
requestID,
14-
-32000, // JSON-RPC standard server error code; https://www.jsonrpc.org/historical/json-rpc-2-0.html
16+
jsonrpc.ResponseCodeDefaultInternalErr, // Used to indicate an internal error on PATH/protocol (e.g. failed to read the HTTP request's body)
1517
fmt.Sprintf("internal error: %s", err.Error()), // Error Message
1618
map[string]string{
1719
"error": err.Error(),
@@ -32,8 +34,8 @@ func newErrResponseInternalErr(requestID jsonrpc.ID, err error) jsonrpc.Response
3234
// The error is marked as permanent since retrying without correcting the request will fail.
3335
func newErrResponseInvalidRequest(err error, requestID jsonrpc.ID) jsonrpc.Response {
3436
return jsonrpc.GetErrorResponse(
35-
requestID, // Use request's original ID if present
36-
-32000, // JSON-RPC standard server error code; https://www.jsonrpc.org/historical/json-rpc-2-0.html
37+
requestID, // Use request's original ID if present
38+
jsonrpc.ResponseCodeDefaultBadRequest, // JSON-RPC error code indicating bad user request
3739
fmt.Sprintf("invalid request: %s", err.Error()), // Error Message
3840
map[string]string{
3941
"error": err.Error(),

qos/evm/response_generic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212

1313
const (
1414
// errCodeUnmarshaling is set as the JSONRPC response's error code if the endpoint returns a malformed response.
15-
// -32000 Error code will result in returning a 500 HTTP Status Code to the client.
16-
errCodeUnmarshaling = -32000
15+
// `jsonrpc.ResponseCodeBackendServerErr`, i.e. code -31002, will result in returning a 500 HTTP Status Code to the client.
16+
errCodeUnmarshaling = jsonrpc.ResponseCodeBackendServerErr
1717

1818
// errMsgUnmarshaling is the generic message returned to the user if the endpoint returns a malformed response.
1919
errMsgUnmarshaling = "the response returned by the endpoint is not a valid JSONRPC response"

qos/jsonrpc/errors.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ import (
55
)
66

77
const (
8-
ResponseCodeDefaultInternalErr = -32000 // JSON-RPC standard server error code; https://www.org/historical/json-rpc-2-0.html
9-
ResponseCodeDefaultBadRequest = -32600 // JSON-RPC error code indicating bad user request
8+
// DEV_NOTE: Intentionallly using Non-reserved JSONRPC error code for internal errors.
9+
// Allows distinguishing errors coming from PATH from backend server errors.
10+
// e.g.
11+
// -32000: indicates a backend server (ETH, Solana, etc.) returned the error.
12+
// -31001: indicates PATH consturcted the JSONRPC error response (e.g. if the endpoint payload failed to parse as a JSONRPC response)
13+
ResponseCodeDefaultInternalErr = -31001 // JSON-RPC server error code; https://www.jsonrpc.org/specification#error_object
14+
ResponseCodeBackendServerErr = -31002 // Indicates a backend server error: e.g. payload could not be parsed into a valid JSON-RPC response.
15+
16+
ResponseCodeDefaultBadRequest = -32600 // JSON-RPC error code indicating bad user request
1017
)
1118

1219
// NewErrResponseInternalErr creates a JSON-RPC error response when an internal error has occurred (e.g. reading HTTP request's body)
@@ -52,8 +59,8 @@ func NewErrResponseInvalidRequest(requestID ID, err error) Response {
5259
// - Marks error as retryable for safe client retry
5360
func NewErrResponseEmptyEndpointResponse(requestID ID) Response {
5461
return GetErrorResponse(
55-
requestID, // Use request's original ID if present
56-
-32000, // JSON-RPC standard server error code; https://www.jsonrpc.org/historical/json-rpc-2-0.html
62+
requestID, // Use request's original ID if present
63+
ResponseCodeBackendServerErr, // Indicates a backend server error caught by PATH.
5764
"Endpoint (data/service node error): Received an empty response. The endpoint will be dropped from the selection pool. Please try again.", // Error Response Message
5865
map[string]string{
5966
// Custom extension - not part of the official JSON-RPC spec
@@ -71,8 +78,8 @@ func NewErrResponseEmptyEndpointResponse(requestID ID) Response {
7178
// - Provides actionable message for clients
7279
func NewErrResponseNoEndpointResponse(requestID ID) Response {
7380
return GetErrorResponse(
74-
requestID, // Use request's original ID if present
75-
-32000, // JSON-RPC standard server error code; https://www.jsonrpc.org/historical/json-rpc-2-0.html
81+
requestID, // Use request's original ID if present
82+
ResponseCodeDefaultInternalErr, // Used to indicate an internal PATH/protocol error (e.g. endpoint timed out).
7683
"Failed to receive any response from endpoints. This could be due to network issues or high load. Please try again.", // Error Response Message
7784
map[string]string{
7885
// Custom extension - not part of the official JSON-RPC spec
@@ -87,8 +94,8 @@ func NewErrResponseNoEndpointResponse(requestID ID) Response {
8794
// Uses null ID per JSON-RPC spec for batch-level errors that cannot be correlated to specific requests.
8895
func NewErrResponseBatchMarshalFailure(err error) Response {
8996
return GetErrorResponse(
90-
ID{}, // Use null ID for batch-level failures per JSON-RPC spec
91-
-32000, // JSON-RPC standard server error code; https://www.jsonrpc.org/historical/json-rpc-2-0.html
97+
ID{}, // Use null ID for batch-level failures per JSON-RPC spec
98+
ResponseCodeDefaultInternalErr, // Used to indicate an internal PATH/protocol error: here it indicates failure to marshal a batch JSON-RPC response.
9299
fmt.Sprintf("Failed to marshal batch response: %s", err.Error()), // Error Message
93100
map[string]string{
94101
"error": err.Error(),

qos/solana/response_generic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515

1616
const (
1717
// errCodeUnmarshaling is set as the JSON-RPC response's error code if the endpoint returns a malformed response.
18-
// -32000 Error code will result in returning a 500 HTTP Status Code to the client.
19-
errCodeUnmarshaling = -32000
18+
// `jsonrpc.ResponseCodeBackendServerErr`, i.e. code -31002, will result in returning a 500 HTTP Status Code to the client.
19+
errCodeUnmarshaling = jsonrpc.ResponseCodeBackendServerErr
2020

2121
// errMsgUnmarshaling is the generic message returned to the user if the endpoint returns a malformed response.
2222
errMsgUnmarshaling = "the response returned by the endpoint is not a valid JSON-RPC response"

0 commit comments

Comments
 (0)