lua-bindings: try not to lose file inclusion Lua error messages
authorKrisztian Litkey <krisztian.litkey@intel.com>
Thu, 20 Feb 2014 17:15:00 +0000 (19:15 +0200)
committerKrisztian Litkey <krisztian.litkey@intel.com>
Fri, 21 Feb 2014 12:05:04 +0000 (14:05 +0200)
Propagate file inclusion Lua error messages back to the caller.
If we're handling a try-include (errors are ignored), log any
Lua runtime  errors as a warning (since the user probably wanted
include-if-exists semantics).

src/core/lua-bindings/lua-lua.c

index f812ad7..67720d0 100644 (file)
@@ -28,6 +28,7 @@
  */
 
 #include <unistd.h>
+#include <errno.h>
 
 #include <lualib.h>
 #include <lauxlib.h>
@@ -50,10 +51,22 @@ static int include_lua(lua_State *L, const char *file, int try, int once)
     dirs[0] = mrp_lua_get_murphy_lua_config_dir();
     dirs[1] = NULL;
 
-    if (mrp_lua_include_file(L, file, &dirs[0], files) == 0 || try)
+    if (mrp_lua_include_file(L, file, &dirs[0], files) == 0)
         return 0;
-    else
-        return -1;
+
+    if (try) {
+        if (errno == EINVAL) {
+            if (lua_type(L, -1) == LUA_TSTRING) {
+                mrp_log_warning("inclusion of '%s' failed with error '%s'",
+                                file, lua_tostring(L, -1));
+            }
+        }
+
+        return 0;
+    }
+
+    return -1;
+
 }
 
 
@@ -84,15 +97,18 @@ static int include_lua_file(lua_State *L, int try, int once)
     file = lua_tostring(L, -1);
 
     status = include_lua(L, file, try, once);
-    lua_settop(L, 0);
 
-    if (status == 0 || try)
+    if (status == 0 || try) {
+        lua_settop(L, 0);
         return 0;
+    }
     else {
         mrp_log_error("failed to include%s Lua file '%s'.",
                       once ? "_once" : "", file);
 
-        return luaL_error(L, "failed to include file '%s'.", file);
+        return luaL_error(L, "failed to include file '%s' (%s)", file,
+                          lua_type(L, -1) == LUA_TSTRING ?
+                          lua_tostring(L, -1) : "<unknown error>");
     }
 }