From 444f79d86f5483a35f83d3a5efc9cea859f88bf8 Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 11 Feb 2014 00:10:25 +0100 Subject: lua: change error behavior Return the error Lua-style, instead of raising it as Lua error. This is better, because raising errors is reserved for more "fatal" conditions. Pretending they're exceptions and trying to do exception-style error handling will just lead to pain in this language. --- player/lua.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/player/lua.c b/player/lua.c index 0a3ef775ad..e7ff414298 100644 --- a/player/lua.c +++ b/player/lua.c @@ -94,14 +94,17 @@ static void report_error(lua_State *L) } // Check client API error code: -// if err >= 0, return 0. -// if err < 0, raise the error as Lua error. +// if err >= 0, push "true" to the stack, and return 1 +// if err < 0, push nil and then the error string to the stack, and return 2 static int check_error(lua_State *L, int err) { - if (err >= 0) - return 0; - luaL_error(L, "mpv API error: %s", mpv_error_string(err)); - abort(); + if (err >= 0) { + lua_pushboolean(L, 1); + return 1; + } + lua_pushnil(L); + lua_pushstring(L, mpv_error_string(err)); + return 2; } static int run_event_loop(lua_State *L) @@ -491,16 +494,11 @@ static int script_get_property(lua_State *L) talloc_free(result); return 1; } - // out of convenience, property access errors are not hard errors - if (err != MPV_ERROR_PROPERTY_NOT_FOUND && - err != MPV_ERROR_PROPERTY_ERROR && - err != MPV_ERROR_PROPERTY_UNAVAILABLE) - check_error(L, err); if (type == MPV_FORMAT_OSD_STRING) { lua_pushstring(L, ""); - return 1; + lua_pushstring(L, mpv_error_string(err)); } - return 0; + return check_error(L, err); } static int script_set_osd_ass(lua_State *L) -- cgit v1.2.3