Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/error
Error response middleware (text, json, html)
https://github.com/koajs/error
Last synced: 2 months ago
JSON representation
Error response middleware (text, json, html)
- Host: GitHub
- URL: https://github.com/koajs/error
- Owner: koajs
- Created: 2013-12-22T17:47:54.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2020-09-10T15:27:38.000Z (over 4 years ago)
- Last Synced: 2024-04-14T13:08:39.411Z (8 months ago)
- Language: JavaScript
- Size: 36.1 KB
- Stars: 103
- Watchers: 5
- Forks: 36
- Open Issues: 6
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
- awesome-koa - error - Error response middleware (text, json, html) (Middleware)
- awesome-koa - koa-error - 错误响应中间件(支持返回text, json, html模板引擎). ![](https://img.shields.io/github/stars/koajs/error.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-error.svg?style=flat-square) (仓库 / 中间件)
README
# koa-error
Error response middleware for koa supporting:
- text
- json
- html## Installation
```js
$ npm install koa-error
```## Options
- `template` path to template written with your template engine, default: `./error.html`
- `engine` template engine name passed to [consolidate](https://github.com/tj/consolidate.js), default: `lodash`
- `cache` cached compiled functions, default: `NODE_ENV != 'development'`
- `env` force a NODE_ENV, default: `development`
- `accepts` mimetypes passed to [ctx.accepts](https://github.com/koajs/koa/blob/master/docs/api/request.md#requestacceptstypes), default: `[ 'html', 'text', 'json' ]`## Custom templates
By using the `template` option you can override the bland default template,
with the following available local variables:- `env`
- `ctx`
- `request`
- `response`
- `error`
- `stack`
- `status`
- `code`Here are some examples:
### Pug (formerly jade)
```js
app.use(error({
engine: 'pug',
template: __dirname + '/error.pug'
}));
``````jade
doctype html
html
head
title= 'Error - ' + status
body
#error
h1 Error
p Looks like something broke!
if env == 'development'
h2 Message:
pre: code= error
h2 Stack:
pre: code= stack
```### Nunjucks
```js
app.use(error({
engine: 'nunjucks',
template: __dirname + '/error.njk'
}));
``````html
Error - {{status}}
Error
Looks like something broke!
{% if env == 'development' %}
Message:
{{error}}
Stack:
{{stack}}
{% endif %}
```
## License
MIT