This document describes how errors are reported and how to handle them when using or contributing to the cheese library.
The public C API in include/cheese.h uses the following conventions:
- Success: Returns a valid pointer (e.g.
cheese_model *,cheese_context *,cheese_vocab *). - Failure: Returns
NULL. The caller must check forNULLbefore use.
Examples: cheese_model_load_from_file(), cheese_init_from_model(), cheese_model_get_vocab().
- Success: Typically returns
0or a non-negative value (e.g. number of tokens, size). - Failure: Returns a negative value or non-zero error code, depending on the function. See the function documentation in the header.
Examples: cheese_decode() returns non-zero on failure; cheese_tokenize() returns negative token count on error when the output buffer is NULL.
- Always check return values and pointers before use. Do not call other cheese API functions with
NULLor after a failed call unless the API explicitly allows it. - On failure, release any resources already obtained (e.g. call
cheese_model_free(model)ifcheese_init_from_model()fails after a successfulcheese_model_load_from_file()).
- Return codes: Use
intorboolreturn values for functions that can fail in a recoverable way; document the meaning of non-zero orfalse. - Exceptions: Internal C++ code may use
throw std::runtime_error(or similar) for fatal, non-recoverable failures (e.g. model load failure when mmap or critical read fails). The C API catches these at the boundary and turns them intoNULLor error return values so C callers never see exceptions. - Logging: Use
CHEESE_LOG_ERRORandCHEESE_LOG_WARNfor diagnostics. Logging is for debugging and operator visibility; it does not replace returning an error to the caller. Prefer returning a clear error to the caller and logging once at the point of failure.
| Situation | Prefer |
|---|---|
C API boundary (public cheese.h functions) |
Return codes and NULL; never let exceptions escape to C callers. |
| Internal C++ code, recoverable failure | Return code or false so the caller can retry or clean up. |
| Internal C++ code, fatal failure (e.g. corrupted file, OOM) | Exception or return code; ensure C API translates to NULL/error. |
| Optional or best-effort behavior | Log with CHEESE_LOG_WARN and continue; do not fail the whole operation. |
When adding or changing code, follow the existing pattern in the same file or module: if the rest of the file uses return codes, use return codes; if it uses exceptions for fatal paths, keep that consistent. For new C API functions, always document in the header whether failure is indicated by NULL, a negative value, or another convention.