PageRenderTime 32ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/error-handlers.js

https://github.com/shwaydogg/node-gtfs
JavaScript | 78 lines | 57 code | 12 blank | 9 comment | 12 complexity | 8c157381b77caef53b232ee9cd9e4313 MD5 | raw file
 1
 2var errors = require('./util/errors');
 3
 4module.exports = function(app){
 5  /**
 6   * 404 Not Found
 7   */
 8
 9  app.error(function(err, req, res, next){
10    if (!app.set('error templates') || !(err instanceof errors.NotFound))
11      return next(err);
12
13    if (req.xhr){
14      res.send(404);
15    } else {
16      res.render('error.jade'
17      , { layout: false
18        , status: 404
19        , locals:
20          { status: 404
21          , title: 'Not Found'
22          , text: '404 Error (File not Found)'
23          }
24        }
25      );
26    }
27  });
28
29  /**
30   * 401 Unauthorized
31   */
32
33  app.error(function(err, req, res, next){
34    if (!app.set('error templates') || !(err instanceof errors.Unauthorized))
35      return next(err);
36
37    if (req.xhr){
38      res.send(401);
39    } else {
40      res.render('error.jade'
41      , { layout: false
42        , status: 401
43        , locals:
44          { status: 401
45          , title: 'Unauthorized'
46          , text: '401 Error (Unauthorized)'
47          }
48        }
49      );
50    }
51  });
52
53  /**
54   * 500 Internal Server Error
55   */
56
57  app.error(function(err, req, res, next){
58    if (!app.set('error templates')) return next(err);
59
60    if (req.xhr){
61      res.send(500);
62    } else {
63      res.render('error.jade'
64      , { layout: false
65        , status: 500
66        , locals:
67          { status: 500
68          , title: 'Internal Server Error'
69          , text: 'Error in our app :( <br> Let us track this down'
70          }
71        }
72      );
73    }
74
75    console.error(err.stack || err);
76  });
77
78};