Skip to content

Commit 6e58103

Browse files
committed
fix(app): throw error when trying to return function or symbol as response
1 parent c5bbee9 commit 6e58103

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,9 @@ function handleHandlerResponse(event: H3Event, val: any, jsonSpace?: number) {
236236
return send(event, val.toString(), MIMES.json);
237237
}
238238

239-
return false;
239+
// Symbol or Function (undefined is already handled by consumer)
240+
throw createError({
241+
statusCode: 500,
242+
statusMessage: `[h3] Cannot send ${valType} as response.`,
243+
});
240244
}

test/app.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ describe("app", () => {
3939
expect(res.text).toBe("9007199254740991");
4040
});
4141

42+
it("throws error when returning symbol or function", async () => {
43+
app.use(
44+
"/fn",
45+
eventHandler(() => {
46+
return function foo() {};
47+
})
48+
);
49+
app.use(
50+
"/symbol",
51+
eventHandler(() => {
52+
return Symbol.for("foo");
53+
})
54+
);
55+
56+
const resFn = await request.get("/fn");
57+
expect(resFn.status).toBe(500);
58+
expect(resFn.body.statusMessage).toBe(
59+
"[h3] Cannot send function as response."
60+
);
61+
62+
const resSymbol = await request.get("/symbol");
63+
expect(resSymbol.status).toBe(500);
64+
expect(resSymbol.body.statusMessage).toBe(
65+
"[h3] Cannot send symbol as response."
66+
);
67+
});
68+
4269
it("can return Response directly", async () => {
4370
app.use(
4471
"/",

0 commit comments

Comments
 (0)