Skip to content
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

Accessing JS error object from MiniRacer::EvalError? #129

Open
tisba opened this issue Feb 23, 2019 · 5 comments
Open

Accessing JS error object from MiniRacer::EvalError? #129

tisba opened this issue Feb 23, 2019 · 5 comments

Comments

@tisba
Copy link
Collaborator

tisba commented Feb 23, 2019

I'm wondering how I can access the JavaScript error object from MiniRacer::EvalError.

I want to run script snippets from users and I want to report errors to them. We have some extended attributes on custom errors which I would need to access.

The only way I found to solve this is by wrapping the script before sending it to MiniRacer::Context#eval like this:

wrapper = <<~JS
  try {
#{script}
  }
  catch(err) {
    var thrownError = err;
  }
JS

context.eval(wrapper)

With this I can access thrownError. But my issue is, that line numbers in backtraces are off. My current solution is to have a map that contains offsets accounting for the wrapper script.

Is there any other way to achieve this?

@SamSaffron
Copy link
Collaborator

I think we need to fix our internals to account for the wrapper, can you give it a shot in a PR?

@tisba
Copy link
Collaborator Author

tisba commented Feb 25, 2019

I'm not sure I can follow.

I took a quick peek at https://github.com/discourse/mini_racer/blob/master/lib/mini_racer.rb#L170-L184 but could not spot anything that looks like a wrapper or exception handling.

In case you are referring to the the C code in https://github.com/discourse/mini_racer/blob/master/ext/mini_racer_extension/mini_racer_extension.cc, I have to pass (I don't understand it).

Just in case we have a misunderstanding: I'd like to access the thrown javascript error object:

context.eval('throw new MyError("a message", { some: "meta data" });');

Is there a way to access the thrown error from miniracer?

@tisba
Copy link
Collaborator Author

tisba commented Apr 9, 2019

Can you point me in the right direction, @SamSaffron?

@SamSaffron
Copy link
Collaborator

I think you are talking about these lines:

https://github.com/discourse/mini_racer/blob/723958f4da3cc6de01530b390cd4803dfbf7de7f/ext/mini_racer_extension/mini_racer_extension.cc#L736-L749

At the moment we send a very rudimentary exception back so what we would want to do is look at the return result there and maybe convert the object to JSON and attach to Runtime error. It is somewhat tricky but we could provide more info for sure.

@tisba
Copy link
Collaborator Author

tisba commented Apr 14, 2019

Okay, this seems nothing I can directly contribute code changes too, sorry :( It would be great, if feasible, to have a method on the MiniRacer::EvalError exception to access the error object from JS land.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants