Skip to content

Commit

Permalink
fix: Gracefully handle errors in publishers (#1710)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Nov 27, 2019
1 parent d0bae37 commit 0616306
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
33 changes: 19 additions & 14 deletions packages/transport-commons/src/channels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function channels () {

debug('Publishing event', event, hook.path);

const logError = (error: any) => debug(`Error in '${hook.path} ${event}' publisher`, error);
const servicePublishers = (service as unknown as PublishMixin)[keys.PUBLISHERS];
const appPublishers = (app as unknown as PublishMixin)[keys.PUBLISHERS];
// This will return the first publisher list that is not empty
Expand All @@ -84,20 +85,24 @@ export function channels () {
noop
);

Promise.resolve(publisher(data, hook)).then(result => {
if (!result) {
return;
}

const results = Array.isArray(result) ? compact(flattenDeep(result)) : [result];
const channel = new CombinedChannel(results);

if (channel && channel.length > 0) {
app.emit('publish', event, channel, hook, data);
} else {
debug('No connections to publish to');
}
});
try {
Promise.resolve(publisher(data, hook)).then(result => {
if (!result) {
return;
}

const results = Array.isArray(result) ? compact(flattenDeep(result)) : [result];
const channel = new CombinedChannel(results);

if (channel && channel.length > 0) {
app.emit('publish', event, channel, hook, data);
} else {
debug('No connections to publish to');
}
}).catch(logError);
} catch (error) {
logError(error);
}
});
});
});
Expand Down
12 changes: 12 additions & 0 deletions packages/transport-commons/test/channels/dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ describe('app.publish', () => {
});
});

it('error in publisher is handled gracefully (#1707)', async () => {
app.service('test').publish('created', () => {
throw new Error('Something went wrong');
});

try {
await app.service('test').create({ message: 'something' });
} catch (error) {
assert.fail('Should never get here');
}
});

it('simple event registration and dispatching', done => {
app.channel('testing').join(c1);

Expand Down

0 comments on commit 0616306

Please # to comment.