What is the recommended practice for setting the `name` property of a custom error object?

Question

Grade: Education Subject: Support
What is the recommended practice for setting the `name` property of a custom error object?
Asked by:
90 Viewed 90 Answers

Answer (90)

Best Answer
(358)
It's recommended to set the `name` property of your custom error object to the class name of the error. This makes it easier to identify the type of error when logging or debugging. You can do this in the constructor: `this.name = this.constructor.name;` . This allows you to use `error.name` to easily differentiate between different types of custom errors.