lua-users home
lua-l archive

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


It was thus said that the Great Andrew Starks once stated:
> I haven't settled on a standardized way to error and I need to. At work,
> we've been debating one of two general strategies:
> 
>  - make an 'error' method that creates an error object, which can log the
> error, put in the boiler plate text, add the object's name, etc. Once
> created, it would throw the error.
> 
>     obj:error(400, "You needed to foo the bar before plotzing.")
> 
> The other option is somewhat similar, except that it would return `nil,
> error_msg`
> 
>     return obj:error(405, "Eating babies is not permitted with these
> credentials.")
> 
> In practice, I find that it's hard to tell what is happening, especially in
> a coroutine, and especially if I use the `nil, error` approach. I generally
> like my errors to be loud and obvious and haven't had a ton of success with
> 'quietly moving on.'
> 
> 
> Soo..... what do smart people do?

  A project at work uses coroutines (and sockets---each coroutine handles a
network request).  I don't bother with pcall(), I just let the coroutines
die and print a message if coroutine.resume() fails:

	-- RQUEUE is the queue of runable coroutines

	local obj = table.remove(RQUEUE,1)
    
	if coroutine.status(obj[1]) == 'suspended' then
	  local okay,err = coroutine.resume(unpack(obj))
	  if not okay then
	    syslog('error',"NIE0035: coroutine %s dead: %s",tostring(obj[1]),err)
	  end
	end

  Generally speaking, this catches nil references (mostly caused by typos,
or not checking the results of a function call properly) and other such type
bugs.  The only place I actually call pcall() is from C, which catches the
same type of errors as above, but from the main Lua thread.  Other errors
are caught and logged (lots of logging).

  I will note that the program is rather simple---listen for a (UDP) packet,
do some processing (maybe a DNS call, query another service) and send a
response back.  While there are points of failure (mostly a timeout when
querying DNS or the other service), just the regular "return nil,err" works
fine.

  -spc