[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Error handling
- From: Romulo Bahiense <romulo@...>
- Date: Fri, 17 Feb 2006 09:02:16 -0300
function raise(errtable)
error(setmetatable(errtable, errtablemt))
end
When I arrived home last night, I realized this will not work as
expected, because __tostring metamethod would be called only when the
message is displayed.
The first potential fix is to calling tostring before ivoking 'error()',
but it will break the meaning of having a table anyway.
function raise(errtable)
error(tostring(setmetatable(errtable, errtablemt)))
end
-- Which could be converted to:
function raise(errtable)
error(
string.format(
'An error ocurred with error code %d: %s.\n%s',
errtable.code,
errtable.msg,
debug.traceback()
)
)
end
The second method, probably the best:
errtablemt = {
__tostring = function(t)
return string.format(
'An error ocurred with error code %d: %s.\n%s',
t.code,
t.msg,
t.stack -- Note that I don't call debug.traceback() anymore
)
end
}
function raise(errtable)
errtable.stack = debug.traceback()
error(setmetatable(errtable, errtablemt))
end
With this version you will have the correct stack trace as long as you want.
--rb