lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Alex Queiroz wrote:
Hallo,

On 1/25/07, Jan Schütze <JanS@dracoblue.de> wrote:

This isn't only used to report an error to users on website.
Since I use lua for webpublishing, I have somewher ein my source a
function called xml_render() - this one, should use die at the end, to
prevent from showing header and stuff when the rest of the script is
executed. The issue is, that this function is called mostly somewhere in
a set of sets of function-sets, so its not possible (easily, without
making the source unreadable) to do this via using if-else-constructs.


    You can catch the error before it reaches the user with pcall().


I myself use a function abort(), which is something like:

local _die = {}
function abort() error(_die) end

And to catch it (pseudo code):


local handler = gethandlerforrequest( url )
local ok, err = pcall( handler )

if not ok and err ~= _die then
    -- something bad happened. Log it
    log.error(
        'An error occurred at a random address. ' .. tostring( err )
    )
end


Note that _die isn't visible anywhere else, so no one would be able to mimic abort() behavior.

--rb