selection: better error messages for errors
-
CODE
vs.RCODE
is a typo, I guess. I think it will be best to define strings for the whole enum without any exceptions. What about this approach instead?static const char *kr_selection_error_str(enum kr_selection_error err) { switch (err) { #define X(ENAME) case KR_SELECTION_ ## ENAME: return #ENAME X(OK); X(QUERY_TIMEOUT); // all of them listed here #undef X } assert(false); // we want to define all; compiler helps by -Wswitch return NULL; }
Advantages:
- it's less copy&paste
- no such typos are possible :-)
- we get a warning when we miss an enum
../lib/selection.c: In function 'kr_selection_error_str': ../lib/selection.c:56:2: warning: enumeration value 'KR_SELECTION_TLS_HANDSHAKE_FAILED' not handled in switch [-Wswitch] 56 | switch (err) { | ^~~~~~
- optimizing compilers convert such switches to a lookup table anyway (though we probably don't really need to care about performance of this function)
In some cases similar macros are used already in definition to avoid even the need to repeat the whole identifier list, but here we probably want to separate the enum in header from this function outside header. And the list isn't expected to be too long anyway.
-
-
mentioned in commit 83487580
-
OK, pushed atop (now as 83487580).
Please register or sign in to comment