-
Notifications
You must be signed in to change notification settings - Fork 1
[bug]: loadfile can fail with no error message displayed #2
Copy link
Copy link
Open
Description
Lua docs on loadfile, state it can return nil and a error message, or a function.
So we can optimize the code so it can either stop the server because of this error and display it, or just log the error and continue in a disabled state.
Snip of init.lua L66-L82 (in the "Import boosts config" section)
local boosts = dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/boosts.lua") or {}
local worldboosts_file = io.open(minetest.get_worldpath() .. "/boosts.lua", "r")
if worldboosts_file then
-- boosts = loadstring(worldboosts_file:read("*all"))() or {}
local loaded
local err
loaded, err = loadstring(worldboosts_file:read("*all"))
if loaded == nil then
-- Your choice
minetest.log("action", "[Boosts] Failed loading world-specific boosts config")
minetest.log("action", "Error: " .. err)
-- OR
error("[Boosts] Failed loading world-specifc boosts config, got error: " .. err)
else
boost = loaded() or {}
worldboosts_file.close()
minetest.log("action", "[Boosts] Using world-specific boosts configuration")
end
end
if get_table_length(boosts) == 0 then
minetest.log("warning", "[Boosts] No boosts configured")
endI recommend just logging the error and continue on with default (from within mod directory), but you may prefer to stop the server on load of invalid configuration.
The nice part with this is either way our user gets a nice error message on exactly what failed
Like in my case I was miss typed a period
.instead of a comma,when I was building myboosts.luain the world directory.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels