TritonDataCenter/node-verror

factory method for VError

Opened this issue · 0 comments

The VError() constructor is agnostic on the subject of the new operator: an instance of VError will be created whether the consumer uses the new operator, or simply calls VError() as a regular function.

Unfortunately, due to the way in which Error.captureStackTrace() appears to function, at least in Node 4.6.0, omitting new makes it impossible to elide one of the VError implementation stack frames from the stack output. I tried modifying the code to pass VError as the constructorOpt to captureStackTrace(), but V8 appears to conflate VError the function and VError the constructor in the stack, and elides only the topmost instance.

For those who wish to avoid the need for new, but still want clean stack traces, we could add a new VError.create() factory method. This would accept the same arguments as VError(), but result in a cleaner stack trace.