From c1bcaca07fc89b3a7e21dc6ca2958b7938eb3a40 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Thu, 21 Jan 2021 18:30:09 +0100 Subject: QJSEngine: Add a function to throw a pre-generated error object It makes little sense that you can construct an error object but not throw it. The test was definitely meant to actually throw the error object, not return it. [ChangeLog][QtQml] QJSEngine has gained an additional overload to the throwError() method, with the effect that calling throwError() with a character literal as argument is now ambiguous. You should explicitly construct a QString instead. Change-Id: I90c6c9edf10509daa142a86581d6a3f7ff45af2c Reviewed-by: Maximilian Goldstein Reviewed-by: Fabian Kosmale --- src/qml/jsapi/qjsengine.cpp | 14 ++++++++++++++ src/qml/jsapi/qjsengine.h | 1 + 2 files changed, 15 insertions(+) (limited to 'src/qml/jsapi') diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index f0e3e69fbb..37b5306897 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -938,6 +938,20 @@ void QJSEngine::throwError(QJSValue::ErrorType errorType, const QString &message m_v4Engine->throwError(e); } +/*! + \overload throwError() + + Throws a pre-constructed run-time \a error (exception). This way you can + use \l newErrorObject() to create the error and customize it as necessary. + + \since 6.1 + \sa {Script Exceptions}, newErrorObject() +*/ +void QJSEngine::throwError(const QJSValue &error) +{ + m_v4Engine->throwError(QJSValuePrivate::asReturnedValue(&error)); +} + /*! * Returns \c true if the last JavaScript execution resulted in an exception or * if throwError() was called. Otherwise returns \c false. Mind that evaluate() diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h index 9b02469b23..783f602c12 100644 --- a/src/qml/jsapi/qjsengine.h +++ b/src/qml/jsapi/qjsengine.h @@ -134,6 +134,7 @@ public: void throwError(const QString &message); void throwError(QJSValue::ErrorType errorType, const QString &message = QString()); + void throwError(const QJSValue &error); bool hasError() const; QJSValue catchError(); -- cgit v1.2.3