Skip to content

Commit

Permalink
Merge pull request #79 from Travelport-Ukraine/dev
Browse files Browse the repository at this point in the history
Milestone 0.1.1
  • Loading branch information
dchertousov authored Oct 20, 2016
2 parents 845d403 + 98936e2 commit a3a6719
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/generateFromList.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const UserValidationErrors = createErrorsList({
}, UserValidationError);


try{
try {
try {
throw new UserValidationErrors.NO_NAME({ user: { name: null } });
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const g = require('./generateFromList');
require('./generateFromList');
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-errors-helpers",
"version": "0.1.0",
"version": "0.1.1",
"description": "Some helpers for better error handling in Node.js",
"main": "index.js",
"files": [
Expand Down Expand Up @@ -31,7 +31,7 @@
"chai": "^3.5.0",
"eslint": "^3.6.0",
"eslint-config-airbnb": "^12.0.0",
"eslint-plugin-import": "^2.0.1",
"eslint-plugin-import": "^1.16.0",
"eslint-plugin-jsx-a11y": "^2.2.2",
"eslint-plugin-react": "^6.3.0",
"mocha": "^3.0.2",
Expand Down
5 changes: 5 additions & 0 deletions src/error-factory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const util = require('util');

const errorFactory = (name, message, baseType) => {
if (baseType && baseType !== Error) {
if ((typeof baseType) !== 'function' || (!(baseType.prototype instanceof Error))) {
throw new Error('baseType prototype should be an instance of Error');
}
}
const baseTypeName = baseType ? baseType.name : 'Error';

/* eslint-disable prefer-template */
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ describe('Generators', () => {
expect(BarError).to.be.a('Function');
expect(BarError.name).to.equal('BarError');
});
it('should create error class if Error passed explicitely', () => {
const BarError = createErrorClass('BarError', BAR_ERROR_MESSAGE, Error);
expect(BarError).to.be.a('Function');
expect(BarError.name).to.equal('BarError');
});
it('should fail if baseType is not an error', () => {
const createErrorWithBadBaseType = () => {
return createErrorClass('BarError', BAR_ERROR_MESSAGE, {});
};
expect(createErrorWithBadBaseType).to.throw(Error);
});
it('should create error instance without data', () => {
const BarError = createErrorClass('BarError', BAR_ERROR_MESSAGE);
const be = new BarError();
Expand Down

0 comments on commit a3a6719

Please sign in to comment.