raiyansarker / honojs-exception-support-preview

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Added NestJS inspired exception handler.

Usage:

Basic use:

app.get('/dashboard', (c) => {
  if (!user) {
    throw new UnauthorizedException();
  }
  ...
});

Custom Exception

app.get('/custom', () => {
  throw new HttpException('Something is wrong', 500);
});

Advance use case:

app.onError((err, c) => {
  if (err instanceof Exception) {
    c.status(err.status as StatusCode);
    if (c.req.headers.get('accept')?.includes('application/json')) {
      return c.json({ message: err.message });
    }

    return c.text(err.message);
  }

  return c.text('Custom Error Message', 500);
});

About


Languages

Language:JavaScript 99.3%Language:TypeScript 0.7%