-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
54 lines (48 loc) · 1.68 KB
/
index.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
const nodeSvc = require('./services/http/node');
const edgeSvc = require('./services/http/edge');
const cudSvc = require('./services/http/cud');
const cypherSvc = require('./services/http/cypher');
const customCypherSvc = require('./services/http/customCypher');
const cudPubsub = require('./services/pubsub/cudPubsub');
const cypherPubsub = require('./services/pubsub/cypherPubsub');
const customCypherPubsub = require('./services/pubsub/customCypherPubsub');
const moment = require('moment');
/**
* Executes a function, and if it throws an error, guarantees error response.
* @param {*} aFunction function to execute
* @param {*} res response object
* @returns an export function
*/
const guaranteeResponseHTTP = (aFunction, failMsg = 'Error processing response') => {
return (req, res) => {
try {
return aFunction(req, res);
} catch (err) {
console.error(err);
return Promise.resolve(res.status(500).json({
date: moment.utc().format(),
message: failMsg,
error: `${err}`,
}));
}
};
};
const guaranteeCallbackPubsub = (aFunction) => {
return (pubSubEvent, context, callback) => {
try {
return aFunction(pubSubEvent, context, callback);
} catch(err) {
return Promise.resolve(callback(err));
}
};
};
module.exports = {
node: guaranteeResponseHTTP(nodeSvc),
edge: guaranteeResponseHTTP(edgeSvc),
cud: guaranteeResponseHTTP(cudSvc),
cypher: guaranteeResponseHTTP(cypherSvc),
customCypher: guaranteeResponseHTTP(customCypherSvc),
cudPubsub: guaranteeCallbackPubsub(cudPubsub),
cypherPubsub: guaranteeCallbackPubsub(cypherPubsub),
customCypherPubsub: guaranteeCallbackPubsub(customCypherPubsub),
};