Customizable koa exception (error) handling middleware
yarn add koa-exception-handler
const Koa = require('koa')
const koaException = require('koa-exception-handler')
const app = new Koa()
app.use(koaException(options))
Customize errorLogger
function.
- Type:
function
- Default:
console.error
Specifies whether it is a production environment that controls the behavior of the middleware when an error occurs on the server side.
- Type:
boolean
- Default:
process.env.NODE_ENV !== 'development'
When options.production
is true
, if an error occurs on the server, the options.fallback
function is executed without sending an error to the client. Of course, you can implement the behavior you expect in options.fallback
.
The handler that is executed when an error occurs on the client.
-
Type:
function
-
Default:
undefined
-
arguments:
- ctx
Koa context
- err
error
- Instance ofError
- ctx
-
example:
app.use(koaException({
clientErrorHandler: (ctx, err) => {
ctx.status = err.code
ctx.type = 'text/plain; charset=utf-8'
ctx.body = 'Client error: ' + err.toString()
}
}))
In a non-production environment, the handler executed when an error occurs on the server side.
- Type:
function
- Default:
undefined
- arguments:
- ctx
Koa context
- err
error
- Instance ofError
- ctx
Usage reference optioins.clientErrorHandler
.
Similar to optioins.serverErrorHandler
, except that optioins.fallback
is a handler that is executed in the production environment when an error occurs on the server side.
- Type:
function
- Default:
undefined
- arguments:
- ctx
Koa context
- err
error
- Instance ofError
- ctx