From 520b19e31376778a068165c34dac12cd8d6ae988 Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Tue, 13 Dec 2022 11:28:02 +0100 Subject: [PATCH] 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 --- ghost/domain-events/lib/DomainEvents.js | 12 ++++++++++-- ghost/domain-events/package.json | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ghost/domain-events/lib/DomainEvents.js b/ghost/domain-events/lib/DomainEvents.js index 892f6a3deb..451f368c08 100644 --- a/ghost/domain-events/lib/DomainEvents.js +++ b/ghost/domain-events/lib/DomainEvents.js @@ -1,4 +1,5 @@ const EventEmitter = require('events').EventEmitter; +const logging = require('@tryghost/logging'); /** * @template T @@ -23,12 +24,19 @@ class DomainEvents { * @template Data * @template {IEvent} EventClass * @param {ConstructorOf} Event - * @param {(event: EventClass) => void} handler + * @param {(event: EventClass) => Promise | void} handler * * @returns {void} */ static subscribe(Event, handler) { - DomainEvents.ee.on(Event.name, 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); + } + }); } /** diff --git a/ghost/domain-events/package.json b/ghost/domain-events/package.json index b66b615687..70f1ba493b 100644 --- a/ghost/domain-events/package.json +++ b/ghost/domain-events/package.json @@ -19,6 +19,7 @@ "devDependencies": { "c8": "7.12.0", "mocha": "10.2.0", - "should": "13.2.3" + "should": "13.2.3", + "@tryghost/logging": "2.3.2" } }