Hello,
I’m struggling trying to get my head around something here. This is just about tidying up the code in vader, nothing absolutely essential or road-blocking. But an interesting problem. And I’m trying to be smart this time, generalizing everything as much as I possibly can.
Is there a smart way to generalize a sort of ‘tool internal’ error handling in Lua?
Currently I’ve got a number of functions that call a bunch of other functions, that call a bunch of other functions that call some other functions that… well, you get the idea. And for functions I’ve got a tool internal standardized return format of RETURN_VALUE, ERROR_MESSAGE, NOTES. The RETURN_VALUE is either nil/false (=error), in which case there is also an ERROR_MSG, or the actual return value, which is by definition true.The notes are something added to logs by the function, not really meaningful for the point here.
What I’m up against, is that I’m forced to handle each and every function call in every function that would handle errors (practically this should be every single one) with a repeating piece of code something like
local return_val, error_msg, notes = function(args)
if not return_val then
return return_val, error_msg, nil
elseif notes then
--handle notes
--with a bunch of
--lines that repeat every
--time
return return_val, nil, notes_handled
end
Basically this forms a ‘return chain’ that, if some function at some point returns false, passes all that back to a ‘main handler’ that will then display the error and halt execution and do some special dance or whatnought.
So. My question is: is there a way to wrap that block inside some neat, generalized (preferably global?) function that would handle the above situation where the wrapped function returns false/nil. It would then pass the return value again to the next caller(/hopefully a magical wrapper?). OR a way to return it directly to the ‘main handler’.
I’m aiming at something like:
my_special_assert(function, args)
…that would skip the if, then, elseif, then, end -part in some strange way possibly involving hidden unicorn powers.
But I have zero knowledge if this is doable in Lua (or any other language for that matter)…?
So…halp.?