2.3.1
A base class to use as a replacement for the standard Error class. Your custom error types should inherit from this class in order to make use of its constructor pattern and fullName resolution capabilities.
Extends Error
(string?)
Same as
options.shortMessage
. The value in
options
will take priority, if both are present.
(Error?)
Same as
options.cause
. The value in
options
will
take priority, if both are present.
(object
= {}
)
Object containing error configuration.
Name | Description |
---|---|
options.shortMessage string?
|
A human-readable error message.
The cause message, if any, will be chained onto the end of this to create
the full error message, ensuring human programmers can easily understand
the whole cause chain at a glance. If not provided, the shortMessage will
be determined using the static
getDefaultMessage
method.
|
options.cause Error
(default null )
|
Cause of the error, for cause chains. |
options.info Object
(default null )
|
Additional data about the error. |
options.hideCauseMessage boolean
(default false )
|
If set to true, the cause message will not be chained to the shortMessage, and the error's full message will simply be the shortMessage. Useful for when you want to simplify error messages but still preserve the cause chain. |
The dot-separated full name of the error constructor. This is lazily evaluated and consists of this constructor's name apppended to the superclass's fullName, separated by a dot. If the superclass has no fullName, it is assumed to be 'Error'.
This is a read-only property and should not be overridden, as it is critical to nani's mechanism for checking against error name heirarchies.
Type: string
Fetches the shortMessage which will be used for any instance that is not provided with one. By default, it returns a generic message, but it may be overridden to provide custom default messages for your subclasses.
(Object)
The value of
options.info
provided to the
constructor, if any, or an empty object otherwise. Allows you to easily
include values from
options.info
in your default messages.
string
:
Default shortMessage.
Error class for representing a group of errors. This class should not be inherited. Instead, the errors inside it should be instances of Error or NaniError subclasses.
The shortMessage of a MultiError will be based on how many errors are provided to it.
Only the first error will be treated as the primary cause and actually
included in the full message, but all errors (including that one) will be
stored on an errors
property for future use.
Extends NaniError
(...Error)
Error instances provided directly as arguments,
instead of as an array. If provided with an errors array, these will be
ignored.
Gets the fullName of both sup and err and returns true if and only if err's
fullName starts with sup's fullName. This allows for checking errors against
class heirarchies without relying on instanceof
.
boolean
:
true
if err fullName starts with sup fullName,
false
otherwise.
Returns the full name of an Error instance or constructor. If no fullName property is found, it attempts to determine one based on the name property. If the name is simply 'Error', then so too is the fullName. If the name ends with 'Error', then the fullName will be the name appended to 'Error', separated by a dot. This enables some support for error classes that are not subclasses of NaniError, by assuming they're at least instances of Error.
(string | null)
:
Full name if it can be determined, null otherwise.
Returns the stack of an error, with the stacks of each error in its cause
chain appended. This will include only the first error in the case of a
MultiError
.
(Error)
An error instance, potentially with a cause chain.
string
:
Full error stack.
A generator function for iterating through an error and all of its causes. When multi-errors are encountered, this function will iterate down the entire cause chain of each embedded error in sequence. If an error would appear more than once in the sequence, subsequent appearances will be ignored.
(Error)
Error instance to iterate.
Another generator function, similar to iterate
, except that it does not
skip duplicate references, and yields objects with some information about
each error's context within the structure instead of just the error itself.
It does check for circular references that would create an infinite loop,
however, and ignores those just to be safe.
Error info is yielded in the form of objects with three properties:
err
- The Error instance.parent
- The parent of err
, if any. null
otherwise.inArray
- true
if err
is in an errors
array on its parent,
false
otherwise.This function will normally give you a bit more informaiton than you need, but it can be useful for writing serializers, among other things.
(Error)
Error instance to iterate.
Iterates over an error and all of its causes, returning the first error for
which a predicate returns truthy. Normally, the predicate will be invoked
with each cause as its first argument. However, if it is an Error
constructor, this function will instead find a cause for which is
returns
true for that constructor.
(Error | null)
:
First matching error, or null if none is found.
Similar to findCause
, except that it returns an array of all matched
errors, instead of just the first.
Array<Error>
:
Array of matching errors.
Iterates through the info properties of an error and all of its causes, collapsing all properties into a single object. Values encountered earlier in the cause chain are prioritized.
(Error)
Error instance from which to fetch info.
Object
:
A plain object containing all info properties.
Converts an array of errors into a single error, wrapping it with a MultiError if necessary.
(MultiError | Error | null)
:
null if
errors
is empty, the first
element if
errors
has only one element, or a MultiError wrapping
errors
if
errors
has more than one element.