Parserror

Parserror

The main class of the library. It allows you to create cases, parsers and scopes.

Constructor

new Parserror(optionsopt)

Source:
Parameters:
Name Type Attributes Default Description
options Partial.<ParserrorOptions> <optional>
{}

The options to customize how the class behaves.

Members

globalScopeName :string

Source:

The name of the global scope.

Type:

Methods

addCase(definition, scopeopt, nullable) → {Parserror}

Source:

Add a new error case.

Parameters:
Name Type Attributes Default Description
definition ErrorCaseDefinition

The case definition settings.

scope string <optional>
<nullable>
null

The name of the scope where the case should be added. If not defined, it will be added to the global scope.

Returns:

For chaining purposes.

Type
Parserror

addCases(definitions, scopeopt, nullable) → {Parserror}

Source:

Adds a list of error cases.

Parameters:
Name Type Attributes Default Description
definitions Array.<ErrorCaseDefinition>

The cases' definitions.

scope string <optional>
<nullable>
null

The name of the scope where the cases should be added. If not defined, they will be added to the global scope.

Returns:

For chaining purposes.

Type
Parserror

addParser(name, parser, scopenullable) → {Parserror}

Source:

Adds a reusable parser.

Parameters:
Name Type Attributes Default Description
name string

The name of the parser.

parser Object.<string, any> | function

The parser function or map. This is the second parameter for CaseParser#constructor.

scope string <nullable>
null

The name of the scope where the parser should be added. If not defined, it will be added to the global scope.

Returns:

For chaining purposes.

Type
Parserror

addScope(name, casesopt, allowedOriginalsopt, overwriteopt) → {Parserror}

Source:

Creates a new scope.

Parameters:
Name Type Attributes Default Description
name string

The name of the scope.

cases Array.<ErrorCaseDefinition> <optional>
[]

A list of cases' defintions to add.

allowedOriginals Array.<Condition> <optional>
[]

A list of conditions/definitions for cases that allow original messages to be matched. To better understand how this work, please read the description of Parserror#allowOriginal.

overwrite boolean <optional>
false

If there's a scope with the same name already, using this flag allows you to overwrite it.

Throws:

If overwrite is false and there's already a scope with the same name.

Type
Error
Returns:

For chaining purposes.

Type
Parserror

allowOriginal(condition, scopeopt, nullable) → {Parserror}

Source:

Allows a specific error message to be matched. The idea is for this feature to be used with fallback messages: If you want a message to be used as it is but at the same time you want to use a fallback message, you would use this method; the original message won't be discarded and you still have the fallback for messages that don't have a match.

Parameters:
Name Type Attributes Default Description
condition Condition

Internally, this method will generate a new ErrorCase, so this parameter can be a string or a regular expression to match the error message, or an actual case definition. By default, the created case will have a random string as a name, but you can use a case definition to specify the name you want.

scope string <optional>
<nullable>
null

The name of the scope where the case should be added. If not defined, it will be added to the global scope.

Returns:

For chaining purposes.

Type
Parserror

allowOriginals(conditions, scopeopt, nullable) → {Parserror}

Source:

Allows for multiple error messages to be matched. This is the "bulk alias" of Parserror#allowOriginal, so please read the documentation of that method to better understand in which case you would want to allow original messages.

Parameters:
Name Type Attributes Default Description
conditions Array.<Condition>

The list of conditions/definitions for the cases that will match the messages.

scope string <optional>
<nullable>
null

The name of the scope where the cases should be added. If not defined, they will be added to the global scope.

Returns:

For chaining purposes.

Type
Parserror

getScope(name, createopt) → {Scope}

Source:

Gets a scope by its name.

Parameters:
Name Type Attributes Default Description
name string

The name of the scope.

create boolean <optional>
true

If true and the scope doesn't exist, it will try to create it.

Throws:

If create is false and the scope doesn't exist.

Type
Error
Returns:
Type
Scope

parse(error, optionsopt) → {FormattedError}

Source:

Parses and formats an error.

Parameters:
Name Type Attributes Default Description
error Error | string | ParserrorErrorObject

The error to parse.

options Partial.<ParserrorParseOptions> <optional>
{}

Options to customize how the parsing is done.

Throws:

If error is not an Error, a string or a ParserrorErrorObject.

Type
TypeError
Returns:
Type
FormattedError

removeScope(name)

Source:

Removes a scope.

Parameters:
Name Type Description
name string

The name of the scope to remove.

Throws:

If you try to remove the global scope.

Type
Error

wrap(cases, scopes, fallbackopt, nullable) → {ParserrorWrapper}

Source:

Creates a wrapper: a pre configured parser to format errors with specific cases and/or scopes.

Parameters:
Name Type Attributes Default Description
cases Array.<string>

A list of cases' names.

scopes Array.<string>

A list of scopes' names.

fallback string <optional>
<nullable>
null

A fallback message in case the error can't be parsed. If not specified, the returned error will maintain the original message.

Returns:
Type
ParserrorWrapper

wrapForScopes(scopes, fallbackopt, nullable) → {ParserrorWrapper}

Source:

Creates a wrapper for specific scopes. A wrapper is a pre configured parser to format errors with specific cases and/or scopes.

Parameters:
Name Type Attributes Default Description
scopes Array.<string>

A list of scopes' names.

fallback string <optional>
<nullable>
null

A fallback message in case the error can't be parsed. If not specified, the returned error will maintain the original message.

Returns:
Type
ParserrorWrapper

(static) new(optionsopt) → {Parserror}

Source:

Create a new instance of Parserror.

Parameters:
Name Type Attributes Description
options ParserrorOptions <optional>

The options to customize how the class behaves.

Returns:
Type
Parserror