|
On 09/19/2014 04:39 PM, Andrew Starks wrote:
Hisham / Jorge: How do you deal with the following: 1: You parse the input that you receive and you know it to be well-formed. 2: You call a library function that will look for a resource by that name and, if it fails, returns `nil, error`. 3: That same call may also generate an error. For example, perhaps the object contains a dependent resource that is in an invalid state.
First, as I said, I don't expect to library to crash me. Then I accept the library might be unable to return anything back, for _any_ reason. This means there's a "if ret then" or equivalent somewhere. Alternatively, if this result is absolutely critical for me continuing doing stuff, I might wrap the call in an assert. I could also do that while developping, to reduce the number of cases I have to handle.
If there's no assert, and I got no result, then I might ask what happened. In your example, I would expect to find in the docs the "resource does not exist" error, and perhaps a bunch of other weird error conditions. Probably all bundled in a "unknown error" :)
In other words, I either have a result, or not, and then all the reasons why not are in one single place.
So my app would be something like: local ret, err = lib.call('resource_name') if ret then --dostuff with ret else --as long as I care if err == "does not exist" then --think hard what to do and why i'm here else error(err) end endIt's a bit like the socket API and the EAGAIN, which is barely an error at all, and all the other errors: they are very different conceptually, all they share is that there's no data returned, but are all handled in the same manner.
Jorge