From 6e581038ebc9462963c676b07d35c73cffdc3867 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 1 Aug 2023 14:10:49 +0200 Subject: [PATCH] fix(app): throw error when trying to return function or symbol as response --- src/app.ts | 6 +++++- test/app.test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 4d827a6e..53fabe75 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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.`, + }); } diff --git a/test/app.test.ts b/test/app.test.ts index 880c09db..837fd6a8 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -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( "/",