The error happens whether it is handled or not, the code itself is not aware of errors unless they are well defined complier error (i.e. division by zero, square root of negative number,etc.). You have called error to tell yourself of the error so that is all the code can ever know, it can’t know the error source if it is not complier/mathematically defined. So the error location will always be where you tagged the failing, badly executed code from, i.e. the line it prints the error. In C++ the convention is to use try/catch blocks at the end of each function, but the code will always break on the error incidence if you want to farm-out the error handling to a class/struct (or in Lua’s case a Module). Generally you supply the line number and the function name, and the parameters that were passed to the erroring function. So an error handler would have inputs like this:
local module = {}
module.MyFunction = function(script_name,function_name,line_number,passed_string,error_lvl,params)
local message = ("The script ".. script_name .. " called a function " .. function_name .. " on line " .. line_number .. " and returned the error " .. passed_string .. " with a severity level of " .. error_lvl .. " the supplied params were ");
local param_string = "";
for i = 1, #params do
message ..= " " .. tostring(params[i]);
end
error(message, error_lvl);
end
return module;
Calling convention (this will still only report the error on the line within the module, but now you have info of the error source):
local module = require(game.ServerStorage.BadScript);
local function squareRoot(a)
if a < 0 then module.MyFunction("Bad script","squareRoot",5,"Square root of negative number",2,{a}) end;
return math.sqrt(a);
end
local function divideBy(a,b)
if b == 0 then module.MyFunction("Bad script","divideBy",11,"Division by zero",2,{a,b}) end;
return a/b;
end
squareRoot(-1);
divideBy(5,0);
Outputs have all the info you need as a programmer for debugging purposes: