1
0
mirror of https://github.com/thp/pyotherside.git synced 2025-01-17 23:22:53 +08:00

Check JS callback errors. Fixes #9

Thanks to Osmo Salomaa for the original report and test case.
This commit is contained in:
Thomas Perl 2014-01-20 19:09:57 +01:00
parent bbe94ecfd9
commit 53273dad50
4 changed files with 68 additions and 1 deletions

View File

@ -120,7 +120,17 @@ QPython::receive(QVariant variant)
for (int i=1; i<list.size(); i++) {
args << callback.engine()->toScriptValue(list[i]);
}
callback.call(args);
QJSValue result = callback.call(args);
if (result.isError()) {
// Ideally we would throw the error back to Python (so that the
// pyotherside.send() method fails, as this is where the call
// originated). We can't do this, because the pyotherside.send()
// call is asynchronous (it returns before we call into JS), so do
// the next best thing and report the error to our error handler in
// QML instead.
emit error(QString("pyotherside.send() failed handler: %1")
.arg(result.toString()));
}
} else {
// Default action
emit received(variant);

6
tests/test_errors/README Normal file
View File

@ -0,0 +1,6 @@
Test if JS errors from pyotherside.send() are propagated to the PyOtherSide
error handler. Thanks to Osmo Salomaa for the original report and test case.
----
https://github.com/thp/pyotherside/issues/9
https://gist.github.com/otsaloma/8258322

View File

@ -0,0 +1,11 @@
import pyotherside
import threading
import time
def run():
while True:
pyotherside.send("test-errors")
time.sleep(3)
thread = threading.Thread(target=run)
thread.start()

View File

@ -0,0 +1,40 @@
import QtQuick 2.0
import io.thp.pyotherside 1.0
Rectangle {
id: page
width: 300
height: 300
Component.onCompleted: {
py.addImportPath(Qt.resolvedUrl('.').substr('file://'.length));
py.setHandler("test-errors", page.testErrors);
py.importModule("test_errors", null);
}
Python {
id: py
onError: {
console.log("PYTHON ERROR: " + traceback);
msg.text += '\n' + traceback;
}
}
Text {
id: msg
anchors {
top: parent.top
left: parent.left
right: parent.right
}
text: "Testing (you should see errors appearing here)..."
wrapMode: Text.Wrap
}
function testErrors() {
console.log("starting");
page.nonexistentMethod();
console.log("finished");
}
}