Skip to content

Commit 788618f

Browse files
committed
fix: enhance error handling for middleware and add catch-all response for unmatched requests
1 parent c85d567 commit 788618f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

packages/plugins/plugin-hono-server/src/adapter.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class HonoHttpServer implements IHttpServer {
126126
});
127127

128128
const streamResponse = await streamPromise;
129-
return streamResponse ?? capturedResponse;
129+
return streamResponse ?? capturedResponse ?? c.json({ error: 'No response from handler' }, 500);
130130
};
131131
}
132132

@@ -148,16 +148,18 @@ export class HonoHttpServer implements IHttpServer {
148148

149149
use(pathOrHandler: string | Middleware, handler?: Middleware) {
150150
if (typeof pathOrHandler === 'string' && handler) {
151-
// Path based middleware
152-
// Hono middleware signature is different (c, next) => ...
153151
this.app.use(pathOrHandler, async (c, next) => {
154-
// Simplistic conversion
155-
await handler({} as any, {} as any, next);
152+
let nextCalled = false;
153+
const wrappedNext = () => { nextCalled = true; return next(); };
154+
await handler({} as any, {} as any, wrappedNext);
155+
if (!nextCalled) await next();
156156
});
157157
} else if (typeof pathOrHandler === 'function') {
158-
// Global middleware
159158
this.app.use('*', async (c, next) => {
160-
await pathOrHandler({} as any, {} as any, next);
159+
let nextCalled = false;
160+
const wrappedNext = () => { nextCalled = true; return next(); };
161+
await pathOrHandler({} as any, {} as any, wrappedNext);
162+
if (!nextCalled) await next();
161163
});
162164
}
163165
}

packages/plugins/plugin-hono-server/src/hono-plugin.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ export class HonoServerPlugin implements Plugin {
347347
}
348348
}
349349

350+
// Catch-all: ensure unmatched requests always get a proper Response
351+
// (prevents Hono "Context is not finalized" error)
352+
const rawAppForNotFound = this.server.getRawApp();
353+
if (typeof rawAppForNotFound.notFound === 'function') {
354+
rawAppForNotFound.notFound((c: any) => c.json({ error: 'Not found' }, 404));
355+
}
356+
350357
// Start server on kernel:ready hook
351358
ctx.hook('kernel:ready', async () => {
352359
// Register standard endpoints before starting to listen

0 commit comments

Comments
 (0)