Skip to content

Commit 8ea2463

Browse files
committed
fix(json-rpc): update error mapping for HTTP status codes to include 3xx redirects
1 parent 63b09a3 commit 8ea2463

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/utils/json-rpc.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,10 @@ function mapHttpStatusToJsonRpcError(status: number): number {
344344
case 429:
345345
return SERVER_ERROR_RATE_LIMITED;
346346

347-
// Other 4xx errors → generic server error with negative offset
348347
default:
349-
if (status >= 400 && status < 500) {
348+
// 3xx redirects → generic server error (unusual but possible)
349+
// Other 4xx errors → generic server error
350+
if (status >= 300 && status < 500) {
350351
return SERVER_ERROR;
351352
}
352353
// 5xx and other errors → Internal error

test/json-rpc.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ describeMatrix("json-rpc", (t, { describe, it, expect }) => {
5353
serverError: () => {
5454
throw new HTTPError({ status: 500, message: "Server exploded" });
5555
},
56+
redirect: () => {
57+
throw new HTTPError({ status: 301, message: "Resource moved permanently" });
58+
},
5659
});
5760

5861
describe("success cases", () => {
@@ -628,5 +631,26 @@ describeMatrix("json-rpc", (t, { describe, it, expect }) => {
628631
},
629632
});
630633
});
634+
635+
it("should map 3xx redirects to SERVER_ERROR (-32000)", async () => {
636+
t.app.post("/json-rpc", eventHandler);
637+
const result = await t.fetch("/json-rpc", {
638+
method: "POST",
639+
body: JSON.stringify({
640+
jsonrpc: "2.0",
641+
method: "redirect",
642+
id: 1,
643+
}),
644+
});
645+
const json = await result.json();
646+
expect(json).toEqual({
647+
jsonrpc: "2.0",
648+
id: 1,
649+
error: {
650+
code: -32_000,
651+
message: "Resource moved permanently",
652+
},
653+
});
654+
});
631655
});
632656
});

0 commit comments

Comments
 (0)