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
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/middleware/with-response-object-check.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { ResponseValidationError } from "./http-exceptions.js"
import { BadRequestError } from "./http-exceptions.js"
import { Middleware } from "./types.js"
import { RouteSpec } from "src/types/route-spec.js"

export class RawJsonResponseError extends BadRequestError {
constructor() {
super("Use ctx.json({...}) instead of returning an object directly.")
}
}

export const withResponseObjectCheck: Middleware<
{ routeSpec: RouteSpec<any> },
{}
> = async (req, ctx, next) => {
const rawResponse = await next(req, ctx)

if (typeof rawResponse === "object" && !(rawResponse instanceof Response)) {
throw new Error(
"Use ctx.json({...}) instead of returning an object directly."
)
throw new RawJsonResponseError()
}

return rawResponse
Expand Down
14 changes: 11 additions & 3 deletions src/middleware/with-unhandled-exception-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ 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."
)
} else {
logger.warn(
"Caught unknown unhandled exception; consider adding a exception handling middleware to your global or route spec."
logger.error(e)
return Response.json(
{
message: e.message,
},
{
status: e.status ?? 500,
}
)
}

logger.warn(
"Caught unknown unhandled exception; consider adding a exception handling middleware to your global or route spec."
)
logger.error(e)

return new Response(null, {
Expand Down
Loading