Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Showing with 54 additions and 148 deletions.
  1. +32 −51 lib/middleware/errorHandler.js
  2. +22 −97 test/errorHandler.test.js
View
83 lib/middleware/errorHandler.js
@@ -13,17 +13,15 @@ var utils = require('../utils')
, url = require('url')
, fs = require('fs');
+// environment
+
+var env = process.env.NODE_ENV || 'development';
+
/**
- * Flexible error handler, providing (_optional_) stack traces
+ * Development error handler, providing stack traces
* and error message responses for requests accepting text, html,
* or json.
*
- * Options:
- *
- * - `showStack`, `stack` respond with both the error message and stack trace. Defaults to `false`
- * - `showMessage`, `message`, respond with the exception message only. Defaults to `false`
- * - `dumpExceptions`, `dump`, dump exceptions to stderr (without terminating the process). Defaults to `false`
- *
* Text:
*
* By default, and when _text/plain_ is accepted a simple stack trace
@@ -38,59 +36,42 @@ var utils = require('../utils')
*
* When accepted connect will output a nice html stack trace.
*
- * @param {Object} options
* @return {Function}
* @api public
*/
-exports = module.exports = function errorHandler(options){
- options = options || {};
-
- // defaults
- var showStack = options.showStack || options.stack
- , showMessage = options.showMessage || options.message
- , dumpExceptions = options.dumpExceptions || options.dump
- , formatUrl = options.formatUrl;
-
+exports = module.exports = function errorHandler(){
return function errorHandler(err, req, res, next){
if (err.status) res.statusCode = err.status;
if (res.statusCode < 400) res.statusCode = 500;
- if (dumpExceptions) console.error(err.stack);
- if (showStack) {
- var accept = req.headers.accept || '';
- // html
- if (~accept.indexOf('html')) {
- fs.readFile(__dirname + '/../public/style.css', 'utf8', function(e, style){
- fs.readFile(__dirname + '/../public/error.html', 'utf8', function(e, html){
- var stack = (err.stack || '')
- .split('\n').slice(1)
- .map(function(v){ return '<li>' + v + '</li>'; }).join('');
- html = html
- .replace('{style}', style)
- .replace('{stack}', stack)
- .replace('{title}', exports.title)
- .replace('{statusCode}', res.statusCode)
- .replace(/\{error\}/g, utils.escape(err.toString()));
- res.setHeader('Content-Type', 'text/html');
- res.end(html);
- });
+ if ('test' != env) console.error(err.stack);
+ var accept = req.headers.accept || '';
+ // html
+ if (~accept.indexOf('html')) {
+ fs.readFile(__dirname + '/../public/style.css', 'utf8', function(e, style){
+ fs.readFile(__dirname + '/../public/error.html', 'utf8', function(e, html){
+ var stack = (err.stack || '')
+ .split('\n').slice(1)
+ .map(function(v){ return '<li>' + v + '</li>'; }).join('');
+ html = html
+ .replace('{style}', style)
+ .replace('{stack}', stack)
+ .replace('{title}', exports.title)
+ .replace('{statusCode}', res.statusCode)
+ .replace(/\{error\}/g, utils.escape(err.toString()));
+ res.setHeader('Content-Type', 'text/html');
+ res.end(html);
});
- // json
- } else if (~accept.indexOf('json')) {
- var json = JSON.stringify({ error: err });
- res.setHeader('Content-Type', 'application/json');
- res.end(json);
- // plain text
- } else {
- res.writeHead(res.statusCode, { 'Content-Type': 'text/plain' });
- res.end(err.stack);
- }
+ });
+ // json
+ } else if (~accept.indexOf('json')) {
+ var json = JSON.stringify({ error: err });
+ res.setHeader('Content-Type', 'application/json');
+ res.end(json);
+ // plain text
} else {
- var body = showMessage
- ? err.toString()
- : 'Internal Server Error';
- res.setHeader('Content-Type', 'text/plain');
- res.end(body);
+ res.writeHead(res.statusCode, { 'Content-Type': 'text/plain' });
+ res.end(err.stack);
}
};
};
View
119 test/errorHandler.test.js
@@ -10,7 +10,7 @@ var connect = require('../')
, create = require('./common').create;
module.exports = {
- 'test defaults with next(err)': function(){
+ 'test html': function(){
var app = create(
function(req, res, next){
next(new Error('keyboard cat!'));
@@ -19,107 +19,17 @@ module.exports = {
);
assert.response(app,
- { url: '/' },
- { body: 'Internal Server Error'
- , status: 500 });
- },
-
- 'test defaults with caught exception': function(){
- var app = create(
- function(req, res, next){
- throw new Error('keyboard cat!');
- },
- connect.errorHandler()
- );
-
- assert.response(app,
- { url: '/' },
- { body: 'Internal Server Error'
- , status: 500 });
- },
-
- 'test showMessage': function(){
- var app = create(
- function(req, res, next){
- next(new Error('keyboard cat!'));
- },
- connect.errorHandler({ showMessage: true })
- );
-
- assert.response(app,
- { url: '/' },
- { body: 'Error: keyboard cat!'
- , status: 500 });
- },
-
- 'test message': function(){
- var app = create(
- function(req, res, next){
- next(new Error('keyboard cat!'));
- },
- connect.errorHandler({ message: true })
- );
-
- assert.response(app,
- { url: '/' },
- { body: 'Error: keyboard cat!'
- , status: 500 });
- },
-
- 'test showStack': function(){
- var app = create(
- function(req, res, next){
- next(new Error('keyboard cat!'));
- },
- connect.errorHandler({ showStack: true })
- );
-
- assert.response(app,
- { url: '/' },
- function(res){
- var buf = '';
- res.body.should.include.string('Error: keyboard cat!');
- res.body.should.include.string('test/errorHandler.test.js');
- });
- },
-
- 'test stack': function(){
- var app = create(
- function(req, res, next){
- next(new Error('keyboard cat!'));
- },
- connect.errorHandler({ stack: true })
- );
-
- assert.response(app,
- { url: '/' },
- function(res){
- var buf = '';
- res.body.should.include.string('Error: keyboard cat!');
- res.body.should.include.string('test/errorHandler.test.js');
- });
- },
-
- 'test showStack html': function(){
- var app = create(
- function(req, res, next){
- next(new Error('keyboard cat!'));
- },
- connect.errorHandler({ stack: true })
- );
-
- assert.response(app,
{ url: '/', headers: { Accept: 'text/html, application/json' }},
{ status: 500
, headers: { 'Content-Type': 'text/html' }});
},
- 'test showStack json': function(){
+ 'test json': function(){
var app = create(
function(req, res, next){
next(new Error('keyboard cat!'));
},
- connect.errorHandler({ stack: true })
+ connect.errorHandler()
);
assert.response(app,
@@ -128,12 +38,12 @@ module.exports = {
, headers: { 'Content-Type': 'application/json' }});
},
- 'test showStack text': function(){
+ 'test text': function(){
var app = create(
function(req, res, next){
next(new Error('keyboard cat!'));
},
- connect.errorHandler({ stack: true })
+ connect.errorHandler()
);
assert.response(app,
@@ -141,14 +51,29 @@ module.exports = {
{ status: 500
, headers: { 'Content-Type': 'text/plain' }});
},
-
+
+ 'test err.status': function(){
+ var app = create(
+ function(req, res, next){
+ var err = new Error('oh no');
+ err.status = 501;
+ next(err);
+ },
+ connect.errorHandler()
+ );
+
+ assert.response(app,
+ { url: '/' },
+ { status: 501 });
+ },
+
'test custom response code': function(){
var app = create(
function(req, res, next){
res.statusCode = 501;
throw new Error('oh no');
},
- connect.errorHandler({ stack: true })
+ connect.errorHandler()
);
assert.response(app,
Please sign in to comment.
Something went wrong with that request. Please try again.