Quellcode durchsuchen

Merge pull request #1296 from DL6ER/fix/lua_error

Fix LUA error reporting
bel2125 vor 8 Monaten
Ursprung
Commit
d5668ee415
1 geänderte Dateien mit 49 neuen und 8 gelöschten Zeilen
  1. 49 8
      src/mod_lua.inl

+ 49 - 8
src/mod_lua.inl

@@ -10,6 +10,9 @@
 #include "civetweb_lua.h"
 #include "civetweb_private_lua.h"
 
+/* Prototypes */
+static int
+lua_error_handler(lua_State *L);
 
 #if defined(_WIN32)
 static void *
@@ -669,11 +672,19 @@ run_lsp_kepler(struct mg_connection *conn,
 		/* Syntax error or OOM.
 		 * Error message is pushed on stack. */
 		lua_pcall(L, 1, 0, 0);
-		lua_cry(conn, lua_ok, L, "LSP", "execute"); /* XXX TODO: everywhere ! */
+		lua_cry(conn, lua_ok, L, "LSP Kepler", "execute");
+		lua_error_handler(L);
+		return 1;
 
 	} else {
 		/* Success loading chunk. Call it. */
-		lua_pcall(L, 0, 0, 1);
+		lua_ok = lua_pcall(L, 0, 0, 0);
+		if(lua_ok != LUA_OK)
+		{
+			lua_cry(conn, lua_ok, L, "LSP Kepler", "call");
+			lua_error_handler(L);
+			return 1;
+		}
 	}
 	return 0;
 }
@@ -787,9 +798,18 @@ run_lsp_civetweb(struct mg_connection *conn,
 						/* Syntax error or OOM.
 						 * Error message is pushed on stack. */
 						lua_pcall(L, 1, 0, 0);
+						lua_cry(conn, lua_ok, L, "LSP", "execute");
+						lua_error_handler(L);
+						return 1;
 					} else {
 						/* Success loading chunk. Call it. */
-						lua_pcall(L, 0, 0, 1);
+						lua_ok = lua_pcall(L, 0, 0, 0);
+						if(lua_ok != LUA_OK)
+						{
+							lua_cry(conn, lua_ok, L, "LSP", "call");
+							lua_error_handler(L);
+							return 1;
+						}
 					}
 
 					/* Progress until after the Lua closing tag. */
@@ -2776,18 +2796,39 @@ lua_error_handler(lua_State *L)
 
 	lua_getglobal(L, "mg");
 	if (!lua_isnil(L, -1)) {
-		lua_getfield(L, -1, "write"); /* call mg.write() */
+		/* Write the error message to the error log */
+		lua_getfield(L, -1, "write");
 		lua_pushstring(L, error_msg);
 		lua_pushliteral(L, "\n");
-		lua_call(L, 2, 0);
-		IGNORE_UNUSED_RESULT(
-		    luaL_dostring(L, "mg.write(debug.traceback(), '\\n')"));
+		lua_call(L, 2, 0); /* call mg.write(error_msg + \n) */
+		lua_pop(L, 1); /* pop mg */
+
+		/* Get Lua traceback */
+		lua_getglobal(L, "debug");
+		lua_getfield(L, -1, "traceback");
+		lua_call(L, 0, 1); /* call debug.traceback() */
+		lua_remove(L, -2); /* remove debug */
+
+		/* Write the Lua traceback to the error log */
+		lua_getglobal(L, "mg");
+		lua_getfield(L, -1, "write");
+		lua_pushvalue(L, -3); /* push the traceback */
+
+		/* Only print the traceback if it is not empty */
+		if (strcmp(lua_tostring(L, -1), "stack traceback:") != 0) {
+			lua_pushliteral(L, "\n"); /* append a newline */
+			lua_call(L, 2, 0); /* call mg.write(traceback + \n) */
+			lua_pop(L, 2); /* pop mg and traceback */
+		} else {
+			lua_pop(L, 3); /* pop mg, traceback and error message */
+		}
+
 	} else {
 		printf("Lua error: [%s]\n", error_msg);
 		IGNORE_UNUSED_RESULT(
 		    luaL_dostring(L, "print(debug.traceback(), '\\n')"));
 	}
-	/* TODO(lsm, low): leave the stack balanced */
+	lua_pop(L, 1); /* pop error message */
 
 	return 0;
 }