rduk-errors

1.2.3 • Public • Published

RDUK - errors

Build Status Coverage Status

Installation

npm install rduk-errors --save --save-exact

Usage

var errors = require('rduk-errors');

You can instantiate a new error from the available errors. see the list

var myarg = 1;
 
try {
    var err = new errors.ArgumentError('myarg', myarg);
    throw err;
} catch(err) {
    console.log(err instanceof Error); // will output true
    console.log(err instanceof errors.ArgumentError); // will output true
}

or throw it directly

try {
    errors.throwArgumentError('myarg', myarg);
} catch(err) {
    console.error(err);
    /**
     * will output:
     * { [ArgumentError: Invalid argument "myarg" (value: "1").]
     *   name: 'ArgumentError',
     *   message: 'Invalid argument "myarg" (value: "1").' }
     */
}

You can also add your own custom error

 
/* FakeError.js */
(function(module) {
 
    'use strict';
 
    module.exports = function FakeError(message) {
        FakeError.super_.call(this, message);
    };
 
} (module));
 
/* main.js */
var errors = require('rduk-errors');
errors.add('FakeError', require('pathToFakeError/FakeError'));
 
try {
    errors.throwFakeError('this is a fake error.');
} catch (err) {
    console.log(err instanceof errors.FakeError); // will output true
}
 

By default, your custom error inherits BaseError. The BaseError class is in charge to initialize all error specific properties.

/* BaseError.js */
(function(require, module) {
 
    'use strict';
 
    module.exports = function BaseError(message) {
        Error.captureStackTrace(this, this.constructor);
 
        this.name = this.constructor.name;
        this.message = message;
    };
 
    require('util').inherits(module.exports, Error);
 
} (require, module));

But you can, if needed, inherit from your own custom error

var errors = require('rduk-errors');
 
errors.add('ChildFakeError', function ChildFakeError() {
    ChildFakeError.super_.call(this, 'this is a fake error.');
}, errors.FakeError);
 
try {
    errors.throwChildFakeError();
} catch (err) {
    /* ... */
}

Available errors

  • errors.ArgumentNullError ( propertyName );
  • errors.ArgumentError ( propertyName , propertyValue );
  • errors.ArgumentOutOfRangeError ( propertyName );
  • errors.ConfigurationError ( message );
  • errors.NotImplementedError ( methodName );
  • errors.NotSupportedError ( methodName );

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.2.31latest

Version History

VersionDownloads (Last 7 Days)Published
1.2.31
1.2.20
1.2.10
1.2.00
1.1.00
1.0.10
1.0.00

Package Sidebar

Install

npm i rduk-errors

Weekly Downloads

1

Version

1.2.3

License

MIT

Last publish

Collaborators

  • khuang