diff --git a/src/create-with-winter-spec.ts b/src/create-with-winter-spec.ts index 229a28c..63cdd44 100644 --- a/src/create-with-winter-spec.ts +++ b/src/create-with-winter-spec.ts @@ -172,6 +172,12 @@ function serializeToResponse( routeSpec: RouteSpec, response: SerializableToResponse | Response ): Response { + if (typeof response === "symbol") { + throw new Error( + "Use ctx.json({...}) instead of returning a symbol directly." + ) + } + if (!shouldValidateResponse) { return "serializeToResponse" in response ? response.serializeToResponse(z.any()) diff --git a/src/lib/format-zod-error.ts b/src/lib/format-zod-error.ts index cf87295..5b69691 100644 --- a/src/lib/format-zod-error.ts +++ b/src/lib/format-zod-error.ts @@ -11,6 +11,10 @@ const zodIssueToString = (issue: ZodIssue) => { } export const formatZodError = (error: ZodError): string => { + if (!Array.isArray(error.issues)) { + return error instanceof Error ? error.message : String(error) + } + let message: string if (error.issues.length === 1) { const issue = error.issues[0] diff --git a/tests/errors/do-not-allow-raw-json.test.ts b/tests/errors/do-not-allow-raw-json.test.ts index 8682796..e6e6c91 100644 --- a/tests/errors/do-not-allow-raw-json.test.ts +++ b/tests/errors/do-not-allow-raw-json.test.ts @@ -37,3 +37,39 @@ test("should throw an error when responding with raw JSON", async (t) => { ) ) }) + +test("should throw an error when responding with a symbol", async (t) => { + const { axios } = await getTestRoute(t, { + globalSpec: { + authMiddleware: {}, + beforeAuthMiddleware: [ + async (req, ctx, next) => { + try { + return await next(req, ctx) + } catch (e: any) { + console.error(e) + return Response.json({ error: e.message }, { status: 500 }) + } + }, + ], + }, + routeSpec: { + methods: ["GET"], + jsonBody: z.any(), + jsonResponse: z.any(), + }, + routePath: "/", + routeFn: (req, ctx) => { + return Symbol("invalid-response") as any + }, + }) + + const { data } = await axios.get("/", { + validateStatus: () => true, + }) + t.true( + data.error.includes( + "Use ctx.json({...}) instead of returning a symbol directly" + ) + ) +})