From 2e58a9c6eb7e8f819ed1022e4fe3761a7445070a Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 15 Jan 2016 18:38:23 +0100 Subject: [PATCH] awesome.load_image(): Return errors instead of "throwing" them Signed-off-by: Uli Schlachter --- draw.c | 15 +++++---------- draw.h | 3 ++- luaa.c | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/draw.c b/draw.c index d9795bf8..1d720783 100644 --- a/draw.c +++ b/draw.c @@ -229,23 +229,18 @@ draw_dup_image_surface(cairo_surface_t *surface) /** Load the specified path into a cairo surface * \param L Lua state * \param path file to load + * \param error A place to store an error message, if needed * \return A cairo image surface or NULL on error. */ cairo_surface_t * -draw_load_image(lua_State *L, const char *path) +draw_load_image(lua_State *L, const char *path, GError **error) { - GError *error = NULL; cairo_surface_t *ret; - GdkPixbuf *buf = gdk_pixbuf_new_from_file(path, &error); + GdkPixbuf *buf = gdk_pixbuf_new_from_file(path, error); - if (!buf) { - luaL_where(L, 1); - lua_pushstring(L, error->message); - lua_concat(L, 2); - g_error_free(error); - lua_error(L); + if (!buf) + /* error was set above */ return NULL; - } ret = draw_surface_from_pixbuf(buf); g_object_unref(buf); diff --git a/draw.h b/draw.h index 7ad79414..224a9c5e 100644 --- a/draw.h +++ b/draw.h @@ -25,6 +25,7 @@ #include #include #include +#include /* for GError */ #include "common/util.h" @@ -70,7 +71,7 @@ a_iso2utf8(const char *str, ssize_t len, char **dest, ssize_t *dlen) cairo_surface_t *draw_surface_from_data(int width, int height, uint32_t *data); cairo_surface_t *draw_dup_image_surface(cairo_surface_t *surface); -cairo_surface_t *draw_load_image(lua_State *L, const char *path); +cairo_surface_t *draw_load_image(lua_State *L, const char *path, GError **error); xcb_visualtype_t *draw_find_visual(const xcb_screen_t *s, xcb_visualid_t visual); xcb_visualtype_t *draw_default_visual(const xcb_screen_t *s); diff --git a/luaa.c b/luaa.c index 446cc7ea..9653bbbd 100644 --- a/luaa.c +++ b/luaa.c @@ -147,16 +147,24 @@ luaA_restart(lua_State *L) /** Load an image from a given path. * * @param name The file name. - * @return A cairo surface as light user datum. + * @return[1] A cairo surface as light user datum. + * @return[2] nil + * @treturn[2] string Error message * @function load_image */ static int luaA_load_image(lua_State *L) { + GError *error = NULL; const char *filename = luaL_checkstring(L, 1); - cairo_surface_t *surface = draw_load_image(L, filename); - if (!surface) - return 0; + cairo_surface_t *surface = draw_load_image(L, filename, &error); + if (!surface) { + lua_pushnil(L); + lua_pushstring(L, error->message); + g_error_free(error); + return 2; + } + /* lua has to make sure to free the ref or we have a leak */ lua_pushlightuserdata(L, surface); return 1;