Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

error handling constants #52

wants to merge 3 commits into


None yet
3 participants

estenberg commented Apr 26, 2012

Much like the cfapi_errid in Nova.
It is useful if a function may return something else than just true/false.
Normally you have at least three states: true/false and some kind of error (e.g. in ReadDB: false may mean not found or read error).

We could merge the cfapi_errid constants into this one, to have just one error struct.

This approach has the following problems:

  • If function returns cf_errid, then it is potentially returns any of the errors defined in the enum. This works to the some extent in Unix tradition with errno, but only due to the fact every function documents all the errors that could be returned, and the reasons why the errors are returned.
  • handling errors with switch() statement will either have to use "default:" case, which is error-prone, or huge list of case CFERRID_THIS_ONE: FatalError("can't happen").
  • It breaks if(fun()) convention, as the only cf_errid value which evaluates to False is CFERRID_SUCCESS. Negative error codes would help though, if(fun() >= 0) is more-or-less accepted convention.
  • if (fun() == CFERRID_SUCCESS) is ugly.

vohi commented Apr 27, 2012

I agree with the problem statement, but I also agree with Mikhail - this is very Microsoft COM-like, which is a synonym for "very easy to get wrong". Unfortunately we can't overload a cast-operator to bool in C.

I'd suggest to split the two commits 52 and 53 - the simplification in 53 seems ok, but the error handling change needs a bit more discussion, I think.


estenberg commented Apr 27, 2012

Oki, thanks for the comments.
I like the convention of negative meaning some kind of error, while zero/positive may give a positive status code (e.g. bytes read).

It still is not beautiful if you want to print a message or stop on error.

if((status = SomeFunction()) <= 0)
// print error with status

There aren't too many options for error handling to choose from in plain C, unfortunately.

I'll try to split 52 an 53 and use some magic error or writing to a param there if I need to for now..
I leave this open until we have agreed on something.

@estenberg estenberg closed this Dec 2, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment