Comparing version 0.0.2 to 0.1.0
'use strict'; | ||
var tourism = require('tourism'); | ||
const tourism = require('tourism'); | ||
@@ -10,3 +10,3 @@ module.exports = tourism({ | ||
server: { | ||
language: 'es5' | ||
language: 'es2015' | ||
} | ||
@@ -13,0 +13,0 @@ } |
'use strict'; | ||
var util = require('util'); | ||
const util = require('util'); | ||
var defekt = function (errorNames) { | ||
var errors = {}; | ||
if (!errorNames) { | ||
const defekt = function (errorDefinitions) { | ||
if (!errorDefinitions) { | ||
throw new Error('Error names are missing.'); | ||
} | ||
errorNames.forEach(function (errorName) { | ||
var CustomError = function (message, cause) { | ||
const errors = {}; | ||
errorDefinitions.forEach(errorDefinition => { | ||
let errorName = errorDefinition; | ||
let errorCode; | ||
if (typeof errorDefinition === 'object') { | ||
errorName = errorDefinition.name; | ||
errorCode = errorDefinition.code; | ||
} | ||
const CustomError = function (message, cause) { | ||
Error.call(this); | ||
this.name = errorName; | ||
this.code = errorCode; | ||
this.message = message || ''; | ||
@@ -18,0 +27,0 @@ this.cause = cause; |
{ | ||
"name": "defekt", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "defekt is custom errors made simple.", | ||
@@ -19,4 +19,4 @@ "contributors": [ | ||
"grunt": "0.4.5", | ||
"assertthat": "0.4.2", | ||
"tourism": "0.13.2" | ||
"assertthat": "0.6.0", | ||
"tourism": "0.21.0" | ||
}, | ||
@@ -27,3 +27,7 @@ "repository": { | ||
}, | ||
"keywords": [ | ||
"error", | ||
"exception" | ||
], | ||
"license": "MIT" | ||
} |
@@ -16,3 +16,3 @@ # defekt | ||
```javascript | ||
var defekt = require('defekt'); | ||
const defekt = require('defekt'); | ||
``` | ||
@@ -23,3 +23,3 @@ | ||
```javascript | ||
var errors = defekt([ | ||
const errors = defekt([ | ||
'ArgumentNull', | ||
@@ -51,2 +51,16 @@ 'InvalidOperation', | ||
### Defining error codes | ||
Additionally, you may want to provide error codes. For that specify an object with a `name` and a `code` property instead of only providing the error name. | ||
```javascript | ||
const errors = defekt([ | ||
{ name: 'ArgumentNull', code: 'ARGNULL' }, | ||
{ name: 'InvalidOperation', code: 'INVALOP' }, | ||
{ name: '...', code: '...' } | ||
]); | ||
``` | ||
Please note that you can mix both definition types arbitrarily. | ||
## Running the build | ||
@@ -53,0 +67,0 @@ |
'use strict'; | ||
var util = require('util'); | ||
const util = require('util'); | ||
var assert = require('assertthat'); | ||
const assert = require('assertthat'); | ||
var defekt = require('../lib/defekt'); | ||
const defekt = require('../lib/defekt'); | ||
suite('defekt', function () { | ||
test('is a function.', function (done) { | ||
suite('defekt', () => { | ||
test('is a function.', done => { | ||
assert.that(defekt).is.ofType('function'); | ||
@@ -15,4 +15,4 @@ done(); | ||
test('throws an error if error names are missing.', function (done) { | ||
assert.that(function () { | ||
test('throws an error if error names are missing.', done => { | ||
assert.that(() => { | ||
defekt(); | ||
@@ -23,3 +23,3 @@ }).is.throwing('Error names are missing.'); | ||
test('returns an object.', function (done) { | ||
test('returns an object.', done => { | ||
assert.that(defekt([ 'InvalidOperation' ])).is.ofType('object'); | ||
@@ -29,5 +29,5 @@ done(); | ||
suite('errors', function () { | ||
test('contains the specified errors.', function (done) { | ||
var errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
suite('errors', () => { | ||
test('contains the specified errors.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
@@ -39,6 +39,6 @@ assert.that(errors.InvalidOperation).is.ofType('function'); | ||
suite('CustomError', function () { | ||
test('is an error.', function (done) { | ||
var errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
var error = new errors.InvalidOperation(); | ||
suite('CustomError', () => { | ||
test('is an error.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
const error = new errors.InvalidOperation(); | ||
@@ -49,5 +49,5 @@ assert.that(error).is.instanceOf(Error); | ||
test('is recognized by util.isError.', function (done) { | ||
var errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
var error = new errors.InvalidOperation(); | ||
test('is recognized by util.isError.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
const error = new errors.InvalidOperation(); | ||
@@ -58,6 +58,6 @@ assert.that(util.isError(error)).is.true(); | ||
suite('name', function () { | ||
test('contains the given name.', function (done) { | ||
var errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
var error = new errors.InvalidOperation(); | ||
suite('name', () => { | ||
test('contains the given name.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
const error = new errors.InvalidOperation(); | ||
@@ -69,7 +69,28 @@ assert.that(error.name).is.equalTo('InvalidOperation'); | ||
suite('message', function () { | ||
test('contains an empty string if no message was given.', function (done) { | ||
var errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
var error = new errors.InvalidOperation(); | ||
suite('code', () => { | ||
test('is undefined by default.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
const error = new errors.InvalidOperation(); | ||
assert.that(error.code).is.undefined(); | ||
done(); | ||
}); | ||
test('is set to the given value.', done => { | ||
const errors = defekt([ | ||
{ name: 'InvalidOperation', code: 'INVOP' }, | ||
'ArgumentNull' | ||
]); | ||
const error = new errors.InvalidOperation(); | ||
assert.that(error.code).is.equalTo('INVOP'); | ||
done(); | ||
}); | ||
}); | ||
suite('message', () => { | ||
test('contains an empty string if no message was given.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
const error = new errors.InvalidOperation(); | ||
assert.that(error.message).is.equalTo(''); | ||
@@ -79,5 +100,5 @@ done(); | ||
test('contains the given message.', function (done) { | ||
var errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
var error = new errors.InvalidOperation('foobar'); | ||
test('contains the given message.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
const error = new errors.InvalidOperation('foobar'); | ||
@@ -89,6 +110,6 @@ assert.that(error.message).is.equalTo('foobar'); | ||
suite('cause', function () { | ||
test('is undefined if no inner error is given.', function (done) { | ||
var errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
var error = new errors.InvalidOperation('foobar'); | ||
suite('cause', () => { | ||
test('is undefined if no inner error is given.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
const error = new errors.InvalidOperation('foobar'); | ||
@@ -99,6 +120,6 @@ assert.that(error.cause).is.undefined(); | ||
test('contains the given inner error.', function (done) { | ||
var errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
var cause = new errors.ArgumentNull(); | ||
var error = new errors.InvalidOperation('foobar', cause); | ||
test('contains the given inner error.', done => { | ||
const errors = defekt([ 'InvalidOperation', 'ArgumentNull' ]); | ||
const cause = new errors.ArgumentNull(); | ||
const error = new errors.InvalidOperation('foobar', cause); | ||
@@ -105,0 +126,0 @@ assert.that(error.cause).is.equalTo(cause); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67134
8
138
79