diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2026-04-24 21:39:22 +0200 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2026-04-25 22:33:42 +0200 |
| commit | 16901492076ebf4d2a13e4c1ece629cc1076dc1e (patch) | |
| tree | 4439f89693ce2ab185ad5be7039e9aa011713762 | |
| parent | 6272094bc7265262cbca8363b425531ad0170caa (diff) | |
| download | luasystem-fix/errorhandling.tar.gz luasystem-fix/errorhandling.tar.bz2 luasystem-fix/errorhandling.zip | |
fix(term): improved errorhandling, more descriptivefix/errorhandling
| -rw-r--r-- | CHANGELOG.md | 5 | ||||
| -rw-r--r-- | src/term.c | 43 |
2 files changed, 36 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 75ab9f9..fce2d0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
| @@ -27,6 +27,11 @@ The scope of what is covered by the version number excludes: | |||
| 27 | 27 | ||
| 28 | ## Version history | 28 | ## Version history |
| 29 | 29 | ||
| 30 | ### version 0.x.x, unreleased | ||
| 31 | |||
| 32 | - Fix: improve error messages and handling on Windows. | ||
| 33 | See [#85](https://github.com/lunarmodules/luasystem/pull/85). | ||
| 34 | |||
| 30 | ### version 0.7.0, released 17-Feb-2026 | 35 | ### version 0.7.0, released 17-Feb-2026 |
| 31 | 36 | ||
| 32 | - Fix: remove two unused-variable warnings. | 37 | - Fix: remove two unused-variable warnings. |
| @@ -64,10 +64,11 @@ static void termFormatError(lua_State *L, DWORD errorCode, const char* prefix) { | |||
| 64 | static int pusherror(lua_State *L, const char *info) | 64 | static int pusherror(lua_State *L, const char *info) |
| 65 | { | 65 | { |
| 66 | lua_pushnil(L); | 66 | lua_pushnil(L); |
| 67 | if (info==NULL) | 67 | if (info==NULL) { |
| 68 | lua_pushstring(L, strerror(errno)); | 68 | lua_pushstring(L, strerror(errno)); |
| 69 | else | 69 | } else { |
| 70 | lua_pushfstring(L, "%s: %s", info, strerror(errno)); | 70 | lua_pushfstring(L, "%s: %s", info, strerror(errno)); |
| 71 | } | ||
| 71 | lua_pushinteger(L, errno); | 72 | lua_pushinteger(L, errno); |
| 72 | return 3; | 73 | return 3; |
| 73 | } | 74 | } |
| @@ -458,8 +459,7 @@ static int lst_getconsoleflags(lua_State *L) | |||
| 458 | 459 | ||
| 459 | if (GetConsoleMode(console_handle, &console_mode) == 0) | 460 | if (GetConsoleMode(console_handle, &console_mode) == 0) |
| 460 | { | 461 | { |
| 461 | lua_pushnil(L); | 462 | termFormatError(L, GetLastError(), "failed to get console mode"); |
| 462 | lua_pushliteral(L, "failed to get console mode"); | ||
| 463 | return 2; | 463 | return 2; |
| 464 | } | 464 | } |
| 465 | #else | 465 | #else |
| @@ -970,8 +970,7 @@ static int lst_readkey(lua_State *L) { | |||
| 970 | // printf("utf8_buffer_len: %d\n", utf8_buffer_len); | 970 | // printf("utf8_buffer_len: %d\n", utf8_buffer_len); |
| 971 | utf8_buffer_index = 0; | 971 | utf8_buffer_index = 0; |
| 972 | if (utf8_buffer_len <= 0) { | 972 | if (utf8_buffer_len <= 0) { |
| 973 | lua_pushnil(L); | 973 | termFormatError(L, GetLastError(), "UTF-8 conversion error"); |
| 974 | lua_pushliteral(L, "UTF-8 conversion error"); | ||
| 975 | return 2; | 974 | return 2; |
| 976 | } | 975 | } |
| 977 | 976 | ||
| @@ -1208,12 +1207,18 @@ int lst_utf8swidth(lua_State *L) { | |||
| 1208 | Gets the current console code page (Windows). | 1207 | Gets the current console code page (Windows). |
| 1209 | @function getconsolecp | 1208 | @function getconsolecp |
| 1210 | @treturn[1] int the current code page (always 65001 on Posix systems) | 1209 | @treturn[1] int the current code page (always 65001 on Posix systems) |
| 1210 | @treturn[2] nil | ||
| 1211 | @treturn[2] string error message | ||
| 1211 | @within Terminal_UTF-8 | 1212 | @within Terminal_UTF-8 |
| 1212 | */ | 1213 | */ |
| 1213 | static int lst_getconsolecp(lua_State *L) { | 1214 | static int lst_getconsolecp(lua_State *L) { |
| 1214 | unsigned int cp = 65001; | 1215 | unsigned int cp = 65001; |
| 1215 | #ifdef _WIN32 | 1216 | #ifdef _WIN32 |
| 1216 | cp = GetConsoleCP(); | 1217 | cp = GetConsoleCP(); |
| 1218 | if (cp == 0) { | ||
| 1219 | termFormatError(L, GetLastError(), "failed to get console code page"); | ||
| 1220 | return 2; | ||
| 1221 | } | ||
| 1217 | #endif | 1222 | #endif |
| 1218 | lua_pushinteger(L, cp); | 1223 | lua_pushinteger(L, cp); |
| 1219 | return 1; | 1224 | return 1; |
| @@ -1226,17 +1231,21 @@ Sets the current console code page (Windows). | |||
| 1226 | @function setconsolecp | 1231 | @function setconsolecp |
| 1227 | @tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8 | 1232 | @tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8 |
| 1228 | @treturn[1] bool `true` on success (always `true` on Posix systems) | 1233 | @treturn[1] bool `true` on success (always `true` on Posix systems) |
| 1234 | @treturn[2] nil | ||
| 1235 | @treturn[2] string error message | ||
| 1229 | @within Terminal_UTF-8 | 1236 | @within Terminal_UTF-8 |
| 1230 | */ | 1237 | */ |
| 1231 | static int lst_setconsolecp(lua_State *L) { | 1238 | static int lst_setconsolecp(lua_State *L) { |
| 1232 | unsigned int cp = (unsigned int)luaL_checkinteger(L, 1); | 1239 | unsigned int cp = (unsigned int)luaL_checkinteger(L, 1); |
| 1233 | int success = TRUE; | ||
| 1234 | #ifdef _WIN32 | 1240 | #ifdef _WIN32 |
| 1235 | SetConsoleCP(cp); | 1241 | if (!SetConsoleCP(cp)) { |
| 1242 | termFormatError(L, GetLastError(), "failed to set console code page"); | ||
| 1243 | return 2; | ||
| 1244 | } | ||
| 1236 | #else | 1245 | #else |
| 1237 | (void)cp; | 1246 | (void)cp; |
| 1238 | #endif | 1247 | #endif |
| 1239 | lua_pushboolean(L, success); | 1248 | lua_pushboolean(L, 1); |
| 1240 | return 1; | 1249 | return 1; |
| 1241 | } | 1250 | } |
| 1242 | 1251 | ||
| @@ -1246,12 +1255,18 @@ static int lst_setconsolecp(lua_State *L) { | |||
| 1246 | Gets the current console output code page (Windows). | 1255 | Gets the current console output code page (Windows). |
| 1247 | @function getconsoleoutputcp | 1256 | @function getconsoleoutputcp |
| 1248 | @treturn[1] int the current code page (always 65001 on Posix systems) | 1257 | @treturn[1] int the current code page (always 65001 on Posix systems) |
| 1258 | @treturn[2] nil | ||
| 1259 | @treturn[2] string error message | ||
| 1249 | @within Terminal_UTF-8 | 1260 | @within Terminal_UTF-8 |
| 1250 | */ | 1261 | */ |
| 1251 | static int lst_getconsoleoutputcp(lua_State *L) { | 1262 | static int lst_getconsoleoutputcp(lua_State *L) { |
| 1252 | unsigned int cp = 65001; | 1263 | unsigned int cp = 65001; |
| 1253 | #ifdef _WIN32 | 1264 | #ifdef _WIN32 |
| 1254 | cp = GetConsoleOutputCP(); | 1265 | cp = GetConsoleOutputCP(); |
| 1266 | if (cp == 0) { | ||
| 1267 | termFormatError(L, GetLastError(), "failed to get console output code page"); | ||
| 1268 | return 2; | ||
| 1269 | } | ||
| 1255 | #endif | 1270 | #endif |
| 1256 | lua_pushinteger(L, cp); | 1271 | lua_pushinteger(L, cp); |
| 1257 | return 1; | 1272 | return 1; |
| @@ -1264,17 +1279,21 @@ Sets the current console output code page (Windows). | |||
| 1264 | @function setconsoleoutputcp | 1279 | @function setconsoleoutputcp |
| 1265 | @tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8 | 1280 | @tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8 |
| 1266 | @treturn[1] bool `true` on success (always `true` on Posix systems) | 1281 | @treturn[1] bool `true` on success (always `true` on Posix systems) |
| 1282 | @treturn[2] nil | ||
| 1283 | @treturn[2] string error message | ||
| 1267 | @within Terminal_UTF-8 | 1284 | @within Terminal_UTF-8 |
| 1268 | */ | 1285 | */ |
| 1269 | static int lst_setconsoleoutputcp(lua_State *L) { | 1286 | static int lst_setconsoleoutputcp(lua_State *L) { |
| 1270 | unsigned int cp = (unsigned int)luaL_checkinteger(L, 1); | 1287 | unsigned int cp = (unsigned int)luaL_checkinteger(L, 1); |
| 1271 | int success = TRUE; | ||
| 1272 | #ifdef _WIN32 | 1288 | #ifdef _WIN32 |
| 1273 | SetConsoleOutputCP(cp); | 1289 | if (!SetConsoleOutputCP(cp)) { |
| 1290 | termFormatError(L, GetLastError(), "failed to set console output code page"); | ||
| 1291 | return 2; | ||
| 1292 | } | ||
| 1274 | #else | 1293 | #else |
| 1275 | (void)cp; | 1294 | (void)cp; |
| 1276 | #endif | 1295 | #endif |
| 1277 | lua_pushboolean(L, success); | 1296 | lua_pushboolean(L, 1); |
| 1278 | return 1; | 1297 | return 1; |
| 1279 | } | 1298 | } |
| 1280 | 1299 | ||
