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) :
QObject(parent)
{
frame = p_frame;
this->setObjectName("MyAPI");
AttachObject();
connect( frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(AttachObject()) );
connect(this,SIGNAL(MyError()),this,SLOT(HandleMyError()));
}
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()
{
this->frame->addToJavaScriptWindowObject(this->objectName(),this);
}
void JSObject::HandleMyError()
{
QString l_js ="alert('throwing error'); throw new Error();";
qDebug("evaluating javascript throw error");
frame->evaluateJavaScript(l_js);
qDebug("throw error call finished");
return;
}@and the html:
@<html>
<head>
<title> Error throwing test</title>
[removed]function test()
{
try
{
var result = MyAPI.TestMethod(); //this should throw an error
alert(result);
}
catch(e)
{
alert("error cought");
}
return;
}
[removed]
</head>
<body>
<button>Test</button>
</body></html>@
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 nowSo 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":http://developer.qt.nokia.com/forums/viewthread/6881/ ).
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!