Error Handling#
RCSW functions report failure through their return value and, where it helps,
errno. There are no exceptions, callbacks or global error state beyond
errno.
Return values#
Functions that can fail and return nothing else return
status_t:OK(0) orERROR(-1).Functions that return a pointer return
NULLon failure.Functions that return an index return -1 on failure.
Query functions (
xx_size(),xx_isempty(), …) return 0,falseorNULLwhen given an invalid handle. These values can’t be told apart from a valid answer, so validate handles before relying on them.
errno is set on many failure paths, but not all of them; treat it as a
hint about why something failed, and the return value as the fact that it
did. Error Codes lists the values the data structures use.
Function preconditions#
Public functions check their arguments with the FPC (function precondition)
macros in rcsw/core/fpc.h. What a failed check does is chosen at build
time with LIBRA_FPC:
Mode |
On a failed precondition |
|---|---|
|
The function sets |
|
The check is an |
|
Nothing is checked. Invalid arguments are undefined behaviour. |
The checks are short-circuited: the first failed condition decides the result.
Cleanup on failure#
Functions that acquire resources step by step use a single error: label
for cleanup. The checking macros jump to it:
Macro |
Jumps to |
|---|---|
|
|
|
|
|
|
|
|
|
Always, after reporting |
status_t widget_init(struct widget* w) {
w->buf = malloc(64);
RCSW_CHECK_PTR(w->buf);
ER_CHECK(OK == bus_attach(w), "Failed to attach widget");
return OK;
error:
free(w->buf);
return ERROR;
}
For conditions that must never fail, ER_ASSERT reports a FATAL
event and then calls assert(), and ER_FATAL_SENTINEL reports a
FATAL event and calls abort(). With NDEBUG defined, ER_ASSERT
still evaluates the condition and reports the event, but the assert()
doesn’t stop the program.