Skip to content

Commit

Permalink
Check JS callback errors. Fixes #9
Browse files Browse the repository at this point in the history
Thanks to Osmo Salomaa for the original report and test case.
  • Loading branch information
thp committed Jan 20, 2014
1 parent bbe94ec commit 53273da
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/qpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tests/test_errors/README
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions tests/test_errors/test_errors.py
Original file line number Diff line number Diff line change
@@ -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()
40 changes: 40 additions & 0 deletions tests/test_errors/test_errors.qml
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 53273da

Please sign in to comment.