Skip to content

Commit

Permalink
test(GimmeError): Add append typeError argument to constructor,
Browse files Browse the repository at this point in the history
  • Loading branch information
johnny-cit committed May 8, 2019
1 parent 2aac335 commit 244cbd7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 3 additions & 2 deletions __tests__/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ describe('GimmeError', () => {
});

describe('with a custom message', function() {
const errorType = 'Type Error';
const customMessage = 'with a custom message!';
const error = new GimmeError(customMessage);
const error = new GimmeError(errorType, customMessage);

it("should capture the message in the instance's message property", function() {
expect(error.message).toBe(customMessage);
expect(error.message).toContain(customMessage);
});
});
});
Expand Down
16 changes: 12 additions & 4 deletions src/models/Error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
class GimmeError extends Error {
constructor(message) {
super(message);
constructor(
errorType,
message = 'Uh Oh! There was an error! Please review the stacktrace',
...args
) {
super(...args);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, GimmeError);
}

this.name = this.constructor.name;
this.message =
message || 'Uh Oh! There was an error! Please review the stacktrace';
this.message = `(${errorType}) ${message}`;
this.stack = new Error(message).stack;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/handleTypeErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const handleTypeErrors = (argsArray = null, schemaArray = null) => {
: `Argument must be a ${schemaArg.type}`;

throw new GimmeError(
`Type Error! ${dynamicMessage}. You entered "${
'Type Error',
`${dynamicMessage}. You entered "${
argsArray[i]
}" which is a ${typeof argsArray[i]}.`
);
Expand Down

0 comments on commit 244cbd7

Please sign in to comment.