Skip to content

Commit

Permalink
adds ErrorFactory function
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeaudry committed Aug 19, 2018
1 parent 119c4b2 commit 79a5b30
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
4 changes: 3 additions & 1 deletion index.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import Model from './src/Model';
import Collection from './src/Collection';
import Enum from './src/Enum';
import Serializable from './src/Serializable';
import ErrorFactory from './src/ErrorFactory';

export {
Model,
Collection,
Enum,
Serializable
Serializable,
ErrorFactory
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const Model = require('./lib/Model');
const Collection = require('./lib/Collection');
const Enum = require('./lib/Enum');
const Serializable = require('./lib/Serializable');
const ErrorFactory = require('./lib/ErrorFactory');

module.exports = {
Model: Model.default,
Collection: Collection.default,
Enum: Enum.default,
Serializable: Serializable.default
Serializable: Serializable.default,
ErrorFactory: ErrorFactory.default
}
3 changes: 2 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export Model from './src/Model';
export Collection from './src/Collection';
export Enum from './src/Enum';
export Serializable from './src/Serializable';
export Serializable from './src/Serializable';
export ErrorFactory from './src/ErrorFactory';
17 changes: 17 additions & 0 deletions src/ErrorFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
*
* construct a custom error constructor and streamline
* the stack trace captured in the custom error
*
* @param {ErrorConstructor} (ErrorClass) [Error]
* @returns {Error}
*
*/
export default function ErrorFactory(ErrorClass=Error) {
return class CustomError extends ErrorClass {
constructor(...args) {
super(...args);
Error.captureStackTrace(this, CustomError);
}
}
}
21 changes: 21 additions & 0 deletions tests/errorfactor.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'ava';
import faker from 'faker';
import ErrorFactory from '../src/ErrorFactory';

test.beforeEach(t => {
const text = faker.random.word();
const ErrorConstructor = ErrorFactory();
const Error = new ErrorConstructor(text);
t.context = {
ErrorConstructor,
Error,
text
};
});

test('ErrorFactory() should return an Error Constructor', t => {
t.is(typeof ErrorFactory, 'function');
t.is(typeof t.context.ErrorConstructor, 'function');
t.is(typeof t.context.Error, 'object');
t.is(t.context.Error.message, t.context.text);
});

0 comments on commit 79a5b30

Please sign in to comment.