-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_functions.js
49 lines (38 loc) · 1.16 KB
/
global_functions.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
to = function(promise) {
return promise
.then(data => {
return [null, data];
}).catch(err =>
[pe(err)]
);
};
pe = require('parse-error');
TE = function(err_message, log){ // TE stands for Throw Error
if(log === true){
const logger = require('morgan');
logger.error('Backend throwing error ',err_message);
}
throw new Error(err_message);
};
ReE = function(res, err, code){ // Error Web Response
if(typeof err === 'object' && typeof err.message !== 'undefined'){
err = err.message;
}
if(typeof code !== 'undefined') res.statusCode = code;
return res.json({success:false, error: err});
};
ReS = function(res, data, code){ // Success Web Response
let send_data = {success:true};
if(typeof data === 'object'){
send_data = Object.assign(data, send_data);//merge the objects
}
if(typeof code !== 'undefined') res.statusCode = code;
return res.json(send_data)
};
// Catch unhandled rejections and throw to console with a full stack
process.on('unhandledRejection', ex => {
const logger = require('./bootstrap/winston');
if (Array.isArray(ex) && ex.length === 1)
ex = ex[0];
logger.error(`Backend process unhandled Rejection: ${ex.stack}`)
});