A Layered Service With Honest Errors
Challenge on lesson: Checkpoint: Errors You Can Trust
Assemble the module: a three-layer pipeline where each layer
translates errors, cleans up its own resources, and never lies. The
boilerplate declares:
``c
/* layer 3 (top) codes */
#define APP_OK 0
#define APP_ECONFIG 1 /* config stage failed */
#define APP_EDATA 2 /* data stage failed */
#define APP_EARGS (-1) /* contract violation by caller */
/* layer 2 stages: 0 ok, -1 fail. Records what happened: */
/* config: parses "k=v
..." lines (max 8, key/value <= 7 chars) */
int cfg_stage(const char *input, char *report, size_t cap);
/* data: sums "n
" lines; all lines must be nonneg ints */
int data_stage(const char *input, long *total, char *report, size_t cap);
/* layer 3: runs both stages in order; first failure aborts and
returns the APP_E* code for the FAILING stage (args bad -> APP_EARGS).
report (cap chars) gets "config:..." then "data:..." details. */
int app_run(const char *cfg_in, const char *data_in, char *report, size_t cap);
``
The error taxonomy is the test: config failures and data failures must
be DISTINGUISHABLE at the top, and caller errors (NULL report) must be
a third thing.
Difficulty: intermediate
Back to lesson: Checkpoint: Errors You Can Trust