honojs/hono

Cannot finalize context on a service?

vickyRathee opened this issue · 1 comments

What version of Hono are you using?

4.5.11

What runtime/platform is your app running on?

Cloudflare pages

What steps can reproduce the bug?

[[server.ts]]

app.notFound(async (ctx) => {
   const { path, method } = ctx.req;
   await redirectHandler(ctx, path);
}

redirect.service.ts

async function redirectHandler(ctx: Context, path: string) {
  const redirect = await getRedirectFromTable(ctx.env, path);
  switch (redirect) {
    case '/home':
      return ctx.redirect('https://example.com/welcome', 302);

    case '/about':
      return ctx.redirect('https://example.com/about-us', 302);

    default:
      return ctx.json({message: 'No page found}, 404);
  }
}

image

What is the expected behavior?

Should redirect/return json as the context is finalized on the redirect.service.ts

What do you see instead?

an error

Additional information

No response

Hi @vickyRathee

You should return:

app.notFound(async (ctx) => {
  const { path, method } = ctx.req
  return await redirectHandler(ctx, path) // <===
})