Ghost/ghost/domain-events/lib/DomainEvents.js
Simon Backx 520b19e313
Added try catch for errors in all domain event listeners (#15976)
refs https://github.com/TryGhost/Team/issues/2370

Reduces the amount of unhandled errors that could cause a crash in Ghost
2022-12-13 11:28:02 +01:00

64 lines
1.5 KiB
JavaScript

const EventEmitter = require('events').EventEmitter;
const logging = require('@tryghost/logging');
/**
* @template T
* @typedef {import('./').ConstructorOf<T>} ConstructorOf<T>
*/
/**
* @template Data
* @typedef {object} IEvent
* @prop {Date} timestamp
* @prop {Data} data
*/
class DomainEvents {
/**
* @private
* @type EventEmitter
*/
static ee = new EventEmitter;
/**
* @template Data
* @template {IEvent<Data>} EventClass
* @param {ConstructorOf<EventClass>} Event
* @param {(event: EventClass) => Promise<void> | void} handler
*
* @returns {void}
*/
static subscribe(Event, handler) {
DomainEvents.ee.on(Event.name, async (event) => {
try {
await handler(event);
} catch (e) {
logging.error('Unhandled error in event handler for event: ' + Event.name);
logging.error(e);
}
});
}
/**
* @template Data
* @param {IEvent<Data>} event
* @returns {void}
*/
static dispatch(event) {
DomainEvents.dispatchRaw(event.constructor.name, event);
}
/**
* Dispatch an event in case you don't have an instance of the event class, but you do have the event name and event data.
* @template Data
* @param {string} name
* @param {Data} data
* @returns {void}
*/
static dispatchRaw(name, data) {
DomainEvents.ee.emit(name, data);
}
}
module.exports = DomainEvents;