node-error-ex
Easily subclass and customize new Error types
Examples
To include in your project:
To create an error message type with a specific name (note, that ErrorFn.name
will not reflect this):
To add a stack line:
To append to the error message:
API
errorEx([name], [properties])
errorEx([name], [properties])
Creates a new ErrorEx error type
name
: the name of the new type (appears in the error message upon throw; defaults toError.name
)properties
: if supplied, used as a key/value dictionary of properties to use when building up the stack message. Keys are property names that are looked up on the error message, and then passed to function values.line
: if specified and is a function, return value is added as a stack entry (error-ex will indent for you). Passed the property value given the key.stack
: if specified and is a function, passed the value of the property using the key, and the raw stack lines as a second argument. Takes no return value (but the stack can be modified directly).message
: if specified and is a function, return value is used as new.message
value upon get. Passed the property value of the property named by key, and the existing message is passed as the second argument as an array of lines (suitable for multi-line messages).
Returns a constructor (Function) that can be used just like the regular Error constructor.
errorEx.line(str)
errorEx.line(str)
Creates a stack line using a delimiter
This is a helper function. It is to be used in lieu of writing a value object for
properties
values.
str
: The string to createUse the delimiter
%s
to specify where in the string the value should go
errorEx.append(str)
errorEx.append(str)
Appends to the error.message
string
This is a helper function. It is to be used in lieu of writing a value object for
properties
values.
str
: The string to appendUse the delimiter
%s
to specify where in the string the value should go
License
Licensed under the MIT License. You can find a copy of it in LICENSE.
Last updated