Skip to content

Commit

Permalink
fix(app): throw error when trying to return function or symbol as res…
Browse files Browse the repository at this point in the history
…ponse
  • Loading branch information
pi0 committed Aug 1, 2023
1 parent c5bbee9 commit 6e58103
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,9 @@ function handleHandlerResponse(event: H3Event, val: any, jsonSpace?: number) {
return send(event, val.toString(), MIMES.json);
}

return false;
// Symbol or Function (undefined is already handled by consumer)
throw createError({
statusCode: 500,
statusMessage: `[h3] Cannot send ${valType} as response.`,
});
}
27 changes: 27 additions & 0 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ describe("app", () => {
expect(res.text).toBe("9007199254740991");
});

it("throws error when returning symbol or function", async () => {
app.use(
"/fn",
eventHandler(() => {
return function foo() {};
})
);
app.use(
"/symbol",
eventHandler(() => {
return Symbol.for("foo");
})
);

const resFn = await request.get("/fn");
expect(resFn.status).toBe(500);
expect(resFn.body.statusMessage).toBe(
"[h3] Cannot send function as response."
);

const resSymbol = await request.get("/symbol");
expect(resSymbol.status).toBe(500);
expect(resSymbol.body.statusMessage).toBe(
"[h3] Cannot send symbol as response."
);
});

it("can return Response directly", async () => {
app.use(
"/",
Expand Down

0 comments on commit 6e58103

Please sign in to comment.