-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternaltask-error.js
40 lines (29 loc) · 1.29 KB
/
externaltask-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
39
40
module.exports = function (RED) {
function ExternalTaskError(config) {
RED.nodes.createNode(this, config);
var node = this;
const flowContext = node.context().flow;
let eventEmitter = null;
node.on('input', function (msg) {
eventEmitter = flowContext.get('emitter');
if (!eventEmitter) {
flowContext.set('emitter', new EventEmitter());
eventEmitter = flowContext.get('emitter');
}
const flowNodeInstanceId = msg.flowNodeInstanceId;
let msgError = msg.error;
if (msgError === undefined) {
msgError.message = 'An error occurred';
}
const error = new Error(msgError.message);
error.errorCode = config.error || {};
error.errorDetails = RED.util.encodeObject(msg);
msg.errorCode = config.error;
msg.errorMessage = msgError.message;
node.log(`handle-${flowNodeInstanceId}: *flowNodeInstanceId* '${flowNodeInstanceId}' with *msg._msgid* '${msg._msgid}'`);
eventEmitter.emit(`handle-${flowNodeInstanceId}`, error, true);
node.send(msg);
});
}
RED.nodes.registerType('externaltask-error', ExternalTaskError);
};