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

Js error message became [object Object] #434

Open
sixinli opened this issue Apr 3, 2021 · 5 comments
Open

Js error message became [object Object] #434

sixinli opened this issue Apr 3, 2021 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@sixinli
Copy link

sixinli commented Apr 3, 2021

js script exception not being extracted properly after 19.x.x

we noticed that exceptions changes from having actual message (in this case we have some errors from handlebars.js), to having [object Object] when upgrading from 19.3.1 to 21.0.0, looking at https://github.com/oracle/graaljs/blob/master/graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/UserScriptException.java it seems like getMessage is changed between the versions...

specifically https://github.com/oracle/graaljs/blob/master/graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/UserScriptException.java#L125 returns null, and that's from com.oracle.truffle.object.DynamicObjectLibraryImpl

@TruffleBoundary
@Override
public Object getOrDefault(DynamicObject object, Shape cachedShape, Object key, Object defaultValue) {
    Property existing = ACCESS.getShape(object).getProperty(key);
    if (existing != null) {
        return getLocation(existing).get(object, false);
    } else {
        return defaultValue;
    }
}

thinks DynamicObject<JSError> has no constructor....

@sixinli sixinli changed the title Poly Js error message became [object Object] Apr 3, 2021
@woess woess added the bug Something isn't working label Apr 6, 2021
@woess
Copy link
Member

woess commented Apr 6, 2021

Do you have some example code that you can share that triggers this?

@sixinli
Copy link
Author

sixinli commented Jun 11, 2021

sorry for the late reply, we have the handlebars.js 3.x running, and it's just an error thrown in javascript like https://github.com/handlebars-lang/handlebars.js/blob/v3.0.8/lib/handlebars/runtime.js#L198, the message is lot in between

@rdosanjh
Copy link

Hey it seems that Handlebars JS has created their own custom exception which seems to be the problem.

Here is a minimal example that outlines the problem

import com.google.common.io.Resources;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Source;


public class Program {
    public static void main(String[] args) {
        Engine engine = Engine.create();
        Context context = Context.newBuilder()
                .engine(engine)
                .option("js.nashorn-compat", "true")
                .allowAllAccess(true)
                .build();


        String customErrorFunc = "function customError(errorMessage) { this.message = Error.prototype.constructor.call(this, errorMessage).message; }\n" +
                "customError.prototype = new Error();";
        try {
            context.eval(Source.create("js", customErrorFunc));
            context.eval(Source.create("js", "throw new customError('This is a basic error')"));

        } catch (Exception e) {
            System.out.println(e.getMessage());
            // 19.3.1 returns {message: "This is a basic error"}
            // 21.1.0 returns [object Object]
        }
    }
}

@rdosanjh
Copy link

I've done some digging and this was introduced in this commit. We were using the "js.nashorn-compat" mode but when they became combined it broke our functionality.

Here is the line in question before - after. (JSUserObject has since been renamed to JSOrdinary).

Would it be possible to add an option to go back to the previous way of displaying errors?

@woess
Copy link
Member

woess commented Sep 1, 2021

A simple workaround for this would be to set the constructor property on the prototype of the custom error:
customError.prototype.constructor = customError;
With this simple change I see:
customError: This is a basic error

Alternatively, you could use just subclass Error, e.g. class CustomError extends Error {}

It would probably make sense to show the message also without the constructor property. We'll look into that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants