@gem-mine/exception
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

Exception middleware for koa

install

npm i @gem-mine/exception -S

useage

正常使用 koa 的中间件:

app.use(exception({
  /**
   * 是否开启调试模式,显示更丰富的错误堆栈等信息
   */
  debug?: boolean
  /**
   * 404页面,当非 json 请求遇到 404 时的返回页面
   */
  notFoundPage?: string
  /**
   * 写入日志处理
   */
  logger?: function(data:Exception):void
  /**
   * 自定义异常处理
   */
  custom?: function(data:Exception):void
}))

例子:

export default exception({
  debug: process.env.NODE_ENV !== 'production'
})
export default exception({
  debug: process.env.NODE_ENV !== 'production',
  notFoundPage: '404.html
})
export default exception({
  debug: process.env.NODE_ENV !== 'production',
  async custom(data, ctx) {
    if (ctx.status === 404 && !ctx.acceptJSON) {
      return await ctx.render('404.html')
    }
    ctx.body = data
  }
})

Package Sidebar

Install

npm i @gem-mine/exception

Weekly Downloads

1

Version

0.2.0

License

MIT

Unpacked Size

6.68 kB

Total Files

4

Last publish

Collaborators

  • gylllll
  • janya
  • githoniel
  • amazebird
  • mraiguo
  • guoyh
  • wengzp
  • caolvchong