This is a fork of the original koa-error package rewritten in TypeScript and reduced to a single dependency.
Error response middleware for Koa supporting:
- text
- json
- html
$ npm install koa-error-middleware
import Koa from 'koa';
import KoaError from 'koa-error-middleware';
const app = new Koa();
app.use(KoaError());
app.listen(3000);
See example/index.ts for a full example.
-
template
: content of error template with placeholders, default: error.html -
env
force a NODE_ENV, default:development
-
accepts
mimetypes passed to ctx.accepts, default:['html', 'text', 'json']
The following changes were made to the original implementation of koa-error.
-
template
: koa-error expected a path to a template file which it would then load from the file system. This package expects the contents of the template file. The calling package can decide whether to load the template file from the file system or embed it directly in its code. -
engine
: koa-error supported multiple template engines via consolidate. This option and dependency has been removed in favor of supporting only one template engine lodash.template.
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
For more information see the Lodash.template documentation.
app.use(KoaError());
<!DOCTYPE html>
<html>
<head>
<title>Error - <%- status %>
</title>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
</head>
<body>
<div id="error">
<h1>Error</h1>
<p>Looks like something broke!</p>
<% if (env==='development' ) { %>
<h2>Original error</h2>
<pre>
<code>
<%- JSON.stringify(originalError, null, ' ') %>
</code>
</pre>
<h2>Message:</h2>
<pre>
<code>
<%- error %>
</code>
</pre>
<h2>Stack:</h2>
<pre>
<code>
<%- stack %>
</code>
</pre>
<% } %>
</div>
</body>
</html>
MIT