howhap list
npm install --save howhap-list
Stores a list of howhap errors for easy manipulation and display.
Usage
let HowhapList = ; let list = default: message: 'An unknown error occurred' status: 500 email: message: '{{email}} is an invalid email address' status: 400 params: email: 'foo' ;
Constructor
let list = errors options;
Both errors and options are optional. Errors can be a plain vanilla javascript object that represents the list of keyed errors.
Methods
HowhapList.add(messageAndStatus, params, key)
Adds a new error to the list. The key defaults to 'default'. Below are some examples...
messageStatus can be an object
list; // RESULT:/*{ email: { message: '{{email}} is an invalid email address', status: 400, params: { email: 'foo' } }}*/
messageStatus can be a string if the availableErrors option is supplied
list; // RESULT:/*{ email: { message: '{{email}} is an invalid email address', status: 400, params: { email: 'foo' } }}*/
neither params nor key are required
list; // RESULT:/*{ email: { message: '{{email}} is an invalid email address', status: 400 }}*/
params can be supplied with out a key. The key will default to 'default'
list; // RESULT:/*{ default: { message: '{{email}} is an invalid email address', status: 400, params: { email: 'foo' } }}*/
key can be supplied with out a params. The params will default to an empty object
list; // RESULT:/*{ email: { message: '{{email}} is an invalid email address', status: 400, params:{} }}*/
HowhapList.remove(key)
Removes an error by its key.
list;
HowhapList.toJSON()
Returns a JSON representation of the howhap list with error objects rendered to strings.
let plainObject = list;
HowhapList.toObject()
Returns a JSON representation of the howhap list with error objects preserved.
let plainObject = list;
HowhapList.display(key)
Displays a specific error based on its (optional) key
let list = default: message: 'An unknown error occurred' status: 500 email: message: '{{email}} is an invalid email address' status: 400 params: email: 'foo' ; listdisplay; // 'An unknown error occurredlistdisplay'default'; // 'An unknown error occurredlistdisplay'email'; // foo is an invalid email address
Options
options.availableErrors
availableErrors
can hold an objet of errors that can be easily added to the list. For example:
// Errors are defined once in the options argumentlet list = null availableErrors: API: UNKNOWN: message: 'An unknown error occurred.' status: 500 NOT_FOUND: message: 'The model with id {{id}} was not found.' status: 404 PERMISSION_DENIED: message: 'You don\'t have permission to {{action}} the resource {{resource}}' status: 401 EMAIL: UNREACHABLE: message: 'The email address {{email}} is unreachable.' status: 400 ; // Errors can be easily added via a string list;list;list;
options.logger
logger
is an object that will be called each time an error is added. Works natively with bunyan but you can also add your own logger. It should adhere to the following interface (for example):
let logger = { console; } { console; } { console; };
Here's an example of how to configure your logger.
let bunyan = ;let HowhapList = ; let list = null logger: bunyan;