# koa-error

> Error reponses (text, json, html) for koa

Latest version **3.2.0** (published 2018-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-error
pnpm add koa-error
yarn add koa-error
bun add koa-error
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2018-04-11 |
| First published | 2013-12-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 103 |
| Maintainers | coderhaoxin, eduardorfs, jonathanong, jongleberry, mattmueller, tjholowaychuk |
| Keywords | koa, middleware, error, page |

## Links

- npm: https://www.npmjs.com/package/koa-error
- Repository: https://github.com/koajs/error
- Homepage: https://github.com/koajs/error#readme
- Issues: https://github.com/koajs/error/issues
- npm.io page: https://npm.io/package/koa-error

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [consolidate](https://npm.io/package/consolidate.md) ^0.15.0

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 3.2.0 (latest) — 2018-04-11
- 3.1.1 — 2018-01-11
- 3.1.0 — 2017-07-24
- 3.0.2 — 2017-07-24
- 3.0.1 — 2017-05-15
- 3.0.0 — 2017-03-01
- 2.1.0 — 2016-05-26
- 2.0.0 — 2016-04-14
- 1.1.3 — 2014-11-29
- 1.1.2 — 2014-05-08
- 1.1.1 — 2014-01-11
- 1.1.0 — 2013-12-25
- 1.0.0 — 2013-12-22

## README

# koa-error

Error response middleware for koa supporting:

- [x] text
- [x] json
- [x] html

## Installation

```bash
# npm
$ npm install koa-error
# yarn
$ yarn add 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/ladjs/consolidate), 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
<!DOCTYPE html>
<html>
  <head>
    <title>Error - {{status}}</title>
  </head>
  <body>
    <div id="error">
      <h1>Error</h1>
      <p>Looks like something broke!</p>
      {% if env == 'development' %}
      <h2>Message:</h2>
      <pre>
          <code>
            {{error}}
          </code>
        </pre>
      <h2>Stack:</h2>
      <pre>
          <code>
            {{stack}}
          </code>
        </pre>
      {% endif %}
    </div>
  </body>
</html>
```

#### Custom filters, use macro,block in Nunjucks

koa-error engine tool base on [consolidate](https://github.com/ladjs/consolidate), you can also set consolidate options

> [more-nunjucks-options](https://github.com/ladjs/consolidate/blob/master/lib/consolidate.js#L1436-L1488) > `app.js`:

```js
//...
const app = new Koa();
const nunjucks = require("nunjucks");
const nunjucksEnv = new nunjucks.Environment(
  new nunjucks.FileSystemLoader(path.join(__dirname, "tpl"))
);

// add filters
const filters = require("./filters");
for (let [k, v] of Object.entries(filters)) {
  nunjucksEnv.addFilter(k, v);
}

//...
app.use(
  koaError({
    //...
    template: path.join(__dirname, "tpl/error.html"),
    options: {
      nunjucksEnv, // custom nunjucks env
    },
  })
);
```

`filters.js`:

```js
module.exports = {
    // define filters function here
    stringify(..args){
        return JSON.stringify(...args);
    }
    //...
};
```

`tpl/error.html`:

```html
<!DOCTYPE html>
<html>
  <head>
    {% include "./com.html" %}
  </head>
  <body>
    <p>{{ request | stringify }}</p> {# use filters here #}
    <!-- ... -->
  </body>
</htm>
```

`tpl/com.html`:

```html
<link rel="stylesheet" href="/css/normalize.css" />
```

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/koa-error · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
