-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathglobal_functions.js
executable file
·45 lines (34 loc) · 1.28 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
to = function(promise) {//global function that will help use handle promise rejections, this article talks about it http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/
return promise
.then(data => {
return [null, data];
}).catch(err =>
[pe(err)]
);
}
pe = require('parse-error');//parses error so you can read error message and handle them accordingly
TE = function(err_message, log){ // TE stands for Throw Error
if(log === true){
console.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)
};
//This is here to handle all the uncaught promise rejections
process.on('unhandledRejection', error => {
console.error('Uncaught Error', pe(error));
});