Class: ErrorMessage

services.ErrorMessage(code, desc)

Helps generating and manipulating error messages. Comprises the error code and the error description template which contains zero or more placeholders to be replaced with arbitrary text during the generation of error message.

This class provides means to create error message out of the error code and description template with placeholders as well as to check whether an arbitrary Error object has the same error code (e.x. error code: #1000) as this ErrorMessage object.

Constructor

new ErrorMessage(code, desc)

Creates new ErrorMessage object out of an error code and an error description template.
Parameters:
Name Type Description
code number error code
desc string error description template with $-based placeholders such as $1, $2, etc.
Since:
  • 0.1.0
Source:
Throws:
Type
Error
Example

Example of creating ErrorMessage object

var ILLEGAL_ARGUMENT = new services.ErrorMessage(1000, 'Illegal argument(s). $1'); // $1 is a placeholder

Methods

isTypeOf(error) → {boolean}

Checks whether the given Error object has error code in its message property equal to the error code of this ErrorMessage object.
Parameters:
Name Type Description
error Error Error object to check its error code
Since:
  • 0.1.0
Source:
Returns:
whether the given Error object has error code in its message property equal to the error code of this ErrorMessage object
Type
boolean
Example

Example of using the isTypeOf method

var ILLEGAL_ARGUMENT = new services.ErrorMessage(1000, 'Illegal argument(s). $1');
try {
    throw new Error(ILLEGAL_ARGUMENT.toString('Not a number!'));
} catch (err){
    console.log(ILLEGAL_ARGUMENT.isTypeOf(err)); // true
}

toString(…replacement) → {string}

Creates error message text (string) based on the error code, the error description template, and the given placeholder replacement strings.
Parameters:
Name Type Attributes Description
replacement string <repeatable>
error message placeholder replacement string
Since:
  • 0.1.0
Source:
Returns:
error message text for this ErrorMessage object and the given placeholder replacements
Type
string
Example

Example of using the toString method on ErrorMessage object

var ILLEGAL_ARGUMENT = new services.ErrorMessage(1000, 'Illegal argument(s). $1');
...
throw new Error(ILLEGAL_ARGUMENT.toString('Not a number!')); // "Oops! Illegal argument(s). Not a number! (#1000)"