From 6e1e4a619a600589074429838790122d3e6645e2 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 18 Aug 2021 13:32:01 +0200
Subject: [PATCH] SCRIPT: The get module/function/job methods in LuaContext now
 aborts the lua interpreter on errors

---
 src/Lib/Script/LuaContext.cc | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/Lib/Script/LuaContext.cc b/src/Lib/Script/LuaContext.cc
index 81887832..a7c26e65 100644
--- a/src/Lib/Script/LuaContext.cc
+++ b/src/Lib/Script/LuaContext.cc
@@ -201,8 +201,11 @@ LuaContext::getModule(const std::string_view name) const noexcept
         const int r = modules.at(name);
         lua_rawgeti(L, LUA_REGISTRYINDEX, r);
         return ModuleDeclaration::CHECK(L, -1);
-    } else {
-        lua_pushnil(L);
+    }
+
+    else {
+        const std::string moduleName(name);
+        luaL_error(L, "Module %s not found", moduleName.c_str());
         return nullptr;
     }
 }
@@ -214,8 +217,12 @@ LuaContext::getFunction(const std::string_view module, const std::string_view na
         const int r = functions.at({ module, name });
         lua_rawgeti(L, LUA_REGISTRYINDEX, r);
         return FunctionDeclaration::CHECK(L, -1);
-    } else {
-        lua_pushnil(L);
+    }
+
+    else {
+        const std::string moduleName(module);
+        const std::string functionName(name);
+        luaL_error(L, "Function %s::%s not found", moduleName.c_str(), functionName.c_str());
         return nullptr;
     }
 }
@@ -223,13 +230,17 @@ LuaContext::getFunction(const std::string_view module, const std::string_view na
 JobDeclaration *
 LuaContext::getJob(const std::string_view module, const std::string_view name) const noexcept
 {
-    if (moduleExists(module)) {
+    if (moduleExists(module) && jobs.count({ module, name }) >= 1) {
         const int r = jobs.at({ module, name });
         lua_rawgeti(L, LUA_REGISTRYINDEX, r);
         return JobDeclaration::CHECK(L, -1);
-    } else {
-        lua_pushnil(L);
-        return nullptr;
+    }
+
+    else {
+        const std::string moduleName(module);
+        const std::string jobName(name);
+        luaL_error(L, "Job %s::%s not found", moduleName.c_str(), jobName.c_str());
+        return nullptr; // Unreachable
     }
 }
 
-- 
GitLab