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 // Unhandled exception kills the running script
stop(false); if (_abortOnUncaughtException) {
stop(false);
logException(output); logException(output);
}
}); });
#endif #endif
@ -909,7 +910,7 @@ void ScriptManager::run() {
PROFILE_RANGE(script, _fileNameString); PROFILE_RANGE(script, _fileNameString);
_returnValue = _engine->evaluate(_scriptContents, _fileNameString); _returnValue = _engine->evaluate(_scriptContents, _fileNameString);
if (_engine->hasUncaughtException()) { if (_engine->hasUncaughtException() && _abortOnUncaughtException) {
qCWarning(scriptengine) << "Engine has uncaught exception, stopping"; qCWarning(scriptengine) << "Engine has uncaught exception, stopping";
stop(); stop();

View file

@ -1180,6 +1180,23 @@ public:
*/ */
std::shared_ptr<ScriptException> getUncaughtException() const; 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: public slots:
/** /**
@ -1555,6 +1572,8 @@ protected:
ScriptManagerScriptingInterfacePointer _scriptingInterface; ScriptManagerScriptingInterfacePointer _scriptingInterface;
bool _abortOnUncaughtException{ false };
friend ScriptManagerPointer newScriptManager(Context context, const QString& scriptContents, const QString& fileNameString); friend ScriptManagerPointer newScriptManager(Context context, const QString& scriptContents, const QString& fileNameString);
friend class ScriptManagerScriptingInterface; friend class ScriptManagerScriptingInterface;

View file

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

View file

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