forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindDefaultHandlers.js
103 lines (88 loc) · 3.25 KB
/
bindDefaultHandlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Default 500 and 404 handler.
* (defers to res.serverError() and res.notFound() whenever possible)
*
* With default hook configuration, these handlers apply to both HTTP
* and virtual requests
*/
module.exports = function(sails) {
return {
/**
* Default 500 handler.
* (for errors implicitly thrown in middleware/routes)
*
* @param {*} err
* @param {Request} req
* @param {Response} res
*/
500: function(err, req, res) {
// console.log('* * * FIRED FINAL HANDLER (500) * * *');
// console.log('args:',arguments);
// console.log('* * * </FIRED_FINAL_HANDLER_500> * * *');
// console.log();
try {
// Use error handler if it exists
if (typeof res.negotiate === 'function') {
// console.log('res.negotiate()');
return res.negotiate(err);
}
} catch (e) {}
try {
// Catch-all:
// Log a message and try to use `res.send` to respond.
sails.log.error('Server Error:');
sails.log.error(err);
// console.log('catch-all.. log an error and send a 500');
if (process.env.NODE_ENV === 'production') { return res.send(500); }
return res.send(500, err);
}
// Serious error occurred-- unable to send response.
//
// Note that in the future, we could also emit an `abort` message on the request object
// in this case-- then if an attached server is managing this request, it could monitor
// for `abort` events and manage its private resources (e.g. TCP sockets) accordingly.
// However, such contingencies should really handled by the underlying HTTP hook, so
// this might not actually make sense.
catch (errorSendingResponse) {
// console.log('serious error occurred');
sails.log.error('But no response could be sent because another error occurred:');
sails.log.error(errorSendingResponse);
}
},
/**
* Default 404 handler.
* (for unmatched routes)
*
* @param {Request} req
* @param {Response} res
*/
404: function(req, res) {
// Use `notFound` handler if it exists
try {
if (typeof res.notFound === 'function') {
return res.notFound();
}
} catch (e) {}
// Catch-all:
// Log a message and try to use `res.send` to respond.
try {
sails.log.verbose('A request (%s) did not match any routes, and no `res.notFound` handler is configured.', req.url);
res.send(404);
return;
}
// Serious error occurred-- unable to send response.
//
// Note that in the future, we could also emit an `abort` message on the request object
// in this case-- then if an attached server is managing this request, it could monitor
// for `abort` events and manage its private resources (e.g. TCP sockets) accordingly.
// However, such contingencies should really handled by the underlying HTTP hook, so
// this might not actually make sense.
catch (e) {
sails.log.error('An unmatched route was encountered in a request...');
sails.log.error('But no response could be sent because an error occurred:');
sails.log.error(e);
return;
}
}
};
};