[PATCH 08/12] filter: lua error reporting

Jason A. Donenfeld Jason at zx2c4.com
Mon Jan 13 05:11:15 CET 2014


Signed-off-by: Jason A. Donenfeld <Jason at zx2c4.com>
---
 filter.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/filter.c b/filter.c
index 6f0e298..d2f6a4f 100644
--- a/filter.c
+++ b/filter.c
@@ -182,6 +182,12 @@ struct lua_filter {
 	lua_State *lua_state;
 };
 
+static void error_lua_filter(struct lua_filter *filter)
+{
+	die("Lua error in %s: %s", filter->script_file, lua_tostring(filter->lua_state, -1));
+	lua_pop(filter->lua_state, 1);
+}
+
 static ssize_t write_lua_filter(struct cgit_filter *base, const void *buf, size_t count)
 {
 	struct lua_filter *filter = (struct lua_filter *) base;
@@ -189,6 +195,7 @@ static ssize_t write_lua_filter(struct cgit_filter *base, const void *buf, size_
 	lua_getglobal(filter->lua_state, "filter_write");
 	lua_pushlstring(filter->lua_state, buf, count);
 	if (lua_pcall(filter->lua_state, 1, 0, 0)) {
+		error_lua_filter(filter);
 		errno = EPROTO;
 		return -1;
 	}
@@ -237,6 +244,7 @@ static int init_lua_filter(struct lua_filter *filter)
 	lua_setglobal(filter->lua_state, "html");
 
 	if (luaL_dofile(filter->lua_state, filter->script_file)) {
+		error_lua_filter(filter);
 		lua_close(filter->lua_state);
 		filter->lua_state = NULL;
 		return 1;
@@ -257,8 +265,10 @@ static int open_lua_filter(struct cgit_filter *base, va_list ap)
 	lua_getglobal(filter->lua_state, "filter_open");
 	for (i = 0; i < filter->base.argument_count; ++i)
 		lua_pushstring(filter->lua_state, va_arg(ap, char *));
-	if (lua_pcall(filter->lua_state, filter->base.argument_count, 0, 0))
+	if (lua_pcall(filter->lua_state, filter->base.argument_count, 0, 0)) {
+		error_lua_filter(filter);
 		return 1;
+	}
 	return 0;
 }
 
@@ -268,8 +278,10 @@ static int close_lua_filter(struct cgit_filter *base)
 	int ret = 0;
 
 	lua_getglobal(filter->lua_state, "filter_close");
-	if (lua_pcall(filter->lua_state, 0, 0, 0))
+	if (lua_pcall(filter->lua_state, 0, 0, 0)) {
+		error_lua_filter(filter);
 		ret = 1;
+	}
 	unhook_write();
 	return ret;
 }
-- 
1.8.5.2



More information about the CGit mailing list