ZErr
Custom JS error creator that subclasses Error
, sets .name
.stack
and .message
, and provides simple interpolation to construct error messages.
var zerr = // first param: error name// second param: string template for error messages (substitutes '%' with args in constructor)var BadParam = // using the created error:try throw 'foo' 'string' catch e console // => 'BadParamError' console // => 'foo must be a string' console // => 'BadParamError: foo must be a string\nat repl:1:13...' console // => true console // => true // the `new` is optionalthrow // if no string template is given, zerr will just use the constructor's param as the messagevar BadParam = try throw 'foo is bad' catch e console // => 'foo is bad' // if you pass an error as the first param to the constructor, zerr will add its stack to the stack history// (taken from https://github.com/dominictarr/explain-error)try throw 'earlier foo was bad' 'foo is bad' catch e console /* => BadPararmError: foo is bad at repl:1:7 BadPararmError: earlier foo was bad at repl:1:15 */