dox-foundation-demo node_modules/express/node_modules/connect/lib/middleware/errorHandler.js

utils

declaration
utils

    Description

    Module dependencies.

    Source

    var utils = require('../utils')
      , fs = require('fs');
    
    // environment
    
    var env = process.env.NODE_ENV || 'development';
    • @return: {Function}

    Description

    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.

    Source

    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);
        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; 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.writeHead(res.statusCode, { 'Content-Type': 'text/plain' });
          res.end(err.stack);
        }
      };
    };

    title

    property
    exports.title

      Description

      Template title, framework authors may override this value.

      Source

      exports.title = 'Connect';