aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-12-03 14:37:49 +0100
committerUlf Hermann <ulf.hermann@qt.io>2020-12-07 15:53:32 +0100
commite8596de70e46e98a851ac8204e294e5ef4994b96 (patch)
tree06a2555357d5b6ceea929d3a43383eed09b07f5d
parentc8d98dc206714abd5c0dc76008ffa729973d3a8f (diff)
Allow checking for and catching errors from QJSEngine
As you can manually throw an error, you should be able to catch it again, too. Change-Id: I82475df1969a1fd76f4cf5fc0a8d921dfafaef89 Reviewed-by: Maximilian Goldstein <max.goldstein@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qml/jsapi/qjsengine.cpp27
-rw-r--r--src/qml/jsapi/qjsengine.h2
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp14
3 files changed, 43 insertions, 0 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp
index 96fcd99de4..a0e2cb2c3a 100644
--- a/src/qml/jsapi/qjsengine.cpp
+++ b/src/qml/jsapi/qjsengine.cpp
@@ -927,6 +927,33 @@ void QJSEngine::throwError(QJSValue::ErrorType errorType, const QString &message
}
/*!
+ * Returns \c true if the last JavaScript execution resulted in an exception or
+ * if throwError() was called. Otherwise returns \c false. Mind that evaluate()
+ * catches any exceptions thrown in the evaluated code.
+ *
+ * \since Qt 6.1
+ */
+bool QJSEngine::hasError() const
+{
+ return m_v4Engine->hasException;
+}
+
+/*!
+ * If an exception is currently pending, catches it and returns it as a
+ * QJSValue. Otherwise returns undefined as QJSValue. After calling this method
+ * hasError() returns \c false.
+ *
+ * \since Qt 6.1
+ */
+QJSValue QJSEngine::catchError()
+{
+ if (m_v4Engine->hasException)
+ return QJSValuePrivate::fromReturnedValue(m_v4Engine->catchException());
+ else
+ return QJSValue();
+}
+
+/*!
\property QJSEngine::uiLanguage
\brief the language to be used for translating user interface strings
\since 5.15
diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h
index 775f9e658a..c480a77599 100644
--- a/src/qml/jsapi/qjsengine.h
+++ b/src/qml/jsapi/qjsengine.h
@@ -121,6 +121,8 @@ public:
void throwError(const QString &message);
void throwError(QJSValue::ErrorType errorType, const QString &message = QString());
+ bool hasError() const;
+ QJSValue catchError();
QString uiLanguage() const;
void setUiLanguage(const QString &language);
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index 4f2e6f1866..a5316dce21 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -243,6 +243,7 @@ private slots:
void throwError();
void throwErrorObject();
void returnError();
+ void catchError();
void mathMinMax();
void importModule();
@@ -4810,6 +4811,19 @@ void tst_QJSEngine::returnError()
QVERIFY(!result.property("stack").isUndefined());
}
+void tst_QJSEngine::catchError()
+{
+ QJSEngine engine;
+ QVERIFY(!engine.hasError());
+ engine.throwError(QJSValue::GenericError, "some error");
+ QVERIFY(engine.hasError());
+ const QJSValue error = engine.catchError();
+ QVERIFY(error.isError());
+ QCOMPARE(error.errorType(), QJSValue::GenericError);
+ QCOMPARE(error.property("message").toString(), "some error");
+ QVERIFY(!engine.hasError());
+}
+
QJSValue tst_QJSEngine::throwingCppMethod1()
{
qjsEngine(this)->throwError("blub");