Skip to content
Merged
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: 11 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,19 @@ export class App<State> {
}

try {
let result: unknown;
if (handlers.length === 1 && handlers[0].length === 1) {
return handlers[0][0](ctx);
result = await handlers[0][0](ctx);
} else {
result = await runMiddlewares(handlers, ctx);
}
return await runMiddlewares(handlers, ctx);
if (!(result instanceof Response)) {
throw new Error(
`Expected a "Response" instance to be returned, but got: ${result}`,
);
}

return result;
} catch (err) {
if (err instanceof HttpError) {
if (err.status >= 500) {
Expand Down
15 changes: 15 additions & 0 deletions src/app_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,18 @@ Deno.test("FreshApp - support setting request init in ctx.render()", async () =>
expect(res.status).toEqual(416);
expect(res.headers.get("X-Foo")).toEqual("foo");
});

Deno.test("FreshApp - throw when middleware returns no response", async () => {
const app = new App<{ text: string }>()
.get(
"/",
// deno-lint-ignore no-explicit-any
(() => {}) as any,
);

const server = new FakeServer(app.handler());
const res = await server.get("/");
const text = await res.text();
expect(res.status).toEqual(500);
expect(text).toContain("Internal server error");
});
Loading