Skip to content

Commit d280e88

Browse files
authored
Merge branch 'v1.3.x' into backport-131-to-v1.3.x
2 parents d002087 + f1c64e6 commit d280e88

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

packages/better-call/src/router.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,73 @@ describe("error handling", () => {
620620
expect(body.message).toBe("Resource not found");
621621
});
622622

623+
describe("invalid JSON body", () => {
624+
it("should return 400 for empty JSON body", async () => {
625+
const endpoint = createEndpoint(
626+
"/post",
627+
{
628+
method: "POST",
629+
},
630+
async (c) => {
631+
return c.body;
632+
},
633+
);
634+
const router = createRouter({ endpoint });
635+
const request = new Request("http://localhost/post", {
636+
method: "POST",
637+
headers: { "Content-Type": "application/json" },
638+
body: "",
639+
});
640+
const response = await router.handler(request);
641+
expect(response.status).toBe(400);
642+
const body = await response.json();
643+
expect(body.code).toBe("BAD_REQUEST");
644+
expect(body.message).toBe("Invalid JSON in request body");
645+
});
646+
647+
it("should return 400 for malformed JSON body", async () => {
648+
const endpoint = createEndpoint(
649+
"/post",
650+
{
651+
method: "POST",
652+
},
653+
async (c) => {
654+
return c.body;
655+
},
656+
);
657+
const router = createRouter({ endpoint });
658+
const request = new Request("http://localhost/post", {
659+
method: "POST",
660+
headers: { "Content-Type": "application/json" },
661+
body: "not json",
662+
});
663+
const response = await router.handler(request);
664+
expect(response.status).toBe(400);
665+
const body = await response.json();
666+
expect(body.code).toBe("BAD_REQUEST");
667+
});
668+
669+
it("should return 200 for valid JSON body", async () => {
670+
const endpoint = createEndpoint(
671+
"/post",
672+
{
673+
method: "POST",
674+
},
675+
async (c) => {
676+
return c.body;
677+
},
678+
);
679+
const router = createRouter({ endpoint });
680+
const request = new Request("http://localhost/post", {
681+
method: "POST",
682+
headers: { "Content-Type": "application/json" },
683+
body: "{}",
684+
});
685+
const response = await router.handler(request);
686+
expect(response.status).toBe(200);
687+
});
688+
});
689+
623690
describe("allowedMediaTypes", () => {
624691
it("should allow requests with allowed media type at router level", async () => {
625692
const endpoint = createEndpoint(

packages/better-call/src/utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ export async function getBody(request: Request, allowedMediaTypes?: string[]) {
3939
}
4040

4141
if (jsonContentTypeRegex.test(normalizedContentType)) {
42-
return await request.json();
42+
try {
43+
return await request.json();
44+
} catch (e) {
45+
if (e instanceof SyntaxError) {
46+
throw new APIError(400, {
47+
message: "Invalid JSON in request body",
48+
code: "BAD_REQUEST",
49+
});
50+
}
51+
throw e;
52+
}
4353
}
4454

4555
if (normalizedContentType.includes("application/x-www-form-urlencoded")) {

0 commit comments

Comments
 (0)