-
Notifications
You must be signed in to change notification settings - Fork 850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate stack trace on the Error constructor #153
Comments
Thinking of making this change for V8 compatibility -- any comments would be appreciated. |
The stack property is not defined even after throwing the error.
Apart from that, it seems like IE11 also behaves similar to your description. I found this code in web-platform tests: var stack = new Error().stack;
// IE11 does not initialize 'Error.stack' until the object is thrown.
if (!stack) {
try {
throw new Error();
} catch (e) {
stack = e.stack;
}
} |
Ah -- Rhino does not generate stack traces at all unless "debug" is $ java -jar buildGradle/libs/rhino-1.7.8-SNAPSHOT.jar -debug On Tue, Jul 21, 2015 at 12:46 AM, hrj notifications@github.com wrote:
Greg Brail | apigee https://apigee.com/ | twitter @gbrail |
I stumbled upon this issue when I tried to run a node script that tries to parse |
|
Yes -- this would help with Node compatibility. However it'd have to be On Thu, Nov 5, 2015 at 5:06 AM, jochenberger notifications@github.com
Greg Brail | apigee https://apigee.com/ | twitter @gbrail |
Hate to bring this back from the dead, but is there any word or updates available on this exact issue? |
@ascendancy05 You can work around this by using a descendant class instead of the original Error class: var UserError = function (messsage){
this.message = message;
if (Error.captureStackTrace)
Error.captureStackTrace(this, UserError);
else
try {
throw new Error();
} catch(e){
this.stack = e.stack || e.stacktrace;
}
};
UserError.prototype = Object.create(Error.prototype);
UserError.prototype.constructor = UserError;
UserError.prototype.name = "UserError"; function main(){
throw new UserError("blabla");
} I guess it will add an extra frame to the stack string in your environment. I am working on a framework currently, which will return the same stack in every js environment, but it is a very hard problem if you want to solve it properly. |
According to the release documentation, the V8 Implementation was added in version 1.7.6 (April 2015): |
Any workaround for this? |
Today in Rhino, the "stack" property of an Error is generated when the Error is thrown, and derived from the Java stack trace.
In V8, however, the "stack" property is populated when the Error is constructed.
In other words, the code below:
var e = new Error();
throw e;
In V8, the line number on the stack trace will be the line where "new Error" was called. In Rhino, it is where "throw" was called.
Similarly, after the first line above, the "stack" property in V8 will show the stack trace, whereas in Rhino it will be undefined.
However, making this change will potentially slow down exception-handling code because the stack trace will be gathered on every error, and then Java will gather the stack trace again when it is thrown.
So, I propose adding a new feature flag to Context that is off by default. When the feature is enabled, Rhino will capture the stack trace when the Error object is first constructed.
The text was updated successfully, but these errors were encountered: