diff --git a/packages/transport-commons/src/channels/index.ts b/packages/transport-commons/src/channels/index.ts index eacc8ee861..84d43bfbc1 100644 --- a/packages/transport-commons/src/channels/index.ts +++ b/packages/transport-commons/src/channels/index.ts @@ -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 @@ -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); + } }); }); }); diff --git a/packages/transport-commons/test/channels/dispatch.test.ts b/packages/transport-commons/test/channels/dispatch.test.ts index 15c328bf74..543fab9c58 100644 --- a/packages/transport-commons/test/channels/dispatch.test.ts +++ b/packages/transport-commons/test/channels/dispatch.test.ts @@ -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);