Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a function when a ClientEmitter has an error

I'm using an event handler, where each event code is in it's own files. I'm attaching the events to the client, so when that file has the event emitted, it will run that code:

// looping through all event files
client.on(file.split('.')[0], require(`events/${file}`).bind(null, client, Util);

If the event file was message.js, it would be similar to:

client.on('message', require('events/message.js').bind(null, client, Util);

So when the message event is emitted, it runs the message.js file, passing along the client and Util classes.

I also have a function that is attached to the client called report. It basically reports when there is an error. I would like it so whenever any event from the client has an error, it will run that function.
I've done this slightly with the commands: command.run(...).catch(error => client.report(error)).
Is there a similar way to do this, instead of having to put a try-catch around all code in all the event files?

like image 238
Steam gamer Avatar asked Jun 22 '26 08:06

Steam gamer


1 Answers

Try this way

client.on('error', require('events/report.js').bind(null, client, Util);
like image 79
Sachin Shah Avatar answered Jun 24 '26 00:06

Sachin Shah