Error catching for events

This commit is contained in:
Evert Prants 2020-12-06 15:40:06 +02:00
parent 1d847b7d92
commit ac912c0052
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

View File

@ -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);