fix: correctly handle & display post-processor errors

This commit is contained in:
LordMZTE 2024-07-10 00:03:50 +02:00
parent 488a64809d
commit dbc84f2c99
Signed by untrusted user: LordMZTE
GPG key ID: B64802DC33A64FF6
2 changed files with 25 additions and 3 deletions

View file

@ -66,6 +66,20 @@ pub fn main() u8 {
, .{usage});
},
error.Explained => {},
error.LuaError => {
// Once Zig is smart enough to remove LuaError from the error set here, we'll
// replace this branch with this compile-time check:
//comptime {
// const ret_errors = @typeInfo(@typeInfo(@typeInfo(@TypeOf(run)).Fn.return_type.?).ErrorUnion.error_set).ErrorSet.?;
// for (ret_errors) |err| {
// if (std.mem.eql(u8, err.name, "LuaError"))
// @compileError("Run function must never return a LuaError!");
// }
//}
// We can't get the error message here as the Lua state will alread have been destroyed.
std.log.err("UNKNOWN LUA ERROR! THIS IS A BUG!", .{});
},
else => {
std.log.err("UNEXPECTED: {s}", .{@errorName(e)});
if (@errorReturnTrace()) |ert| std.debug.dumpStackTrace(ert.*);

View file

@ -189,12 +189,17 @@ pub fn generate(l: *c.lua_State, code: TemplateCode) !GeneratedFile {
if (c.lua_pcall(l, 0, 0, 0) != 0) {
std.log.err("failed to run template: {?s}", .{ffi.luaToString(l, -1)});
return error.RunTemplate;
}
return .{
.content = try tmpl.getOutput(l),
.content = tmpl.getOutput(l) catch |e| switch (e) {
error.LuaError => {
std.log.err("failed to run post-processor: {?s}", .{ffi.luaToString(l, -1)});
return error.RunPostProcessor;
},
else => return e,
},
.mode = tmpl.mode,
.assume_deterministic = tmpl.assume_deterministic,
};
@ -626,7 +631,6 @@ pub const LTemplate = struct {
/// caller owns return value.
fn getOutput(self: *LTemplate, l: *c.lua_State) ![]const u8 {
const top = c.lua_gettop(l);
defer c.lua_settop(l, top);
c.lua_pushlightuserdata(l, self);
c.lua_gettable(l, c.LUA_REGISTRYINDEX);
@ -635,6 +639,7 @@ pub const LTemplate = struct {
// check if there's no post processor
if (c.lua_isnil(l, -1)) {
c.lua_settop(l, top);
return try self.output.allocator.dupe(u8, self.output.items);
}
@ -643,11 +648,14 @@ pub const LTemplate = struct {
// call post processor
if (c.lua_pcall(l, 1, 1, 0) != 0) {
try ffi.luaFmtString(l, "running post processor: {?s}", .{ffi.luaToString(l, -1)});
c.lua_insert(l, top + 1);
c.lua_settop(l, top + 1);
return error.LuaError;
}
const out = ffi.luaConvertString(l, -1);
c.lua_settop(l, top);
return try self.output.allocator.dupe(u8, out);
}