In this lesson, learn how to create a custom JavaScript error type. It’s as simple as extending the base Error type like this:
class PaymentError extends Error{
constructor(code, message){
super(message);
this.name = "PaymentError"
}
}
These custom error classes can then extended with more app specific functionality like introducing app specific error codes and related user friendly error messages. These custom error classes can also be detected with the instanceof
keyword to handle different error types in different ways in catch blocks.