-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathglobal_functions.js
More file actions
40 lines (32 loc) · 972 Bytes
/
global_functions.js
File metadata and controls
40 lines (32 loc) · 972 Bytes
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
const pe = require('parse-error'); // parses error so you can read error message and handle them accordingly
executeOrThrow = function(promise) {
return promise
.then((data) => {
return [null, data];
})
.catch((err) => [pe(err)]);
};
throwError = function(errMessage) {
console.error(errMessage);
throw new Error(errMessage);
};
returnError = function(res, err, code) {
let error;
if (typeof err == 'object' && typeof err.message != 'undefined') {
error = err.message;
}
if (typeof code !== 'undefined') {
res.statusCode = code;
}
return res.json({ success: false, error: error });
};
returnSuccessResponse = function(res, data, code) {
let sendData = { success: true };
if (typeof data == 'object') {
sendData = Object.assign(data, sendData);
}
if (typeof code !== 'undefined') {
res.statusCode = code;
}
return res.json(sendData);
};