From f0a84785dbc07c8fe3bb5bbc02e541ba206e7d96 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Tue, 19 Jan 2016 14:57:53 -0500 Subject: [PATCH] Better polyfill for Error.captureStackTrace in Meteor.makeErrorType. The trick of returning an object other than `this` from the constructor function makes the classes created by Meteor.makeErrorType impossible to subclass, unless the subclass is very careful to return the same object from its own constructor. Fortunately, the only reason we needed an actual Error instance in this code was to get access to its .stack property, which we can just as easily borrow from an auxiliary Error object. --- packages/meteor/errors.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/meteor/errors.js b/packages/meteor/errors.js index cf2777dab8..dfff723900 100644 --- a/packages/meteor/errors.js +++ b/packages/meteor/errors.js @@ -4,26 +4,19 @@ // Meteor.makeErrorType = function (name, constructor) { var errorClass = function (/*arguments*/) { - var self = this; - // Ensure we get a proper stack trace in most Javascript environments if (Error.captureStackTrace) { // V8 environments (Chrome and Node.js) - Error.captureStackTrace(self, errorClass); + Error.captureStackTrace(this, errorClass); } else { - // Firefox - var e = new Error; - e.__proto__ = errorClass.prototype; - if (e instanceof errorClass) - self = e; + // Borrow the .stack property of a native Error object. + this.stack = new Error().stack; } // Safari magically works. - constructor.apply(self, arguments); + constructor.apply(this, arguments); - self.errorType = name; - - return self; + this.errorType = name; }; Meteor._inherits(errorClass, Error);