1.0.12 • Published 10 years ago

exception-factory v1.0.12

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

exception-factory

License MIT NPM version Downloads Dependencies

Simple powerful exception factory to create custom error objects, with error code (!), error constants and optional prefixes.

I. Installation

npm install exception-factory --save

Use it like this:

var customException = require('exception-factory').build('customException');

customException.const('NOT_FOUND', '001');
customException.const('FATAL_ERROR', '002');

try {
   throw new customException('Oh no!', customException.FATAL_ERROR);
} catch (err) {
   console.log(err.name); // 'customException'
   console.log(err.code); // '002'
   console.log(err.message); // 'Oh no!'
}

or with Promise:

var validationException = require('exception-factory').build('validationException');
var typeException = require('exception-factory').build('typeException');

Promise.resolve(function(){
   throw new validationException('Password is too short');
})
.catch(validationException, function(err){
   // catched here!
   console.log(err);
   throw err;
})
.catch(typeException, function(err){
   // not this time!
   console.log(err);
   throw err;
})
.catch(function(err){
   // just for unexpected very vanilla errors!
   throw err;
});

You can also define universal prefix of exception:

var validationException = require('exception-factory').build('validationException', 'Validation exception: ');

try {
   throw new validationException('Password is too short');
} catch (err) {
   console.log(err); // Validation exception: Password is too short
}

License

MIT

1.0.12

10 years ago

1.0.11

10 years ago

1.0.10

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago