class CustomError extends Error {
constructor(message) {
super(message);
this.name = "CustomError";
}
}
In the above example, we create a new class called CustomError that extends the built-in Error class. We then define a constructor function that takes a message parameter and passes it to the super() method, which calls the parent constructor. We also set the name property of the error to "CustomError".
Once you have defined your custom error, you can throw it like any other error:
throw new CustomError("Something went wrong!");