]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
lua: Don't raise an error if a network cannot be found
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 31 Mar 2024 16:33:34 +0000 (16:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 31 Mar 2024 16:33:34 +0000 (16:33 +0000)
The lookup function now returns nil which is easier to handle than
catching a Lua error.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/lua/database.c

index 841041d89df6472363b584f0b8a6f38f9bb93f60..aada739edd65eb2b4d3ed116a6b48b4a88bfae76 100644 (file)
@@ -174,8 +174,17 @@ static int Database_lookup(lua_State* L) {
 
        // Perform lookup
        r = loc_database_lookup_from_string(self->db, address, &network);
-       if (r)
-               return luaL_error(L, "Could not lookup address %s: %s\n", address, strerror(errno));
+       if (r) {
+               switch (errno) {
+                       // Return nil if the network was not found
+                       case ENOENT:
+                               lua_pushnil(L);
+                               return 1;
+
+                       default:
+                               return luaL_error(L, "Could not lookup address %s: %s\n", address, strerror(errno));
+               }
+       }
 
        // Create a network object
        r = create_network(L, network);