ref: eb4255471af9a9624e64af2c7a9b692d2b60a8ff
parent: c5077847e0cdadbbff06fa453ae309a66f25c9eb
author: phil9 <telephil9@gmail.com>
date: Fri Dec 2 02:35:04 EST 2022
proper error handling
--- a/api.c
+++ b/api.c
@@ -54,7 +54,7 @@
cnoloop(lua_State*)
{
looping = 0;
- return LUA_OK;
+ return 0;
}
int
@@ -61,7 +61,7 @@
cloop(lua_State*)
{
looping = 1;
- return LUA_OK;
+ return 0;
}
int
@@ -71,9 +71,9 @@
n = luaL_checkinteger(L, 1);
if(n < 0)
- return LUA_ERRRUN;
+ return luaL_argerror(L, 1, "framerate should be greater than 0");
framerate = n;
- return LUA_OK;
+ return 0;
}
int
@@ -109,7 +109,7 @@
b = luaL_checkinteger(L, 3);
i = color(r, g, b, colormode == Chsv);
}else{
- fprint(2, "invalid color request\n");
+ luaL_error(L, "invalid argument count (expected 1 or 3 but received %d)", c);
return nil;
}
return i;
@@ -121,12 +121,10 @@
int n;
n = luaL_checkinteger(L, 1);
- if(n != Crgb && n != Chsv){
- fprint(2, "error: invalid color mode\n");
- return LUA_ERRRUN;
- }
+ if(n != Crgb && n != Chsv)
+ return luaL_argerror(L, 1, "expected RGB or HSV");
colormode = n;
- return LUA_OK;
+ return 0;
}
int
@@ -135,8 +133,6 @@
Image *i;
i = getcolor(L);
- if(i == nil)
- return 1;
draw(canvas, canvas->r, i, nil, ZP);
return 0;
}
@@ -165,8 +161,6 @@
Image *i;
i = getcolor(L);
- if(i == nil)
- return 1;
stroke = i;
nostroke = 0;
return 0;
@@ -185,8 +179,6 @@
Image *i;
i = getcolor(L);
- if(i == nil)
- return 1;
fill = i;
nofill = 0;
return 0;
--- a/slug.c
+++ b/slug.c
@@ -51,9 +51,9 @@
r = lua_pcall(L, 0, 0, base);
lua_remove(L, base);
if(r != LUA_OK){
- fprint(2, "error: %s\n", lua_tostring(L, -1));
+ fprint(2, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
- return;
+ threadexitsall("error");
}
drawcanvas();
}