pax_global_header00006660000000000000000000000064134647166370014533gustar00rootroot0000000000000052 comment=03c22a6082375f755e02397654384c49f7c430ce errorhandler-1.5.1/000077500000000000000000000000001346471663700142265ustar00rootroot00000000000000errorhandler-1.5.1/.eslintignore000066400000000000000000000000261346471663700167270ustar00rootroot00000000000000coverage node_modules errorhandler-1.5.1/.eslintrc.yml000066400000000000000000000000351346471663700166500ustar00rootroot00000000000000root: true extends: standard errorhandler-1.5.1/.gitignore000066400000000000000000000000701346471663700162130ustar00rootroot00000000000000coverage/ node_modules/ npm-debug.log package-lock.json errorhandler-1.5.1/.travis.yml000066400000000000000000000055271346471663700163500ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.9" - "5.12" - "6.17" - "7.10" - "8.16" - "9.11" - "10.15" - "11.15" - "12.2" sudo: false cache: directories: - node_modules before_install: - | # Setup utility functions function node_version_lt () { [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v "${1}")" ]] } function npm_module_installed () { npm -lsp ls | grep -Fq "$(pwd)/node_modules/${1}:${1}@" } function npm_remove_module_re () { node -e ' fs = require("fs"); p = JSON.parse(fs.readFileSync("package.json", "utf8")); r = RegExp(process.argv[1]); for (k in p.devDependencies) { if (r.test(k)) delete p.devDependencies[k]; } fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n"); ' "$@" } function npm_use_module () { node -e ' fs = require("fs"); p = JSON.parse(fs.readFileSync("package.json", "utf8")); p.devDependencies[process.argv[1]] = process.argv[2]; fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n"); ' "$@" } function v () { tr '.' '\n' <<< "${1}" \ | awk '{ printf "%03d", $0 }' \ | sed 's/^0*//' } # Configure npm - | # Skip updating shrinkwrap / lock npm config set shrinkwrap false # Setup Node.js version-specific dependencies - | # Configure eslint for linting if node_version_lt '6.0'; then npm_remove_module_re '^eslint(-|$)' fi - | # Configure istanbul for coverage if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$' fi - | # Configure mocha for testing if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3' elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3' elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0' fi - | # Configure supertest for http calls if node_version_lt '0.10'; then npm_use_module 'supertest' '1.1.0' elif node_version_lt '4.0' ; then npm_use_module 'supertest' '2.0.0' elif node_version_lt '6.0' ; then npm_use_module 'supertest' '3.4.2' fi # Update Node.js modules - | # Prune & rebuild node_modules if [[ -d node_modules ]]; then npm prune npm rebuild fi before_scrpt: - | # Contents of node_modules npm -s ls ||: script: - | # Run test script, depending on istanbul install if npm_module_installed 'istanbul'; then npm run-script test-travis else npm test fi - | # Run linting, if eslint exists if npm_module_installed 'eslint'; then npm run-script lint fi after_script: - | # Upload coverage to coveralls if exists if [[ -e ./coverage/lcov.info ]]; then npm install --save-dev coveralls@2 coveralls < ./coverage/lcov.info fi errorhandler-1.5.1/HISTORY.md000066400000000000000000000063441346471663700157200ustar00rootroot000000000000001.5.1 / 2019-05-08 ================== * deps: accepts@~1.3.7 - deps: mime-types@~2.1.24 - deps: negotiator@0.6.2 1.5.0 / 2016-11-15 ================== * Pretty print JSON error response * deps: accepts@~1.3.3 - deps: mime-types@~2.1.11 - deps: negotiator@0.6.1 * perf: front-load HTML template and stylesheet at middleware construction * perf: only load template and stylesheet once * perf: resolve file paths at start up 1.4.3 / 2016-01-17 ================== * deps: accepts@~1.3.0 - deps: mime-types@~2.1.7 - deps: negotiator@0.6.0 * deps: escape-html@~1.0.3 - perf: enable strict mode - perf: optimize string replacement - perf: use faster string coercion 1.4.2 / 2015-07-30 ================== * deps: accepts@~1.2.12 - deps: mime-types@~2.1.4 1.4.1 / 2015-07-05 ================== * deps: accepts@~1.2.10 - deps: mime-types@~2.1.2 1.4.0 / 2015-06-10 ================== * Add charset to the `Content-Type` header * Support `statusCode` property on `Error` objects * deps: accepts@~1.2.9 - deps: mime-types@~2.1.1 - deps: negotiator@0.5.3 - perf: avoid argument reassignment & argument slice - perf: avoid negotiator recursive construction - perf: enable strict mode - perf: remove unnecessary bitwise operator * deps: escape-html@1.0.2 1.3.6 / 2015-05-14 ================== * deps: accepts@~1.2.7 - deps: mime-types@~2.0.11 - deps: negotiator@0.5.3 1.3.5 / 2015-03-14 ================== * deps: accepts@~1.2.5 - deps: mime-types@~2.0.10 1.3.4 / 2015-02-15 ================== * deps: accepts@~1.2.4 - deps: mime-types@~2.0.9 - deps: negotiator@0.5.1 1.3.3 / 2015-01-31 ================== * deps: accepts@~1.2.3 - deps: mime-types@~2.0.8 1.3.2 / 2015-01-01 ================== * Fix heading content to not include stack 1.3.1 / 2014-12-31 ================== * deps: accepts@~1.2.2 - deps: mime-types@~2.0.7 - deps: negotiator@0.5.0 1.3.0 / 2014-11-22 ================== * Add `log` option 1.2.4 / 2015-01-01 ================== * Fix heading content to not include stack 1.2.3 / 2014-11-21 ================== * deps: accepts@~1.1.3 - deps: mime-types@~2.0.3 1.2.2 / 2014-10-15 ================== * deps: accepts@~1.1.2 - Fix error when media type has invalid parameter - deps: negotiator@0.4.9 1.2.1 / 2014-10-12 ================== * deps: accepts@~1.1.1 - deps: mime-types@~2.0.2 - deps: negotiator@0.4.8 1.2.0 / 2014-09-02 ================== * Display error using `util.inspect` if no other representation * deps: accepts@~1.1.0 1.1.1 / 2014-06-20 ================== * deps: accepts@~1.0.4 - use `mime-types` 1.1.0 / 2014-06-16 ================== * Display error on console formatted like `throw` * Escape HTML with `escape-html` module * Escape HTML in stack trace * Escape HTML in title * Fix up edge cases with error sent in response * Set `X-Content-Type-Options: nosniff` header * Use accepts for negotiation 1.0.2 / 2014-06-05 ================== * Pass on errors from reading error files 1.0.1 / 2014-04-29 ================== * Clean up error CSS * Do not respond after headers sent 1.0.0 / 2014-03-03 ================== * Genesis from `connect` errorhandler-1.5.1/LICENSE000066400000000000000000000022241346471663700152330ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014 Jonathan Ong Copyright (c) 2014-2015 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. errorhandler-1.5.1/README.md000066400000000000000000000076261346471663700155200ustar00rootroot00000000000000# errorhandler [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Test Coverage][coveralls-image]][coveralls-url] Development-only error handler middleware. This middleware is only intended to be used in a development environment, as the _full error stack traces and internal details of any object passed to this module_ will be sent back to the client when an error occurs. When an object is provided to Express as an error, this module will display as much about this object as possible, and will do so by using content negotiation for the response between HTML, JSON, and plain text. * When the object is a standard `Error` object, the string provided by the `stack` property will be returned in HTML/text responses. * When the object is a non-`Error` object, the result of [util.inspect](https://nodejs.org/api/util.html#util_util_inspect_object_options) will be returned in HTML/text responses. * For JSON responses, the result will be an object with all enumerable properties from the object in the response. ## Install This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): ```sh $ npm install errorhandler ``` ## API ```js var errorhandler = require('errorhandler') ``` ### errorhandler(options) Create new middleware to handle errors and respond with content negotiation. #### Options Error handler accepts these properties in the options object. ##### log Provide a function to be called with the error and a string representation of the error. Can be used to write the error to any desired location, or set to `false` to only send the error back in the response. Called as `log(err, str, req, res)` where `err` is the `Error` object, `str` is a string representation of the error, `req` is the request object and `res` is the response object (note, this function is invoked _after_ the response has been written). The default value for this option is `true` unless `process.env.NODE_ENV === 'test'`. Possible values: * `true`: Log errors using `console.error(str)`. * `false`: Only send the error back in the response. * A function: pass the error to a function for handling. ## Examples ### Simple example Basic example of adding this middleware as the error handler only in development with `connect` (`express` also can be used in this example). ```js var connect = require('connect') var errorhandler = require('errorhandler') var app = connect() if (process.env.NODE_ENV === 'development') { // only use in development app.use(errorhandler()) } ``` ### Custom output location Sometimes you may want to output the errors to a different location than STDERR during development, like a system notification, for example. ```js var connect = require('connect') var errorhandler = require('errorhandler') var notifier = require('node-notifier') var app = connect() if (process.env.NODE_ENV === 'development') { // only use in development app.use(errorhandler({ log: errorNotification })) } function errorNotification (err, str, req) { var title = 'Error in ' + req.method + ' ' + req.url notifier.notify({ title: title, message: str }) } ``` ## License [MIT](LICENSE) [coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/errorhandler/master [coveralls-url]: https://coveralls.io/r/expressjs/errorhandler?branch=master [npm-downloads-image]: https://badgen.net/npm/dm/errorhandler [npm-url]: https://npmjs.org/package/errorhandler [npm-version-image]: https://badgen.net/npm/v/errorhandler [travis-image]: https://badgen.net/travis/expressjs/errorhandler/master [travis-url]: https://travis-ci.org/expressjs/errorhandler errorhandler-1.5.1/index.js000066400000000000000000000107261346471663700157010ustar00rootroot00000000000000/*! * errorhandler * Copyright(c) 2010 Sencha Inc. * Copyright(c) 2011 TJ Holowaychuk * Copyright(c) 2014 Jonathan Ong * Copyright(c) 2014-2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var accepts = require('accepts') var escapeHtml = require('escape-html') var fs = require('fs') var path = require('path') var util = require('util') /** * Module variables. * @private */ var DOUBLE_SPACE_REGEXP = /\x20{2}/g var NEW_LINE_REGEXP = /\n/g var STYLESHEET = fs.readFileSync(path.join(__dirname, '/public/style.css'), 'utf8') var TEMPLATE = fs.readFileSync(path.join(__dirname, '/public/error.html'), 'utf8') var inspect = util.inspect var toString = Object.prototype.toString /* istanbul ignore next */ var defer = typeof setImmediate === 'function' ? setImmediate : function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) } /** * Error handler: * * Development error handler, providing stack traces * and error message responses for requests accepting text, html, * or json. * * Text: * * By default, and when _text/plain_ is accepted a simple stack trace * or error message will be returned. * * JSON: * * When _application/json_ is accepted, connect will respond with * an object in the form of `{ "error": error }`. * * HTML: * * When accepted connect will output a nice html stack trace. * * @return {Function} * @api public */ exports = module.exports = function errorHandler (options) { // get environment var env = process.env.NODE_ENV || 'development' // get options var opts = options || {} // get log option var log = opts.log === undefined ? env !== 'test' : opts.log if (typeof log !== 'function' && typeof log !== 'boolean') { throw new TypeError('option log must be function or boolean') } // default logging using console.error if (log === true) { log = logerror } return function errorHandler (err, req, res, next) { // respect err.statusCode if (err.statusCode) { res.statusCode = err.statusCode } // respect err.status if (err.status) { res.statusCode = err.status } // default status code to 500 if (res.statusCode < 400) { res.statusCode = 500 } // log the error var str = stringify(err) if (log) { defer(log, err, str, req, res) } // cannot actually respond if (res._header) { return req.socket.destroy() } // negotiate var accept = accepts(req) var type = accept.type('html', 'json', 'text') // Security header for content sniffing res.setHeader('X-Content-Type-Options', 'nosniff') // html if (type === 'html') { var isInspect = !err.stack && String(err) === toString.call(err) var errorHtml = !isInspect ? escapeHtmlBlock(str.split('\n', 1)[0] || 'Error') : 'Error' var stack = !isInspect ? String(str).split('\n').slice(1) : [str] var stackHtml = stack .map(function (v) { return '
  • ' + escapeHtmlBlock(v) + '
  • ' }) .join('') var body = TEMPLATE .replace('{style}', STYLESHEET) .replace('{stack}', stackHtml) .replace('{title}', escapeHtml(exports.title)) .replace('{statusCode}', res.statusCode) .replace(/\{error\}/g, errorHtml) res.setHeader('Content-Type', 'text/html; charset=utf-8') res.end(body) // json } else if (type === 'json') { var error = { message: err.message, stack: err.stack } for (var prop in err) error[prop] = err[prop] var json = JSON.stringify({ error: error }, null, 2) res.setHeader('Content-Type', 'application/json; charset=utf-8') res.end(json) // plain text } else { res.setHeader('Content-Type', 'text/plain; charset=utf-8') res.end(str) } } } /** * Template title, framework authors may override this value. */ exports.title = 'Connect' /** * Escape a block of HTML, preserving whitespace. * @api private */ function escapeHtmlBlock (str) { return escapeHtml(str) .replace(DOUBLE_SPACE_REGEXP, '  ') .replace(NEW_LINE_REGEXP, '
    ') } /** * Stringify a value. * @api private */ function stringify (val) { var stack = val.stack if (stack) { return String(stack) } var str = String(val) return str === toString.call(val) ? inspect(val) : str } /** * Log error to console. * @api private */ function logerror (err, str) { console.error(str || err.stack) } errorhandler-1.5.1/package.json000066400000000000000000000023561346471663700165220ustar00rootroot00000000000000{ "name": "errorhandler", "description": "Development-only error handler middleware", "version": "1.5.1", "contributors": [ "Douglas Christopher Wilson ", "Jonathan Ong (http://jongleberry.com)" ], "license": "MIT", "repository": "expressjs/errorhandler", "dependencies": { "accepts": "~1.3.7", "escape-html": "~1.0.3" }, "devDependencies": { "after": "0.8.2", "eslint": "5.16.0", "eslint-config-standard": "12.0.0", "eslint-plugin-import": "2.17.2", "eslint-plugin-markdown": "1.0.0", "eslint-plugin-node": "8.0.1", "eslint-plugin-promise": "4.1.1", "eslint-plugin-standard": "4.0.0", "istanbul": "0.4.5", "mocha": "6.1.4", "supertest": "4.0.2" }, "files": [ "public/", "LICENSE", "HISTORY.md", "index.js" ], "engines": { "node": ">= 0.8" }, "scripts": { "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec --bail --check-leaks test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" } } errorhandler-1.5.1/public/000077500000000000000000000000001346471663700155045ustar00rootroot00000000000000errorhandler-1.5.1/public/error.html000066400000000000000000000004241346471663700175230ustar00rootroot00000000000000 {error}

    {title}

    {statusCode} {error}

      {stack}
    errorhandler-1.5.1/public/style.css000066400000000000000000000011321346471663700173530ustar00rootroot00000000000000* { margin: 0; padding: 0; outline: 0; } body { padding: 80px 100px; font: 13px "Helvetica Neue", "Lucida Grande", "Arial"; background: #ECE9E9 -webkit-gradient(linear, 0% 0%, 0% 100%, from(#fff), to(#ECE9E9)); background: #ECE9E9 -moz-linear-gradient(top, #fff, #ECE9E9); background-repeat: no-repeat; color: #555; -webkit-font-smoothing: antialiased; } h1, h2 { font-size: 22px; color: #343434; } h1 em, h2 em { padding: 0 5px; font-weight: normal; } h1 { font-size: 60px; } h2 { margin-top: 10px; } ul li { list-style: none; } #stacktrace { margin-left: 60px; } errorhandler-1.5.1/test/000077500000000000000000000000001346471663700152055ustar00rootroot00000000000000errorhandler-1.5.1/test/.eslintrc.yml000066400000000000000000000000231346471663700176240ustar00rootroot00000000000000env: mocha: true errorhandler-1.5.1/test/test.js000066400000000000000000000261241346471663700165270ustar00rootroot00000000000000 process.env.NODE_ENV = 'test' var after = require('after') var assert = require('assert') var errorHandler = require('..') var http = require('http') var request = require('supertest') var util = require('util') describe('errorHandler()', function () { it('should set nosniff header', function (done) { var server = createServer(new Error('boom!')) request(server) .get('/') .expect('X-Content-Type-Options', 'nosniff') .expect(500, done) }) describe('status code', function () { describe('when non-error status code', function () { it('should set the status code to 500', function (done) { var server = createServer({ status: 200 }) request(server) .get('/') .expect(500, done) }) }) describe('when err.status exists', function () { it('should set res.statusCode', function (done) { var server = createServer({ status: 404 }) request(server) .get('/') .expect(404, done) }) }) describe('when err.statusCode exists', function () { it('should set res.statusCode', function (done) { var server = createServer({ statusCode: 404 }) request(server) .get('/') .expect(404, done) }) }) describe('when err.statusCode and err.status exist', function () { it('should prefer err.status', function (done) { var server = createServer({ statusCode: 400, status: 404 }) request(server) .get('/') .expect(404, done) }) }) }) describe('error value', function () { describe('when Error object', function () { it('should use "stack" property', function (done) { var error = new TypeError('boom!') var server = createServer(error) request(server) .get('/') .set('Accept', 'text/plain') .expect(500, error.stack.toString(), done) }) }) describe('when string', function () { it('should pass-through string', function (done) { var server = createServer('boom!') request(server) .get('/') .set('Accept', 'text/plain') .expect(500, 'boom!', done) }) }) describe('when number', function () { it('should stringify number', function (done) { var server = createServer(42.1) request(server) .get('/') .set('Accept', 'text/plain') .expect(500, '42.1', done) }) }) describe('when object', function () { it('should use util.inspect', function (done) { var server = createServer({ hop: 'pop' }) request(server) .get('/') .set('Accept', 'text/plain') .expect(500, '{ hop: \'pop\' }', done) }) }) describe('with "toString" property', function () { it('should use "toString" value', function (done) { var server = createServer({ toString: function () { return 'boom!' } }) request(server) .get('/') .set('Accept', 'text/plain') .expect(500, 'boom!', done) }) }) }) describe('response content type', function () { var error var server before(function () { error = new Error('boom!') error.description = 'it went this way' server = createServer(error) }) describe('when "Accept: text/html"', function () { it('should return a html response', function (done) { request(server) .get('/') .set('Accept', 'text/html') .expect('Content-Type', /text\/html/) .expect(/Error: boom!<\/title>/) .expect(/<h2><em>500<\/em> Error: boom!<\/h2>/) .expect(/<li>    at/) .expect(500, done) }) it('should contain inspected object', function (done) { request(createServer({ foo: 'bar', fizz: 'buzz' })) .get('/') .set('Accept', 'text/html') .expect('Content-Type', /text\/html/) .expect(/<title>Error<\/title>/) .expect(/<h2><em>500<\/em> Error<\/h2>/) .expect(/<li>{ foo: 'bar', fizz: 'buzz' }<\/li>/) .expect(500, done) }) }) describe('when "Accept: application/json"', function () { it('should return a json response', function (done) { var body = { error: { message: 'boom!', description: 'it went this way', stack: error.stack.toString() } } request(server) .get('/') .set('Accept', 'application/json') .expect('Content-Type', /application\/json/) .expect(500, body, done) }) }) describe('when "Accept: text/plain"', function () { it('should return a plain text response', function (done) { request(server) .get('/') .set('Accept', 'text/plain') .expect('Content-Type', /text\/plain/) .expect(500, error.stack.toString(), done) }) }) describe('when "Accept: x-unknown"', function () { it('should return a plain text response', function (done) { request(server) .get('/') .set('Accept', 'x-unknown') .expect('Content-Type', /text\/plain/) .expect(500, error.stack.toString(), done) }) }) }) describe('headers sent', function () { var server before(function () { var _errorHandler = errorHandler() server = http.createServer(function (req, res) { res.end('0') process.nextTick(function () { _errorHandler(new Error('boom!'), req, res, function (error) { process.nextTick(function () { throw error }) }) }) }) }) it('should not die', function (done) { request(server) .get('/') .expect(200, done) }) }) describe('console', function () { var _consoleerror before(function () { _consoleerror = console.error process.env.NODE_ENV = '' }) afterEach(function () { console.error = _consoleerror process.env.NODE_ENV = 'test' }) it('should output error', function (done) { var cb = after(2, done) var error = new Error('boom!') var server = createServer(error) console.error = function () { var log = util.format.apply(null, arguments) if (log !== error.stack.toString()) { return _consoleerror.apply(this, arguments) } cb() } request(server) .get('/') .set('Accept', 'text/plain') .expect(500, error.stack.toString(), cb) }) }) }) describe('errorHandler(options)', function () { describe('log', function () { it('should reject a string', function () { assert.throws(errorHandler.bind(null, { log: 'yes, please' }), /option log must be/) }) describe('when "undefined"', function () { var _consoleerror before(function () { _consoleerror = console.error }) afterEach(function () { console.error = _consoleerror }) describe('when NODE_ENV == test', function () { alterEnvironment('NODE_ENV', 'test') it('should produce no output', function (done) { var error = new Error('boom!') var server = createServer(error) console.error = function () { var log = util.format.apply(null, arguments) if (log !== error.stack.toString()) { return _consoleerror.apply(this, arguments) } done(new Error('console.error written to')) } request(server) .get('/') .set('Accept', 'text/plain') .expect(500, error.stack.toString(), done) }) }) describe('when NODE_ENV != test', function () { alterEnvironment('NODE_ENV', '') it('should write to console', function (done) { var cb = after(2, done) var error = new Error('boom!') var server = createServer(error) console.error = function () { var log = util.format.apply(null, arguments) if (log !== error.stack.toString()) { return _consoleerror.apply(this, arguments) } cb() } request(server) .get('/') .set('Accept', 'text/plain') .expect(500, error.stack.toString(), cb) }) }) }) describe('when "true"', function () { var _consoleerror before(function () { _consoleerror = console.error }) afterEach(function () { console.error = _consoleerror }) it('should write to console', function (done) { var cb = after(2, done) var error = new Error('boom!') var server = createServer(error, { log: true }) console.error = function () { var log = util.format.apply(null, arguments) if (log !== error.stack.toString()) { return _consoleerror.apply(this, arguments) } cb() } request(server) .get('/') .set('Accept', 'text/plain') .expect(500, error.stack.toString(), cb) }) }) describe('when "false"', function () { var _consoleerror alterEnvironment('NODE_ENV', '') before(function () { _consoleerror = console.error }) afterEach(function () { console.error = _consoleerror }) it('should not write to console', function (done) { var error = new Error('boom!') var server = createServer(error, { log: false }) console.error = function () { var log = util.format.apply(null, arguments) if (log !== error.stack.toString()) { return _consoleerror.apply(this, arguments) } done(new Error('console.error written to')) } request(server) .get('/') .set('Accept', 'text/plain') .expect(500, error.stack.toString(), done) }) }) describe('when a function', function () { it('should call function', function (done) { var cb = after(2, done) var error = new Error('boom!') var server = createServer(error, { log: log }) function log (err, str, req, res) { assert.strictEqual(err, error) assert.strictEqual(str, error.stack.toString()) assert.strictEqual(req.url, '/') assert.strictEqual(res.statusCode, 500) cb() } request(server) .get('/') .set('Accept', 'text/plain') .expect(500, error.stack.toString(), cb) }) }) }) }) function createServer (error, options) { var _errorHandler = errorHandler(options) return http.createServer(function (req, res) { _errorHandler(error, req, res, function (err) { res.statusCode = err ? 500 : 404 res.end(err ? 'Critical: ' + err.stack : 'oops') }) }) } function alterEnvironment (key, value) { var prev before(function () { prev = process.env[key] process.env[key] = value }) after(function () { process.env[key] = prev }) } ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������