Error and Exception interchangeably ) class that JavaScript provides was not going to be enough. I decided to come up with a Layered Error Handling approach. To better visualise this, I made this super technical drawing:

This concept of having more and more specific exceptions isn’t new, however JavaScript doesn’t provide a rich collection of exceptions as a more backend focused language like C# does. Thinking more about exceptions in this way gets you a little closer.
Domain Level Exceptions
These are my first level of specificity for my exceptions. They are simple a class for my more generic exceptions to inherit from.instanceof check here to see where the error originated from. Now, the subjects I provide are a bit more specific than that and can be whatever you want. The default is useful to provide an error if something completely unexpected happened. It might seem bad at first, but this will allow you to run through locally with the input data and narrow down where something went wrong. Allowing you to strengthen your code and tests, and assign an appropriate exception type to this problem.
Domain Specific Exceptions
The next layer (I have only gone this deep, but you can go further depending on your use case) is an exception specific to a particular Domain. These will inherit from the Domain Level exception allowing us to use theinstanceof check in the example getSubject method above.
constructor to take in specific data based on the exception and construct a message that will go into the body of the service ticket.
error.message prop to populate the body of the Service Ticket. However, for some exceptions, I do need to provide a bit more information to help the client and debugging. For this, I created an ITicketException interface that an Error can implement to construct a more complicated error message.
--- would be details that the client may be able to use to solve the issue (if possible from their end), otherwise, I am able to check the other details and quickly use the payload to debug the issue locally. If I am not able to narrow down the details, I can track down the logs in Azure to further help with the debugging effort.
Similarly to the getSubject method, I also have a getContent method.
ITicketException is not a class we can’t use the instanceof check, I created a helper to determine if a particular error implements the interface.
That is a wrap
That is how far I have gotten in this little Layered Error Handling approach. So far, it is working quite well. A bit of upfront effort to set up these exception classes however, I think the payoff is really good. It has also helped with testing as I am able to test to make sure that a particular method throws the exception that I am expecting if incorrect data is passed to it.Read my latest 'paper'
These 'papers' are bigger articles I write on topics around development and how to do it better.Module Driven Development
Discover the benefits of Module-Driven Development (MDD) in software development. Learn how MDD can improve the efficiency, reliability, and maintainability of large and complex systems. Read the article now to find out how your organization can benefit from MDD.