~cyphermox/cordova-cli/flatten

« back to all changes in this revision

Viewing changes to node_modules/cordova/_vendor/connect/2.6.0/lib/middleware/errorHandler.js

  • Committer: Mathieu Trudel-Lapierre
  • Date: 2013-12-12 05:26:53 UTC
  • Revision ID: mathieu-tl@ubuntu.com-20131212052653-eatjt8zguqua5qmq
testing the flattenage, yo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
 * Connect - errorHandler
 
3
 * Copyright(c) 2010 Sencha Inc.
 
4
 * Copyright(c) 2011 TJ Holowaychuk
 
5
 * MIT Licensed
 
6
 */
 
7
 
 
8
/**
 
9
 * Module dependencies.
 
10
 */
 
11
 
 
12
var utils = require('../utils')
 
13
  , url = require('url')
 
14
  , fs = require('fs');
 
15
 
 
16
// environment
 
17
 
 
18
var env = process.env.NODE_ENV || 'development';
 
19
 
 
20
/**
 
21
 * Error handler:
 
22
 *
 
23
 * Development error handler, providing stack traces
 
24
 * and error message responses for requests accepting text, html,
 
25
 * or json.
 
26
 *
 
27
 * Text:
 
28
 *
 
29
 *   By default, and when _text/plain_ is accepted a simple stack trace
 
30
 *   or error message will be returned.
 
31
 *
 
32
 * JSON:
 
33
 *
 
34
 *   When _application/json_ is accepted, connect will respond with
 
35
 *   an object in the form of `{ "error": error }`.
 
36
 *
 
37
 * HTML:
 
38
 *
 
39
 *   When accepted connect will output a nice html stack trace.
 
40
 *
 
41
 * @return {Function}
 
42
 * @api public
 
43
 */
 
44
 
 
45
exports = module.exports = function errorHandler(){
 
46
  return function errorHandler(err, req, res, next){
 
47
    if (err.status) res.statusCode = err.status;
 
48
    if (res.statusCode < 400) res.statusCode = 500;
 
49
    if ('test' != env) console.error(err.stack);
 
50
    var accept = req.headers.accept || '';
 
51
    // html
 
52
    if (~accept.indexOf('html')) {
 
53
      fs.readFile(__dirname + '/../public/style.css', 'utf8', function(e, style){
 
54
        fs.readFile(__dirname + '/../public/error.html', 'utf8', function(e, html){
 
55
          var stack = (err.stack || '')
 
56
            .split('\n').slice(1)
 
57
            .map(function(v){ return '<li>' + v + '</li>'; }).join('');
 
58
            html = html
 
59
              .replace('{style}', style)
 
60
              .replace('{stack}', stack)
 
61
              .replace('{title}', exports.title)
 
62
              .replace('{statusCode}', res.statusCode)
 
63
              .replace(/\{error\}/g, utils.escape(err.toString()));
 
64
            res.setHeader('Content-Type', 'text/html; charset=utf-8');
 
65
            res.end(html);
 
66
        });
 
67
      });
 
68
    // json
 
69
    } else if (~accept.indexOf('json')) {
 
70
      var error = { message: err.message, stack: err.stack };
 
71
      for (var prop in err) error[prop] = err[prop];
 
72
      var json = JSON.stringify({ error: error });
 
73
      res.setHeader('Content-Type', 'application/json');
 
74
      res.end(json);
 
75
    // plain text
 
76
    } else {
 
77
      res.writeHead(res.statusCode, { 'Content-Type': 'text/plain' });
 
78
      res.end(err.stack);
 
79
    }
 
80
  };
 
81
};
 
82
 
 
83
/**
 
84
 * Template title, framework authors may override this value.
 
85
 */
 
86
 
 
87
exports.title = 'Connect';