Some recent discussion in the WatchMeCode slack spawned a bit of research into creating custom errors through factory methods, while keeping the stack trace for those errors clean, in Node.js
After a bit of digging, I found a good solution using Node’s Error.captureStackTrace method, and recorded a quick screencast to highlight it’s use.
The Screencast
The Sample Code
If you’d like to run the sample code that I showed in this screencast, you can grab it from this gist.
Additional Resources
And if you would like to read up on errors further, check out these additional resources:
- Error.captureStackTrace documentation
- Creating Custom Error Objects In Node.js With Error.captureStackTrace()
- A String Is Not An Error
- The Art of Error