From ac912c0052edaa7c9260e80ecda49fdb7872d0c8 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 6 Dec 2020 15:40:06 +0200 Subject: [PATCH] Error catching for events --- src/util/events.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/util/events.ts b/src/util/events.ts index bb88bb0..c99bac3 100644 --- a/src/util/events.ts +++ b/src/util/events.ts @@ -1,3 +1,5 @@ +import { logger } from '../core'; + export class ScopedEventEmitter { private listeners: {[key: string]: any[]}; public addEventListener = this.on; @@ -33,7 +35,12 @@ export class ScopedEventEmitter { const listener = this.listeners[name][i]; if (listener.event === event && listener.func) { - listener.func(...args); + try { + listener.func(...args); + } catch (e) { + logger.error('An error occured in one of the listeners for "%s":', + event, e.stack); + } if (listener.once) { this.listeners[name].splice(parseInt(i, 10), 1); @@ -56,7 +63,12 @@ export class ScopedEventEmitter { const listener = this.listeners[name][i]; if (listener.event === event && listener.func) { - listener.func(...args); + try { + listener.func(...args); + } catch (e) { + logger.error('An error occured in one of the listeners for "%s", stream "%s":', + event, name, e.stack); + } if (listener.once) { this.listeners[name].splice(parseInt(i, 10), 1);