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
Make stack property available on new Error() too #109
Conversation
I was looking for a way to get the stack property on custom errors and came up with this, which is a bit more generic, I think, as it exposes the stack property on new Error() but also on custom errors through constructor functions that have Error as their prototype. Unfortunately I'm not yet fleut enough in git(hub) yet to clone the repo and setup a pullrequest, so below the new code for existing methods/new method for both the JavaScriptException and NativeError class. ====JavaScriptException.java====
=======NativeError.java=======
Does this make sense? For some of the discussion that came before this patch, see: https://groups.google.com/forum/#!topic/mozilla-rhino/5p_-9g01eqE |
Note that this fix puts Rhino inline with browsers that also allow creating custom Errors that then expose the stack property (albeit the stack property not being ECMA spec, most browsers have implemented it to my knowledge) |
Use code written up for mozilla#109 to add a "stack" property to new Error messages (not just those caused by "throw").
Why is this change not included into official rhino version? I'm still not able to get stack of error when throwing and catching the exception
where internal.log is a function in Java code that logs elements, but I'm still receiving an undefined object. |
Are you running with the debug flag turned on in the Rhino context? Errors only have stack traces when Rhino's debug flag is enabled. For instance: rhino -e "var error = new Error('Error string'); try { throw error; } catch (e) { print(e.stack); }" rhino -debug -e "var error = new Error('Error string'); try { throw error; } catch (e) { print(e.stack); }" |
Why would the stack property only be available when the -debug option is set? |
Rhino has historically not put debug information (file names and line Without that information in the generated code, there's no way to generate BTW when I say "turn debug on," I mean to set the "GeneratingDebug" flag on On Fri, Sep 2, 2016 at 7:56 AM, Paul Bakker notifications@github.com
Greg Brail | apigee https://apigee.com/ | twitter @gbrail |
ok, that makes sense, didn't think of that. So, if the stack property is exposed when the GeneratingDebug flag is switched on and its not likely that the stack property will start returning anything while that is not the case, shouldn't this issue be closed then? |
I don't think this works as described. Setting the So my goal was to throw a js Error, catch it, and access the call stack property. The way I managed to achieve that was by enabling the FEATURE_LOCATION_INFORMATION_IN_ERROR (which is off by default) on the global Context factory. There might be more subtle ways of doing that (I'm new to Rhino) but what I did was I one-time preset:
Suggestions for better way to enable it are welcome.
outputs the following to the If you are interested in the whole stack, including the java part, there is a 'magic' object injected in the catch scope, so when you are in a catch block you can reference it by the name I hope this helps to those of us in the same boat. |
I honestly don't know why "FEATURE_LOCATION_INFORMATION_IN_ERROR" isn't the
default. Maybe it'd have some small performance impact on the code. But if
there's support out there for changing the default then we can certainly do
that and avoid confusing new users.
…On Tue, Oct 24, 2017 at 8:28 AM, shturec ***@***.***> wrote:
I don't think this works as described.
Setting the generatingDebug flag on context to true had no effect for me,
and looking (briefly) at the code, I couldn't find relation between the
code injecting the stack property and the generatingDebug flag.
So my goal was to throw a js Error, catch it, and access the call stack
property.
The way I managed to achieve that was by enabling the
FEATURE_LOCATION_INFORMATION_IN_ERROR
<https://github.com/mozilla/rhino/blob/6420e464a890faba5a47ead45e6f1d5050f95260/src/org/mozilla/javascript/JavaScriptException.java#L44>
(which is off by default) on the global Context factory
<http://rhino/src/org/mozilla/javascript/ContextFactory.java>. There
might be more subtle ways of doing that (I'm new to Rhino) but what I did
was I one-time preset:
ContextFactory.initGlobal(new ContextFactory() {
@OverRide
protected boolean hasFeature(Context cx, int featureIndex) {
if (featureIndex == Context.FEATURE_LOCATION_INFORMATION_IN_ERROR) {
return true;
}
return super.hasFeature(cx, featureIndex);
}
});
Suggestions for better way to enable it are welcome.
In my case I'm using context.evaluateReader and I can do what i intended
just fine. Now catching both: throw Error('test') and new Error('test')
features a stack property on the error variable defined in the catch
block. For example:
try{
throw Error('test');
} catch(e){
java.lang.System.out.println(e.stack)
}
outputs the following to the System.out: at a/b/test.js:1
(assuming script path is a/b/test.js and the code above is its (top)
content)
What you can't do is throw a free form object (throw {"message":"test"})
and have the stack property available. I personally, can live with that.
If you are interested in the whole stack, including the java part, there
is a 'magic' object injected in the catch scope, so when you are in a catch
block you can reference it by the name __exception__. This is a reference
to the JavaScriptException
<http://rhino/src/org/mozilla/javascript/JavaScriptException.java>
wrapper for the thrown Error and you can invoke methods on it as usual.
I hope this helps to those of us in the same boat.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#109 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAf0a7anlOoQqlOMlk_sMoOlL1TlGx4Mks5svgIkgaJpZM4Acyut>
.
|
@gbrail Hi Greg, do you see a reason why your apigee patch is not merged into this repo. For HtmlUnit i still have a slightly different version of marc's patch. I think we have to integrate this in some way. |
@gbrail knock, knock |
Since it's been a long time, it looks like the thing we want to enable is:
to have a "stack" property populated. It looks like a pretty small change from the very old PR to fix that. Is that what we want to fix? It also sounds like we have other problems, including the weird behavior of only adding line numbers if the special flag is set. It seems like it might be time to get rid of that flag, but I think that's a different issue. |
@gbrail i guess this is solved by your last NativeError rework |
I think that we finally took care of this one! |
The exception thrown by something like:
has the stacktrace available as
stack
property but the exception thrown bydoesn't.
This pull request fixes this problem (including unit test)