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/middleware/http-exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,9 @@ export class ResponseValidationError extends WinterSpecMiddlewareError {
super(formatZodError(error), 500)
}
}

export class InvalidRouteReturnError extends WinterSpecMiddlewareError {
constructor(message: string) {
super(message, 500)
}
}
4 changes: 2 additions & 2 deletions src/middleware/with-response-object-check.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResponseValidationError } from "./http-exceptions.js"
import { InvalidRouteReturnError } from "./http-exceptions.js"
import { Middleware } from "./types.js"
import { RouteSpec } from "src/types/route-spec.js"

Expand All @@ -9,7 +9,7 @@ export const withResponseObjectCheck: Middleware<
const rawResponse = await next(req, ctx)

if (typeof rawResponse === "object" && !(rawResponse instanceof Response)) {
throw new Error(
throw new InvalidRouteReturnError(
"Use ctx.json({...}) instead of returning an object directly."
)
}
Expand Down
3 changes: 3 additions & 0 deletions src/middleware/with-unhandled-exception-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const withUnhandledExceptionHandling: Middleware<{
logger.warn(
"Caught unhandled HTTP exception thrown by WinterSpec provided middleware. Consider adding createWithDefaultExceptionHandling middleware to your global or route spec."
)
return new Response(e?.message ?? null, {
status: e?.status ?? 500,
})
} else {
logger.warn(
"Caught unknown unhandled exception; consider adding a exception handling middleware to your global or route spec."
Expand Down
32 changes: 32 additions & 0 deletions tests/errors/raw-object-default-error-message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import test from "ava"
import { z } from "zod"
import { getTestRoute } from "tests/fixtures/get-test-route.js"

test("returning a raw object surfaces ctx.json guidance by default", async (t) => {
const { axios } = await getTestRoute(t, {
globalSpec: {
authMiddleware: {},
},
routeSpec: {
methods: ["GET"],
jsonBody: z.any(),
jsonResponse: z.any(),
},
routePath: "/",
routeFn: () => {
return { foo: "bar" } as any
},
})

const response = await axios.get("/", {
validateStatus: () => true,
responseType: "text",
})

t.is(response.status, 500)
t.true(
String(response.data).includes(
"Use ctx.json({...}) instead of returning an object directly"
)
)
})
Loading