From 356a49ca53df9258cefe524c9b57aab87f590a6d Mon Sep 17 00:00:00 2001 From: Chris Armstrong Date: Tue, 18 Jun 2019 17:31:31 +1000 Subject: [PATCH] fix: generation of Error class --- jmespath.js | 21 ++++++++++++++++----- test/jmespath.js | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/jmespath.js b/jmespath.js index d3993a2..1ed4609 100644 --- a/jmespath.js +++ b/jmespath.js @@ -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; } diff --git a/test/jmespath.js b/test/jmespath.js index 98d4ead..28f68cc 100644 --- a/test/jmespath.js +++ b/test/jmespath.js @@ -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); } }