Error response middleware for koa supporting:
-
text
-
html
-
json
Adapted from koa-error to use EJS as view engine
$ npm install koa-error-ejs --save
and then copy the default error view to your views folder
$ cp node_modules/koa-error-ejs/error.html views/
setup in your application
var app= require('koa')();
var error= require('koa-error-ejs');
app.use(error());
view
String path to template written with ejs. Defaults to {view.root}/errorlayout
String|Boolean layout to use on error view, or false if none. Defaults to false.custom
Object specific view for a status code, for example: {404: 'error/not-found'}. Defaults to {}
Example:
var app= require('koa')();
var error= require('koa-error-ejs');
app.use(error({
view: 'error/error', //(optional)
layout: 'layouts/error', //(optional)
custom: { //(optional)
401: 'error/login',
403: 'error/forbidden',
404: 'error/not-found',
500: 'error/sorry'
}
}));
By using the view
option you can override the bland default template,
with the following available local variables:
env
ctx
request
response
error
stack
status
code
Here's an example:
<!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'){ %>
<pre>
<code>
<%-stack%>
</code>
</pre>
<% } %>
</div>
</body>
</html>
MIT