-
Notifications
You must be signed in to change notification settings - Fork 2
/
500.js
77 lines (65 loc) · 2.28 KB
/
500.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
/**
* Default 500 (Server Error) middleware
*
* If an error is thrown in a policy or controller,
* Sails will respond using this default error handler
*
* This middleware can also be invoked manually from a controller or policy:
* res.serverError( [errors] )
*
*
* @param {Array|Object|String} errors
* optional errors
*/
module.exports[500] = function serverErrorOccurred(errors, req, res) {
/*
* NOTE: This function is Sails middleware-- that means that not only do `req` and `res`
* work just like their Express equivalents to handle HTTP requests, they also simulate
* the same interface for receiving socket messages.
*/
var viewFilePath = '500',
statusCode = 500,
i, errorToLog, errorToJSON;
var result = {
status: statusCode
};
// Normalize a {String|Object|Error} or array of {String|Object|Error}
// into an array of proper, readable {Error}
var errorsToDisplay = sails.util.normalizeErrors(errors);
for (i in errorsToDisplay) {
// Log error(s) as clean `stack`
// (avoids ending up with \n, etc.)
if ( errorsToDisplay[i].original ) {
errorToLog = sails.util.inspect(errorsToDisplay[i].original);
}
else {
errorToLog = errorsToDisplay[i].stack;
}
sails.log.error('Server Error (500)');
sails.log.error(errorToLog);
// Use original error if it exists
errorToJSON = errorsToDisplay[i].original || errorsToDisplay[i].message;
errorsToDisplay[i] = errorToJSON;
}
// Only include errors if application environment is set to 'development'
// In production, don't display any identifying information about the error(s)
if (sails.config.environment === 'development') {
result.errors = errorsToDisplay;
}
// If the user-agent wants JSON, respond with JSON
if (req.wantsJSON) {
return res.json(result, result.status);
}
// Set status code and view locals
res.status(result.status);
for (var key in result) {
res.locals[key] = result[key];
}
// And render view
res.render(viewFilePath, result, function (err) {
// If the view doesn't exist, or an error occured, just send JSON
if (err) { return res.json(result, result.status); }
// Otherwise, if it can be rendered, the `views/500.*` page is rendered
res.render(viewFilePath, result);
});
};