forked from tarun1475/Nodejs-Bittrex-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.js
38 lines (35 loc) · 968 Bytes
/
error.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
"use strict";
var env = process.env.NODE_ENV || 'production';
var logging = require('./logging');
var constants = require('./constants');
/**
* Generic middleware to handle error in APIs
* @param err
* @param req
* @param res
* @param next
*/
module.exports = function(err, req, res, next) {
var code = err.status || constants.responseFlags.INTERNAL_SERVER_ERR;
var handlerInfo = {
"apiModule": "Error",
"apiHandler": "err"
};
var response = {
"error": err,
};
if(err.data)
response.data = err.data;
if(err.url)
response.url = err.url;
if(code >= constants.responseFlags.INTERNAL_SERVER_ERR) {
logging.warn(handlerInfo, err.stack);
}
if(env.toLowerCase() == "production") {
if(code == constants.responseFlags.INTERNAL_SERVER_ERR) {
response.error = "An unexpected error has occured.";
}
response.stack = undefined;
}
res.status(code).json(response);
};