Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/create-with-winter-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ function serializeToResponse(
routeSpec: RouteSpec<any>,
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())
Expand Down
4 changes: 4 additions & 0 deletions src/lib/format-zod-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
36 changes: 36 additions & 0 deletions tests/errors/do-not-allow-raw-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
)
})