Skip to content

Commit

Permalink
fix: generation of Error class
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Armstrong committed Jun 18, 2019
1 parent d427ce8 commit 356a49c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 16 additions & 5 deletions jmespath.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,23 @@
}

function createErrorClass(className) {
var ExtendedError = function (message) {
Error.call(this, message);
this.name = className;
function ExtendedError(message) {
var instance = new Error(message);
instance.name = className;
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
if (Error.captureStackTrace) {
Error.captureStackTrace(instance, ExtendedError)
}
return instance;
};
ExtendedError.prototype = Object.create(Error.prototype);
ExtendedError.prototype.constructor = ExtendedError;
ExtendedError.prototype = Object.create(Error.prototype, {
constructor: {
value: Error,
enumerable: false,
writeable: true,
configurable: true,
},
});

return ExtendedError;
}
Expand Down
1 change: 1 addition & 0 deletions test/jmespath.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ describe('search', function() {
assert(e.message.search(
'expected argument 1 to be type string,array,object'
), e.message);
assert.strictEqual(e.name, 'TypeMismatchError');
assert(e.message.search('received type null'), e.message);
}
}
Expand Down

0 comments on commit 356a49c

Please sign in to comment.