pax_global_header00006660000000000000000000000064124015210370014505gustar00rootroot0000000000000052 comment=9dbb9abb9322476b2499d50db2b631024ecd414d errorhandler-1.2.0/000077500000000000000000000000001240152103700141745ustar00rootroot00000000000000errorhandler-1.2.0/.gitignore000066400000000000000000000012221240152103700161610ustar00rootroot00000000000000# Compiled source # ################### *.com *.class *.dll *.exe *.o *.so # Packages # ############ # it's better to unpack these files and commit the raw source # git has its own built in compression methods *.7z *.dmg *.gz *.iso *.jar *.rar *.tar *.zip # Logs and databases # ###################### *.log *.sql *.sqlite # OS generated files # ###################### .DS_Store* ehthumbs.db Icon? Thumbs.db # Node.js # ########### lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results coverage node_modules npm-debug.log # Git # ####### *.orig *.BASE.* *.BACKUP.* *.LOCAL.* *.REMOTE.* # Components # ############## /build /components errorhandler-1.2.0/.travis.yml000066400000000000000000000003711240152103700163060ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.11" matrix: allow_failures: - node_js: "0.11" fast_finish: true script: "npm run-script test-travis" after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls" errorhandler-1.2.0/HISTORY.md000066400000000000000000000014271240152103700156630ustar00rootroot000000000000001.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.2.0/LICENSE000066400000000000000000000021101240152103700151730ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014 Jonathan Ong 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.2.0/README.md000066400000000000000000000030771240152103700154620ustar00rootroot00000000000000# errorhandler [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Build Status][travis-image]][travis-url] [![Test Coverage][coveralls-image]][coveralls-url] [![Gratipay][gratipay-image]][gratipay-url] Development-only error handler middleware ## Install ```sh $ npm install errorhandler ``` ## API ```js var errorhandler = require('errorhandler') ``` ### errorhandler() Create new middleware to handle errors and respond with content negotiation. This middleware is only intended to be used in a development environment, as the full error stack traces will be sent back to the client when an error occurs. ## 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()) } ``` ## License [MIT](LICENSE) [npm-image]: https://img.shields.io/npm/v/errorhandler.svg?style=flat [npm-url]: https://npmjs.org/package/errorhandler [travis-image]: https://img.shields.io/travis/expressjs/errorhandler.svg?style=flat [travis-url]: https://travis-ci.org/expressjs/errorhandler [coveralls-image]: https://img.shields.io/coveralls/expressjs/errorhandler.svg?style=flat [coveralls-url]: https://coveralls.io/r/expressjs/errorhandler?branch=master [downloads-image]: http://img.shields.io/npm/dm/errorhandler.svg?style=flat [downloads-url]: https://npmjs.org/package/errorhandler [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat [gratipay-url]: https://www.gratipay.com/dougwilson/ errorhandler-1.2.0/index.js000066400000000000000000000064461240152103700156530ustar00rootroot00000000000000/*! * errorhandler * Copyright(c) 2010 Sencha Inc. * Copyright(c) 2011 TJ Holowaychuk * Copyright(c) 2014 Jonathan Ong * MIT Licensed */ /** * Module dependencies. */ var accepts = require('accepts') var escapeHtml = require('escape-html'); var fs = require('fs'); var util = require('util') /** * Module variables. */ var inspect = util.inspect var toString = Object.prototype.toString /** * 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(){ // get environment var env = process.env.NODE_ENV || 'development' return function errorHandler(err, req, res, next){ // respect err.status if (err.status) { res.statusCode = err.status } // default status code to 500 if (res.statusCode < 400) { res.statusCode = 500 } // write error to console if (env !== 'test') { console.error(stringify(err)) } // cannot actually respond if (res._header) { return req.socket.destroy() } // negotiate var accept = accepts(req) var type = accept.types('html', 'json', 'text') // Security header for content sniffing res.setHeader('X-Content-Type-Options', 'nosniff') // html if (type === 'html') { fs.readFile(__dirname + '/public/style.css', 'utf8', function(e, style){ if (e) return next(e); fs.readFile(__dirname + '/public/error.html', 'utf8', function(e, html){ if (e) return next(e); var stack = String(err.stack || '') .split('\n').slice(1) .map(function(v){ return '
  • ' + escapeHtml(v).replace(/ /g, '  ') + '
  • '; }).join(''); html = html .replace('{style}', style) .replace('{stack}', stack) .replace('{title}', escapeHtml(exports.title)) .replace('{statusCode}', res.statusCode) .replace(/\{error\}/g, escapeHtml(stringify(err)).replace(/ /g, '  ').replace(/\n/g, '
    ')); res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(html); }); }); // 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 }); res.setHeader('Content-Type', 'application/json'); res.end(json); // plain text } else { res.setHeader('Content-Type', 'text/plain'); res.end(stringify(err)); } }; }; /** * Template title, framework authors may override this value. */ exports.title = 'Connect'; /** * 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 } errorhandler-1.2.0/package.json000066400000000000000000000017241240152103700164660ustar00rootroot00000000000000{ "name": "errorhandler", "description": "Development-only error handler middleware", "version": "1.2.0", "contributors": [ "Douglas Christopher Wilson ", "Jonathan Ong (http://jongleberry.com)" ], "license": "MIT", "repository": "expressjs/errorhandler", "dependencies": { "accepts": "~1.1.0", "escape-html": "1.0.1" }, "devDependencies": { "connect": "3", "istanbul": "0.3.0", "mocha": "~1.21.4", "should": "~4.0.1", "supertest": "~0.13.0" }, "files": [ "public/", "LICENSE", "HISTORY.md", "index.js" ], "engines": { "node": ">= 0.8" }, "scripts": { "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.2.0/public/000077500000000000000000000000001240152103700154525ustar00rootroot00000000000000errorhandler-1.2.0/public/error.html000066400000000000000000000004241240152103700174710ustar00rootroot00000000000000 {error}

    {title}

    {statusCode} {error}

      {stack}
    errorhandler-1.2.0/public/style.css000066400000000000000000000011321240152103700173210ustar00rootroot00000000000000* { 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.2.0/test/000077500000000000000000000000001240152103700151535ustar00rootroot00000000000000errorhandler-1.2.0/test/test.js000066400000000000000000000123351240152103700164740ustar00rootroot00000000000000 process.env.NODE_ENV = 'test'; var connect = require('connect'); var errorHandler = require('..') var http = require('http') var request = require('supertest'); var should = require('should'); var util = require('util') describe('errorHandler()', function () { var app, error, server; before(function () { app = connect(); app.use(function (req, res, next) { next(error); }); app.use(errorHandler()); server = http.createServer(app).listen(); }); beforeEach(function () { error = null; }); it('should set nosniff header', function (done) { error = new Error() request(server) .get('/') .expect('X-Content-Type-Options', 'nosniff') .expect(500, done) }) describe('status code', function () { it('should set the status code to 500 if a non error status code was given', function (done) { error = {status: 200}; request(server) .get('/') .end(function (err, res) { if (err) throw err; res.statusCode.should.be.exactly(500); done(); }); }); it('should pass an error status code to the response object', function (done) { error = {status: 404}; request(server) .get('/') .end(function (err, res) { if (err) throw err; res.statusCode.should.be.exactly(404); done(); }); }); }); describe('response content type', function () { beforeEach(function () { error = new Error('boom!'); }); it('should return a html response when html is accepted', function (done) { request(server) .get('/') .set('Accept', 'text/html') .end(function (err, res) { if (err) throw err; res.headers['content-type'].should.startWith('text/html'); res.text.should.containEql(''); res.text.should.containEql('Error: boom!'); res.text.should.containEql('    at'); done(); }); }); it('should return a json response when json is accepted', function (done) { request(server) .get('/') .set('Accept', 'application/json') .end(function (err, res) { if (err) throw err; var errorMessage = JSON.parse(res.text); res.headers['content-type'].should.startWith('application/json'); errorMessage.should.be.a.Object; errorMessage.should.have.property('error'); errorMessage.error.should.have.properties(['message', 'stack']); done(); }); }); it('should return a plain text response when json or html is not accepted', function (done) { request(server) .get('/') .set('Accept', 'bogus') .end(function (err, res) { if (err) throw err; res.headers['content-type'].should.startWith('text/plain'); res.text.should.be.exactly(error.stack.toString()); done(); }); }); }); describe('headers sent', function () { it('should not die', function (done) { var app = connect(); var handler = errorHandler(); app.use(function (req, res, next) { res.end('0'); process.nextTick(function () { handler(new Error('msg'), req, res, function (error) { process.nextTick(function () { throw error; }); }); }); }); request(app) .get('/') .expect(200, done); }); }); describe('write error to console.error', function () { var app var error = null var log var old before(function () { old = console.error console.error = function () { log = util.format.apply(null, arguments) } process.env.NODE_ENV = '' app = connect() app.use(function (req, res, next) { next(error) }) app.use(errorHandler()) }) beforeEach(function () { error = null log = undefined }) after(function () { console.error = old process.env.NODE_ENV = 'test' }) it('should write stack', function (done) { error = new Error('boom!') request(app) .get('/') .expect(500, function (err) { if (err) return done(err) log.should.startWith('Error: boom!\n at') done() }) }) it('should stringify primitive', function (done) { error = 'boom!' request(app) .get('/') .expect(500, function (err) { if (err) return done(err) log.should.equal('boom!') done() }) }) it('should stringify plain object', function (done) { error = {hop: 'pop'} request(app) .get('/') .expect(500, function (err) { if (err) return done(err) log.should.equal('{ hop: \'pop\' }') done() }) }) it('should stringify number', function (done) { error = 42 request(app) .get('/') .expect(500, function (err) { if (err) return done(err) log.should.equal('42') done() }) }) it('should stringify plain object with toString', function (done) { error = {toString: function () { return 'boom!' }} request(app) .get('/') .expect(500, function (err) { if (err) return done(err) log.should.equal('boom!') done() }) }) }) }) �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������