Skip to content

Commit

Permalink
Expose inner error message alongside consumer provided message (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Desmarais authored Feb 19, 2019
1 parent 34358f4 commit c2df54b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"babel-minify": "0.5.0",
"babel-preset-env": "1.7.0",
"documentation": "9.1.1",
"eslint": "5.12.0",
"eslint": "5.12.1",
"is-docker": "1.1.0",
"istanbul": "0.4.5",
"istanbul-instrumenter-loader": "3.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export default function coerce (value, Type, message) {
return new Type(value);
}
catch (error) {
throw new TypeError(message);
throw new TypeError(`${message} Inner Error: ${error.message}`);
}
}
13 changes: 11 additions & 2 deletions src/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import coerce from './index';

const ERROR_MESSAGE = 'You failing the hardest';
class FakeClassForTesting {}
class FailingClassForTesting {
constructor() {
throw new Error('You failing the hardest');
throw new Error(ERROR_MESSAGE);
}
}
describe('coerce', () => {
Expand All @@ -26,6 +27,14 @@ describe('coerce', () => {
it('should cast into the type and return Typed version', () => {
const errorMessage = 'south things have gone';

expect(() => coerce({}, FailingClassForTesting, errorMessage)).toThrowError(errorMessage);
const errorMessageRegex = new RegExp(`${errorMessage}`);
expect(() => coerce({}, FailingClassForTesting, errorMessage)).toThrowError(errorMessageRegex);
});

it('should add inner message to error message', () => {
const errorMessage = 'south things have gone';

const errorMessageRegex = new RegExp(`${errorMessage}.*${ERROR_MESSAGE}`);
expect(() => coerce({}, FailingClassForTesting, errorMessage)).toThrowError(errorMessageRegex);
});
});

0 comments on commit c2df54b

Please sign in to comment.