Classes

NaniError

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.

new NaniError(shortMessage: string?, cause: Error?, options: object)

Extends Error

Parameters
shortMessage (string?) Same as options.shortMessage . The value in options will take priority, if both are present.
cause (Error?) Same as options.cause . The value in options will take priority, if both are present.
options (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.
Static Members
fullName
getDefaultMessage(info)
Instance Members
shortMessage
cause
info
name
fullName

MultiError

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.

new MultiError(errors: Array<Error>?, errors: ...Error)

Extends NaniError

Parameters
errors (Array<Error>?) Array of error instances to include in the group.
errors (...Error) Error instances provided directly as arguments, instead of as an array. If provided with an errors array, these will be ignored.
Instance Members
errors

Utility Functions

is

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.

is(sup: Function?, err: (Error | Function)?): boolean
Parameters
sup (Function?) Error constructor. If omitted or falsy, this function will return false .
err ((Error | Function)?) Error constructor or instance. If omitted or falsy, this function will return false .
Returns
boolean: true if err fullName starts with sup fullName, false otherwise.

getFullName

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.

getFullName(err: (Error | Function)?): (string | null)
Parameters
err ((Error | Function)?) An Error instance or constructor. If omitted or falsy, this function will return null.
Returns
(string | null): Full name if it can be determined, null otherwise.

getFullStack

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.

getFullStack(err: Error): string
Parameters
err (Error) An error instance, potentially with a cause chain.
Returns
string: Full error stack.

iterate

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.

iterate(err: Error)
Parameters
err (Error) Error instance to iterate.

iterateFull

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.

iterateFull(err: Error)
Parameters
err (Error) Error instance to iterate.

find

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.

find(err: Error, predicate: Function): (Error | null)
Parameters
err (Error) Error instance to search.
predicate (Function) Predicate function or Error constructor.
Returns
(Error | null): First matching error, or null if none is found.

filter

Similar to findCause, except that it returns an array of all matched errors, instead of just the first.

filter(err: Error, predicate: Function): Array<Error>
Parameters
err (Error) Error instance to search.
predicate (Function) Predicate function or Error constructor.
Returns
Array<Error>: Array of matching errors.

collapseInfo

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.

collapseInfo(err: Error): Object
Parameters
err (Error) Error instance from which to fetch info.
Returns
Object: A plain object containing all info properties.

fromArray

Converts an array of errors into a single error, wrapping it with a MultiError if necessary.

fromArray(errors: Array<Error>): (MultiError | Error | null)
Parameters
errors (Array<Error>) Array of errors to convert.
Returns
(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.