[Pkg-javascript-commits] [node-errorhandler] 01/01: Imported Upstream version 1.0.2

Leo Iannacone l3on-guest at moszumanska.debian.org
Sat Jun 14 16:27:50 UTC 2014


This is an automated email from the git hooks/post-receive script.

l3on-guest pushed a commit to branch master
in repository node-errorhandler.

commit 0e661ef4f26d7e659dab7436ce6a76f70e0669b4
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Sat Jun 14 18:17:05 2014 +0200

    Imported Upstream version 1.0.2
---
 .npmignore        |   3 ++
 .travis.yml       |  11 ++++++
 History.md        |  15 +++++++
 README.md         |  59 +++++++++++++++++++++++++++
 index.js          | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json      |  23 +++++++++++
 public/error.html |  14 +++++++
 public/style.css  |  35 ++++++++++++++++
 test/test.js      | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 386 insertions(+)

diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..cd39b77
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,3 @@
+coverage/
+test/
+.travis.yml
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..1ff243c
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,11 @@
+language: 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 at 2.10.0 && cat ./coverage/lcov.info | coveralls"
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..de45645
--- /dev/null
+++ b/History.md
@@ -0,0 +1,15 @@
+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`
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ca10d41
--- /dev/null
+++ b/README.md
@@ -0,0 +1,59 @@
+# errorhandler
+
+[![NPM version](https://badge.fury.io/js/errorhandler.svg)](http://badge.fury.io/js/errorhandler)
+[![Build Status](https://travis-ci.org/expressjs/errorhandler.svg?branch=master)](https://travis-ci.org/expressjs/errorhandler)
+[![Coverage Status](https://img.shields.io/coveralls/expressjs/errorhandler.svg?branch=master)](https://coveralls.io/r/expressjs/errorhandler)
+
+Previously `connect.errorHandler()`.
+
+## Install
+
+```sh
+$ npm install errorhandler
+```
+
+## API
+
+### 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 send 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') {
+  app.use(errorhandler())
+}
+```
+
+## License
+
+The MIT License (MIT)
+
+Copyright (c) 2014 Jonathan Ong me at jongleberry.com
+
+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.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..739dd30
--- /dev/null
+++ b/index.js
@@ -0,0 +1,110 @@
+/*!
+ * errorhandler
+ * Copyright(c) 2010 Sencha Inc.
+ * Copyright(c) 2011 TJ Holowaychuk
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var fs;
+try {
+  fs = require('graceful-fs');
+} catch (_) {
+  fs = require('fs');
+}
+
+// environment
+
+var env = process.env.NODE_ENV || 'development';
+
+/**
+ * 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(){
+  return function errorHandler(err, req, res, next){
+    if (err.status) res.statusCode = err.status;
+    if (res.statusCode < 400) res.statusCode = 500;
+    if ('test' != env) console.error(err.stack);
+    if (res._header) return;
+    var accept = req.headers.accept || '';
+    // html
+    if (~accept.indexOf('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 = (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, escapeHTML(err.toString().replace(/\n/g, '<br/>')));
+            res.setHeader('Content-Type', 'text/html; charset=utf-8');
+            res.end(html);
+        });
+      });
+    // json
+    } else if (~accept.indexOf('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(err.stack);
+    }
+  };
+};
+
+/**
+ * Template title, framework authors may override this value.
+ */
+
+exports.title = 'Connect';
+
+
+/**
+ * Escape the given string of `html`.
+ *
+ * @param {String} html
+ * @return {String}
+ * @api private
+ */
+
+function escapeHTML(html){
+  return String(html)
+    .replace(/&(?!\w+;)/g, '&')
+    .replace(/</g, '<')
+    .replace(/>/g, '>')
+    .replace(/"/g, '"');
+};
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..3e4dc8c
--- /dev/null
+++ b/package.json
@@ -0,0 +1,23 @@
+{
+  "name": "errorhandler",
+  "description": "connect's default error handler page",
+  "version": "1.0.2",
+  "author": "Jonathan Ong <me at jongleberry.com> (http://jongleberry.com)",
+  "license": "MIT",
+  "repository": "expressjs/errorhandler",
+  "devDependencies": {
+    "connect": "3",
+    "istanbul": "0.2.10",
+    "mocha": ">= 1.17.0 < 2",
+    "should": "~4.0.1",
+    "supertest": "~0.13.0"
+  },
+  "engines": {
+    "node": ">= 0.8"
+  },
+  "scripts": {
+    "test": "mocha --reporter dot test/",
+    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot test/",
+    "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec test/"
+  }
+}
diff --git a/public/error.html b/public/error.html
new file mode 100644
index 0000000..a6d3faf
--- /dev/null
+++ b/public/error.html
@@ -0,0 +1,14 @@
+<html>
+  <head>
+    <meta charset='utf-8'> 
+    <title>{error}</title>
+    <style>{style}</style>
+  </head>
+  <body>
+    <div id="wrapper">
+      <h1>{title}</h1>
+      <h2><em>{statusCode}</em> {error}</h2>
+      <ul id="stacktrace">{stack}</ul>
+    </div>
+  </body>
+</html>
diff --git a/public/style.css b/public/style.css
new file mode 100644
index 0000000..b571e98
--- /dev/null
+++ b/public/style.css
@@ -0,0 +1,35 @@
+* {
+  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;
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..53bf936
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,116 @@
+
+process.env.NODE_ENV = 'test';
+
+var connect = require('connect');
+var request = require('supertest');
+var should = require('should');
+var http = require('http');
+var errorHandler = require('..');
+
+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;
+  });
+
+  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('');
+    });
+
+    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('<title>');
+        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('/')
+      .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);
+    });
+  });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-errorhandler.git



More information about the Pkg-javascript-commits mailing list