From 5bbb880700e01f412f72851680b488080fb86a9e Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 16 Feb 2020 11:44:50 +0100 Subject: [PATCH] Improve script exception error message. --- .../AngelScript/AngelScripResolver.cpp | 1 + .../AngelScript/AngelScriptScript.hpp | 10 ++++++++-- tests/ScriptTests/BaseScriptClassTests.cpp | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ScriptResolving/AngelScript/AngelScripResolver.cpp b/src/ScriptResolving/AngelScript/AngelScripResolver.cpp index f73aa9b..bd70e17 100644 --- a/src/ScriptResolving/AngelScript/AngelScripResolver.cpp +++ b/src/ScriptResolving/AngelScript/AngelScripResolver.cpp @@ -113,4 +113,5 @@ void AngelScripResolver::FinalizeModule() { void AngelScripResolver::CreateScript(ScriptCategory category, const char* scriptName) { auto scriptString = _loadFunc(category, scriptName); _mainModule->AddScriptSection(scriptName, scriptString); + } diff --git a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp index 9657252..4d8a9bf 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp @@ -29,7 +29,7 @@ public: ContextPool* GetContextPool() { return _ctxPool; } -#define CALL_HOOK(name, setup) \ +#define CALL_HOOK(name, setup) \ auto s = _type->Get##name(); \ if (!s.Exists) \ return; \ @@ -45,7 +45,13 @@ public: ctx->SetObject(_obj); \ setup; \ auto scriptResult = ctx->Execute(); \ - if (scriptResult != 0) { \ + if (scriptResult != asEXECUTION_FINISHED) { \ + if (scriptResult == asEXECUTION_EXCEPTION) { \ + std::stringstream err; \ + err << "Script exception in script '" << GetName() << "', line " << ctx->GetExceptionLineNumber() \ + << ". Message: '" << ctx->GetExceptionString() << "'."; \ + throw CreatureException(err.str()); \ + } \ throw CreatureException("Script didn't finish properly; message " + std::to_string(scriptResult)); \ } \ if (newContext) { \ diff --git a/tests/ScriptTests/BaseScriptClassTests.cpp b/tests/ScriptTests/BaseScriptClassTests.cpp index 8c33073..7a79240 100644 --- a/tests/ScriptTests/BaseScriptClassTests.cpp +++ b/tests/ScriptTests/BaseScriptClassTests.cpp @@ -56,6 +56,9 @@ void StopBeforeAttack(ExecutingMove@ attack, bool& result) override{ AS_CLASS(OnAfterHitsScript, "int value = 0; void OnAfterHits(ExecutingMove@ attack, Pokemon@ target) override { value++; } " "int GetValue() { return value; }"), + AS_CLASS(throwScript, + R"(void PreventAttack(ExecutingMove@ attack, bool& result) override{ throw("test exception"); })"), + }; @@ -271,4 +274,21 @@ TEST_CASE("Invoke OnAfterHits script function") { delete script; } +TEST_CASE("Handle script exceptions.") { + auto mainLib = TestLibrary::GetLibrary(); + auto script = GetScript(mainLib, "throwScript"); + bool b = false; + bool hasThrown = false; + try{ + script->PreventAttack(nullptr, &b); + } + catch (const CreatureException& e){ + hasThrown = true; + REQUIRE(strcmp("Script exception in script 'throwScript', line 1. Message: 'test exception'.", e.what()) == 0); + } + REQUIRE(hasThrown); + + delete script; +} + #endif \ No newline at end of file