Skip to Content
nyx logonyx guide

Error Handling

Reading time:1 min read

Every executor holds an ErrorHandler, where errors thrown by middlewares and executed objects are handled.

const  = .().().();
const  = .().();
const  = .().().();

It holds a map of consumers, where each handle a specific error class. It also receives the erroring object, and the arguments that were used to call it.

Manipulating consumers

This example will use the CommandManager's handler, but it works the same for other executors.

To add a consumer, use ErrorHandler#setConsumer():

import { ,  } from '@nyx-discord/djs';
import type { ,  } from '@nyx-discord/djs';

.(
  ,
  (
    : ,
    : ,
    : ,
  ) => {
    const [, ] = ;
    const {  } = .();

    .().(
      `MyError occurred while executing ${} with args: ${}`,
      
    );
  }
);

You can also remove consumers:

.();

And set a fallback consumer, for all errors that don't have a consumer:

import {  } from '@nyx-discord/djs';

.(
  (
    : object,
    : ,
    : ,
  ) => {
    const [, ] = ;
    const {  } = .();

    .().(
      `An unhandled error occurred while executing ${} with args: ${}`,
      
    );
  }
);

Consumer resolution

The error handler resolves a consumer based on the error prototype, going from the top to the bottom.

For instance:

class  extends  {}

class  extends  {}

.(, () => {
  .('MyError');
});

This will log MyError when an instance of MyError2 is thrown, because it's a subclass.

If no consumer is found, the fallback consumer is used, whose error type is simply object.

Edit on GitHub

Last updated on

On this page