tgriesser / checkit

simple, flexible validations for node and the browser
MIT License
223 stars 53 forks source link

Set stack traces correctly for custom errors #97

Closed thomsbg closed 4 years ago

thomsbg commented 5 years ago

I noticed in my app that the stack traces for instances of CheckitError indicate where the error function was defined, rather than where it is raised. I tracked this down to CheckitError.prototype.stack being set via CheckitError.prototype = new Error.

To fix, I added some logic to abstract the process of creating a custom error type, and to calculate the stack trace when created, using Error.captureStackTrace if available, or via a cleaned version of new Error(msg).stack otherwise.