Skip to content

Commit 00b68d9

Browse files
authored
Hotfix event error handler (#264)
* refactoring * feat(core): Added error handler for events * fix(core): Added error handler for events * fix: Added support for Node 18
1 parent 406e96b commit 00b68d9

File tree

7 files changed

+148
-14
lines changed

7 files changed

+148
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ jobs:
2525
- 4222:4222
2626
strategy:
2727
matrix:
28-
node-version: [12.x, 13.x, 14.x, 15.x, 16.x]
29-
28+
node-version: [12.x, 13.x, 14.x, 15.x, 16.x, 17.x, 18.x]
3029
steps:
3130
- uses: actions/checkout@v2
3231
- name: Setup Node.js

.idea/workspace.xml

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/core/lib/middlewares/error-handler/index.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = (runtime) => {
1010
const wrapErrorHandlerMiddleware = function (handler) {
1111
return function errorHandlerMiddleware (context, serviceInjections) {
1212
return handler(context, serviceInjections)
13-
.catch(error => {
13+
.catch((error) => {
1414
if (!(error instanceof Error)) {
1515
error = new WeaveError(error, 500);
1616
}
@@ -19,17 +19,49 @@ module.exports = (runtime) => {
1919
runtime.transport.removePendingRequestsById(context.id);
2020
}
2121

22-
runtime.log.debug(`The action ${context.action.name} is rejected`, { requestId: context.id }, error);
22+
Object.defineProperty(error, 'context', {
23+
value: context,
24+
writable: true,
25+
enumerable: false
26+
});
27+
28+
runtime.log.debug(`The action "${context.action.name}" was rejected`, { requestId: context.requestId }, error);
29+
return runtime.handleError(error);
30+
});
31+
};
32+
};
33+
34+
const wrapEventErrorHandlerMiddleware = function (handler) {
35+
return function errorHandlerMiddleware (context, serviceInjections) {
36+
return handler(context, serviceInjections)
37+
.catch((error) => {
38+
if (!(error instanceof Error)) {
39+
error = new WeaveError(error, 500);
40+
}
41+
42+
if (runtime.nodeId !== context.nodeId) {
43+
runtime.transport.removePendingRequestsById(context.id);
44+
}
45+
46+
Object.defineProperty(error, 'context', {
47+
value: context,
48+
writable: true,
49+
enumerable: false
50+
});
51+
52+
runtime.log.debug(`The event "${context.eventName}" was rejected`, { requestId: context.requestId }, error);
2353
return runtime.handleError(error);
54+
})
55+
.catch((error) => {
56+
// we just log the error because we don't want to crash the event loop
57+
runtime.log.error(error);
2458
});
25-
// .catch(error => {
26-
// runtime.log.error(error)
27-
// })
2859
};
2960
};
3061

3162
return {
3263
localAction: wrapErrorHandlerMiddleware,
33-
remoteAction: wrapErrorHandlerMiddleware
64+
remoteAction: wrapErrorHandlerMiddleware,
65+
localEvent: wrapEventErrorHandlerMiddleware
3466
};
3567
};

packages/core/core/test/integration/events.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ const OtherService = {
5252
events: {
5353
'other.thing' () {
5454
flow.push(`${this.broker.nodeId}-${this.name}-other.thing`);
55+
},
56+
'failed-event' () {
57+
flow.push(`${this.broker.nodeId}-${this.name}-failed-event`);
58+
throw new Error('This is a failed event');
5559
}
5660
}
5761
};
@@ -406,3 +410,19 @@ describe('Event acknowledgement', () => {
406410
node1.emit('user-created', null, { ack: true });
407411
});
408412
});
413+
414+
describe('Event error handling', () => {
415+
const nodes = createNodes(['test-event-error-handling']);
416+
417+
beforeAll(() => Promise.all(nodes.map(node => node.start())));
418+
afterAll(() => Promise.all(nodes.map(node => node.stop())));
419+
420+
it('should throw an error', async () => {
421+
const node = nodes[0];
422+
try {
423+
await node.emit('failed-event');
424+
} catch (error) {
425+
expect(error.message).toBe('This is a failed event');
426+
}
427+
});
428+
});

packages/core/repl/lib/helper/invoke-action.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const fs = require('fs');
33
const cliUI = require('../utils/cli-ui');
44
const convertArgs = require('../utils/convert-args');
55
const { safeCopy, isStream, isObject, timespanFromUnixTimes } = require('@weave-js/utils');
6-
76
const util = require('util');
87

98
function handleResult (result, args, startTime) {

packages/core/utils/lib/get-IP-list.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ exports.getIpList = function getIpList (skipInternal = true) {
55
return Object.keys(interfaces)
66
.map(name => {
77
let IPs = interfaces[name]
8-
.filter(int => int.family === 'IPv4');
8+
.filter((networkInterface) => networkInterface.family === 'IPv4' || networkInterface.family === 4);
99

1010
if (skipInternal) {
11-
IPs = IPs.filter(int => !int.internal);
11+
IPs = IPs.filter((networkInterface) => !networkInterface.internal);
1212
}
1313

14-
return IPs.map(int => int.address);
15-
}).reduce((a, b) => a.concat(b), []);
14+
return IPs.map((networkInterface) => networkInterface.address);
15+
}).flat();
1616
};
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const utils = require('../lib');
22

33
describe('IP List function', () => {
4-
it('should flatten an array a single level deep (1)', () => {
4+
it('should return an IP List', () => {
55
const regex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
66
const result = utils.getIpList();
7+
78
expect(regex.test(result[0])).toBe(true);
89
});
910
});

0 commit comments

Comments
 (0)