Skip to content

Commit a3a6719

Browse files
authored
Merge pull request #79 from Travelport-Ukraine/dev
Milestone 0.1.1
2 parents 845d403 + 98936e2 commit a3a6719

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

examples/generateFromList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const UserValidationErrors = createErrorsList({
1515
}, UserValidationError);
1616

1717

18-
try{
18+
try {
1919
try {
2020
throw new UserValidationErrors.NO_NAME({ user: { name: null } });
2121
} catch (e) {

examples/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const g = require('./generateFromList');
1+
require('./generateFromList');

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-errors-helpers",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Some helpers for better error handling in Node.js",
55
"main": "index.js",
66
"files": [
@@ -31,7 +31,7 @@
3131
"chai": "^3.5.0",
3232
"eslint": "^3.6.0",
3333
"eslint-config-airbnb": "^12.0.0",
34-
"eslint-plugin-import": "^2.0.1",
34+
"eslint-plugin-import": "^1.16.0",
3535
"eslint-plugin-jsx-a11y": "^2.2.2",
3636
"eslint-plugin-react": "^6.3.0",
3737
"mocha": "^3.0.2",

src/error-factory.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const util = require('util');
22

33
const errorFactory = (name, message, baseType) => {
4+
if (baseType && baseType !== Error) {
5+
if ((typeof baseType) !== 'function' || (!(baseType.prototype instanceof Error))) {
6+
throw new Error('baseType prototype should be an instance of Error');
7+
}
8+
}
49
const baseTypeName = baseType ? baseType.name : 'Error';
510

611
/* eslint-disable prefer-template */

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ describe('Generators', () => {
2929
expect(BarError).to.be.a('Function');
3030
expect(BarError.name).to.equal('BarError');
3131
});
32+
it('should create error class if Error passed explicitely', () => {
33+
const BarError = createErrorClass('BarError', BAR_ERROR_MESSAGE, Error);
34+
expect(BarError).to.be.a('Function');
35+
expect(BarError.name).to.equal('BarError');
36+
});
37+
it('should fail if baseType is not an error', () => {
38+
const createErrorWithBadBaseType = () => {
39+
return createErrorClass('BarError', BAR_ERROR_MESSAGE, {});
40+
};
41+
expect(createErrorWithBadBaseType).to.throw(Error);
42+
});
3243
it('should create error instance without data', () => {
3344
const BarError = createErrorClass('BarError', BAR_ERROR_MESSAGE);
3445
const be = new BarError();

0 commit comments

Comments
 (0)