Please nominate your Qt Champions for 2021!

Problem with addToJavaScriptWindowObject and JS throwing errors

  • Hello,
    I'm trying to add errors thrown from my cpp objects exposed to JS.
    Here's an example

    @JSObject::JSObject(QWebFrame *p_frame, QObject *parent) :
    frame = p_frame;
    connect( frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(AttachObject()) );
    QString JSObject::TestMethod()
    //logic here, let's assume something went wrong
    qDebug("test method called, emmiting signal");
    emit MyError();
    qDebug("emit finished, method returning now");
    return "method finish";

    void JSObject::AttachObject()
    void JSObject::HandleMyError()
    QString l_js ="alert('throwing error'); throw new Error();";
    qDebug("evaluating javascript throw error");
    qDebug("throw error call finished");

    and the html:
    <title> Error throwing test</title>

    function test()
    var result = MyAPI.TestMethod(); //this should throw an error
    alert("error cought");

    The alert set here:
    @QString l_js ="alert('throwing error'); throw new Error();"; @
    happens when expected, i get this output:
    test method called, emmiting signal
    evaluating javascript throw error
    //alert happens here, untill i press ok there is no more output, execution stops and waits for alert window to close
    throw error call finished
    emit finished, method returning now

    So I know throw error is executed at the right time, but something is wrong, JS try/catch does not catch it.
    Anybody knows of a good way to add JS errors to exposed cpp objects?

  • I think you basically have the same problem as I do ("see here": ).

    The eval() trick does not work (I guess) because errors thrown inside the eval() are not passed to the calling JavaScript context.

    If perhaps you did find a solution in the meantime, then please let me know, thanks!

Log in to reply