Typed error

One less "any" in the codebase.

In our BodyIntercetor, we have a try/catch and the catch block is as follows:

catch (error) {
  throw new BadRequestException(error.message);
}

This works, but the error has the type any. As we expect an Error here, we can use the instanceof operator on error, to ensure it's actually an instance of Error.

if (error instanceof Error) {
  // throw the exception
}

Subclasses are also included. In this case, error should be a SyntaxError.

We also get type safety in the process. Thanks to Matt Pocock for his insight in this lesson.

Commit - Providing type safety to error

Last updated