-
Notifications
You must be signed in to change notification settings - Fork 16
fix: harden HTTP/RPC response handling and gas-fee fallback #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b3642a
fix: surface HTTP status and tolerate error:null in RPC response hand…
sherifahmed990 517ddfe
fix: keep the fetched priority fee when eth_gasPrice is unsupported
sherifahmed990 8de4d1b
fix: guard sendJsonRpcRequest against scalar and null JSON payloads
sherifahmed990 aacefdc
Merge branch 'dev' into fix/transport-rpc-response-handling
sherifahmed990 22bf90e
fix: reject HTTP-failure responses that carry a JSON-RPC result
sherifahmed990 d59d6b7
fix: guard against non-object JSON-RPC error payloads in BaseRpcTrans…
sherifahmed990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Offline tests for HTTP/JSON-RPC error handling: error:null successes, | ||
| // non-JSON error bodies, and enveloped-but-empty responses must surface | ||
| // useful diagnostics instead of TypeErrors or JSON parse errors. | ||
|
|
||
| const ak = require("../../dist/index.cjs"); | ||
|
|
||
| function mockFetch(status, body, statusText = "") { | ||
| return async () => ({ | ||
| ok: status >= 200 && status < 300, | ||
| status, | ||
| statusText, | ||
| text: async () => (typeof body === "string" ? body : JSON.stringify(body)), | ||
| json: async () => JSON.parse(typeof body === "string" ? body : JSON.stringify(body)), | ||
| }); | ||
| } | ||
|
|
||
| function transportWith(status, body, statusText) { | ||
| return new ak.HttpTransport("https://example.test/rpc", { | ||
| fetch: mockFetch(status, body, statusText), | ||
| }); | ||
| } | ||
|
|
||
| describe("HttpTransport error handling", () => { | ||
| test("treats error:null alongside result as success", async () => { | ||
| const t = transportWith(200, { jsonrpc: "2.0", id: 1, result: "0x1", error: null }); | ||
| expect(await t.request({ method: "eth_chainId", params: [] })).toBe("0x1"); | ||
| }); | ||
|
|
||
| test("surfaces the HTTP status for a non-JSON error body", async () => { | ||
| const t = transportWith(502, "<html>Bad Gateway</html>", "Bad Gateway"); | ||
| await expect(t.request({ method: "eth_chainId", params: [] })).rejects.toMatchObject({ | ||
| name: "TransportRpcError", | ||
| message: expect.stringContaining("502"), | ||
| }); | ||
| }); | ||
|
|
||
| test("surfaces the HTTP status for a JSON error body without an envelope", async () => { | ||
| const t = transportWith(401, { message: "unauthorized" }, "Unauthorized"); | ||
| await expect(t.request({ method: "eth_chainId", params: [] })).rejects.toMatchObject({ | ||
| name: "TransportRpcError", | ||
| message: expect.stringContaining("401"), | ||
| }); | ||
| }); | ||
|
|
||
| test("still reports the server's JSON-RPC error from an HTTP failure", async () => { | ||
| const t = transportWith(429, { | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| error: { code: -32005, message: "rate limited" }, | ||
| }); | ||
| await expect(t.request({ method: "eth_chainId", params: [] })).rejects.toMatchObject({ | ||
| code: -32005, | ||
| message: "rate limited", | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("sendJsonRpcRequest error handling", () => { | ||
| const realFetch = globalThis.fetch; | ||
| afterEach(() => { | ||
| globalThis.fetch = realFetch; | ||
| }); | ||
|
|
||
| test("surfaces the HTTP status instead of crashing on a non-envelope body", async () => { | ||
| globalThis.fetch = mockFetch(401, { message: "unauthorized" }, "Unauthorized"); | ||
| await expect( | ||
| ak.sendJsonRpcRequest("https://example.test/rpc", "eth_chainId", []), | ||
| ).rejects.toMatchObject({ | ||
| name: "TransportRpcError", | ||
| message: expect.stringContaining("401"), | ||
| }); | ||
| }); | ||
|
|
||
| test("surfaces the HTTP status for a non-JSON body", async () => { | ||
| globalThis.fetch = mockFetch(502, "<html>Bad Gateway</html>", "Bad Gateway"); | ||
| await expect( | ||
| ak.sendJsonRpcRequest("https://example.test/rpc", "eth_chainId", []), | ||
| ).rejects.toMatchObject({ | ||
| name: "TransportRpcError", | ||
| message: expect.stringContaining("502"), | ||
| }); | ||
| }); | ||
|
|
||
| test.each([['"ok"'], ["null"], ["42"]])( | ||
| "reports a scalar/null JSON payload (%s) as malformed instead of a TypeError", | ||
| async (body) => { | ||
| globalThis.fetch = async () => ({ | ||
| ok: true, | ||
| status: 200, | ||
| statusText: "OK", | ||
| text: async () => body, | ||
| json: async () => JSON.parse(body), | ||
| }); | ||
| await expect( | ||
| ak.sendJsonRpcRequest("https://example.test/rpc", "eth_chainId", []), | ||
| ).rejects.toMatchObject({ | ||
| name: "TransportRpcError", | ||
| message: expect.stringContaining("malformed"), | ||
| }); | ||
| }, | ||
| ); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Validate JSON-RPC error fields at runtime.
Untrusted payloads such as
{"error":"bad"}or{"error":{}}can produce aTransportRpcErrorwith undefinedcode/messagerather than the required malformed-response error.src/transport/BaseRpcTransport.ts#L101-L103: validate thaterroris an object with numericcodeand stringmessagebefore destructuring.src/utils.ts#L572-L580: extend the existing object guard to validateerr.codeanderr.message; add malformed-error-envelope cases to the transport tests.📍 Affects 2 files
src/transport/BaseRpcTransport.ts#L101-L103(this comment)src/utils.ts#L572-L580🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sherifahmed990 this is valid imo