Home

Awesome

nodeON-error

Error Objects for everybody!

Build Status

Install

Install the module using NPM:

npm install nodeon-error --save

<a name='TOC'>Table of Contents</a>

  1. Overview
    1. Handling existing errors
    2. Error Properties
  2. API
    1. Signing the Error Objects
    2. Getting an API Safe version
  3. Error Types
    1. The Unknown Error
    2. The JSON Error
    3. The Database Error
    4. The Validation Error
      1. The Validation Item
    5. The Authentication Error

Overview

the nodeon-error package offers signed Error Objects which are an extension of the Error native Object.

var appError = require('nodeon-error');

// A new error with a message
var error = new appError.Error('A message');

<a name='existingErrors'>Handling Existing Errors</a>

When an Error gets thrown from any other library you may simply supply the alien Error Object to the first argument of the constructor. Once this is done nodeon-error will do the following:

var fs = require('fs');

var appError = require('nodeon-error');

function stat(filepath, cb) {
    fs.stat(filepath, function (err, stats) {
        if (err) {
            var ourErr = appError.Error(err);
            ourErr.message = 'Oppsy';
            cb(ourErr);
        } else {
            cb(null, stats);
        }
    });
}

// ....

stat('bogus', function(err, res) {
    console.log(err.message);
    // --> ENOENT, no such file or directory 'bogus'

    console.log(err.srcError);
    // --> Will display the original Error Object from fs.
});

[⬆]

<a name='properties'>Error Properties</a>

All Error Objects extend the Javascript native Error Object, thus have all built-in properties. Additionally, the following properties are augmenting the Error Object:

[⬆]

API

<a name='setName'>Signing the Error Objects</a>

appError.setName(name)

All Error Objects are signed by setting the name property. This method sets the name with which to prepend all signatures, make sure the first char is uppercase.

var appErr = require('nodeon-error');

appErr.setName('Myapp');

var error = new appErr.Error();

console.log(error.name);
// prints: "MyappBaseError"

[⬆]

<a name='toApi'>Getting an API Safe version</a>

errInstance.toApi()

Returns Object A sanitized object.

Clones the error object and strips it of all the Error getters (like stack) and the following attributes:

* `srcError`
var appErr = require('nodeon-error');

var error = new appErr.Error();

console.log(error.toApi());

[⬆]

Error Types

The following error types are available:

<a name='baseError'>The Base Error</a>

new appError.Error(optMessage)

This is the default base error object.

Instance Properties

[⬆]

<a name='unknownError'>The Unknown Error</a>

new appError.Unknown(optMessage)

This is for errors of unknown nature.

Instance Properties

[⬆]

<a name='jsonError'>The JSON Error</a>

new appError.JSON(exception)

This is for errors originating from JSON parsing or stringifying.

Instance Properties

[⬆]

<a name='databaseError'>The Database Error</a>

new appError.Database(optMessage)

This is for errors originating for the database.

Instance Properties

[⬆]

<a name='validationError'>The Validation Error</a>

new appError.Validation(optMessage)

This is for errors originating from validation operations.

Instance Properties

<a name='validationItem'>The Validation Item</a>

new appError.ValidationItem(message, optPath, optType, optValue)

Creates a Validation Item to inject to the Validation Error.

Validation Item Properties
Example Usage
var appError = require('nodeon-error');

var validationError = new appError.ValidationError();

var validItem = new appError.ValidationItem('Not valid email');
validItem.path = 'email';
validItem.type = 'invalid';
validItem.value = 'email@bogus';

validationError.errors.push(validItem);

[⬆]

<a name='authError'>The Authentication Error</a>

new appError.Authentication(optMessage)

This is for errors of authentication nature.

Instance Properties

[⬆]

Release History

License

Copyright (c) 2014 Thanasis Polychronakis. Licensed under the MIT license.