feat: change invoke CMS Events to throw Error (#5813)

This commit is contained in:
senda_y 2021-09-30 19:00:35 +09:00 committed by GitHub
parent 089533683b
commit 90bf4cb06a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -170,6 +170,19 @@ describe('registry', () => {
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(data, options2);
});
it(`should throw error when '${name}' handler throws error`, async () => {
const { registerEventListener, invokeEvent } = require('../registry');
const handler = jest.fn(() => {
throw new Error('handler failed!');
});
registerEventListener({ name, handler });
const data = { entry: fromJS({ data: {} }) };
await expect(invokeEvent({ name, data })).rejects.toThrow('handler failed!');
});
});
it(`should return an updated entry's DataMap`, async () => {

View File

@ -246,14 +246,10 @@ export async function invokeEvent({ name, data }) {
let _data = { ...data };
for (const { handler, options } of handlers) {
try {
const result = await handler(_data, options);
if (result !== undefined) {
const entry = _data.entry.set('data', result);
_data = { ...data, entry };
}
} catch (e) {
console.warn(`Failed running handler for event ${name} with message: ${e.message}`);
const result = await handler(_data, options);
if (result !== undefined) {
const entry = _data.entry.set('data', result);
_data = { ...data, entry };
}
}
return _data.entry.get('data');