8000 fix: Gracefully handle errors in publishers by daffl · Pull Request #1710 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
8000
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