Review fix: add flag to choose whether to abort script on exception

This commit is contained in:
Dale Glass 2023-03-05 21:54:14 +01:00 committed by ksuprynowicz
parent aec756b0b9
commit acd19f7c40
4 changed files with 34 additions and 6 deletions

View file

@ -305,9 +305,10 @@ ScriptManager::ScriptManager(Context context, const QString& scriptContents, con
}
// Unhandled exception kills the running script
stop(false);
logException(output);
if (_abortOnUncaughtException) {
stop(false);
logException(output);
}
});
#endif
@ -909,7 +910,7 @@ void ScriptManager::run() {
PROFILE_RANGE(script, _fileNameString);
_returnValue = _engine->evaluate(_scriptContents, _fileNameString);
if (_engine->hasUncaughtException()) {
if (_engine->hasUncaughtException() && _abortOnUncaughtException) {
qCWarning(scriptengine) << "Engine has uncaught exception, stopping";
stop();

View file

@ -1180,6 +1180,23 @@ public:
*/
std::shared_ptr<ScriptException> getUncaughtException() const;
/**
* @brief Whether this engine will abort on an uncaught exception
*
* @warning This probably should be refactored into a more comprehensive per-script flags system
* @return true
* @return false
*/
bool getAbortOnUncaughtException() const { return _abortOnUncaughtException; }
/**
* @brief Whether to abort on an uncaught exception
*
* @warning This probably should be refactored into a more comprehensive per-script flags system
* @param value
*/
void setAbortOnUncaughtException(bool value) { _abortOnUncaughtException = value; }
public slots:
/**
@ -1555,6 +1572,8 @@ protected:
ScriptManagerScriptingInterfacePointer _scriptingInterface;
bool _abortOnUncaughtException{ false };
friend ScriptManagerPointer newScriptManager(Context context, const QString& scriptContents, const QString& fileNameString);
friend class ScriptManagerScriptingInterface;

View file

@ -57,6 +57,8 @@ ScriptManagerPointer ScriptEngineTests::makeManager(const QString &scriptSource,
ScriptManagerPointer sm = newScriptManager(ScriptManager::NETWORKLESS_TEST_SCRIPT, scriptSource, scriptFilename);
sm->setAbortOnUncaughtException(true);
connect(sm.get(), &ScriptManager::scriptLoaded, [](const QString& filename){
qWarning() << "Loaded script" << filename;
});
@ -227,8 +229,7 @@ void ScriptEngineTests::testRaiseExceptionAndCatch() {
" print(\"Caught!\");"
" }"
"}"
"Script.stop(true);"
;
"Script.stop(true);";
QString printed;
auto sm = makeManager(script, "testRaiseCatch.js");
@ -248,6 +249,11 @@ void ScriptEngineTests::testRaiseExceptionAndCatch() {
}
void ScriptEngineTests::testSignal() {
}
void ScriptEngineTests::scriptTest() {
return;

View file

@ -63,6 +63,8 @@ private slots:
void testInvokeNonInvokable();
void testRaiseException();
void testRaiseExceptionAndCatch();
void testSignal();
private: