D.3 Errors, exit codes and the failure record
What you will learn
- Describe the single failure type of the program and what every failure carries.
- Choose the right exit code for a new failure, and write its message and hint.
- Explain why a bug is an exit code, not a crash, and how a test proves it.
Prerequisites: D.2 and Chapter 39, errors, warnings and troubleshooting.
One failure type
Every function that can fail returns a result whose error is a single type: a failure with three parts.
| Part | Purpose | Rule for writing it |
|---|---|---|
code |
the stable exit code | chosen from the table below; never invented |
msg |
what went wrong | one sentence, naming the file, the key or the value involved |
hint |
what to do next | optional; present whenever a user can act (a corrected command, a template to write) |
Two helpers make the cheap cases cheap: one builds a failure with a code and a message, and one adds the hint. A function can add context to a failure from deeper in the stack, which prefixes the message but keeps the code and the hint, so a message about a parameter reads, from outside in, "the settings file x.qcd: [data]: unknown key…".
The principle is stated in the module's first lines: failures are never silently repaired. A refused input is reported; the program does not substitute a default, guess a column or skip a row without saying so. This is also what the validation package relies on (Chapter 42, 42.14).
The exit codes
| Code | Name | Raised when | Typical hint |
|---|---|---|---|
| 0 | success | the command completed (a warning does not change this) | – |
| 2 | invalid usage | an option or a combination of options is invalid | the corrected option |
| 3 | file discovery or I/O failure | a file cannot be found or read, a folder cannot be created | check the path and the permissions |
| 4 | invalid configuration | a settings file is unreadable, has an unknown key or a bad value | write the annotated template with --print-config |
| 5 | unsupported model feature | a model statement or feature the program does not support | the supported alternative |
| 6 | inconsistent NONMEM artifacts | result files are missing, or do not match the control stream | which file and why |
| 7 | data validation failure | the dataset fails a check the command is told to stop on | the summary file to read |
| 8 | posterior reconstruction failure | the posterior engine cannot rebuild what it needs | – |
| 9 | numerical or learner failure | a computation did not give a usable number (singular matrix, a learner that cannot fit) | the setting to change |
| 10 | no evaluable relationships | nothing is left to analyse after the filters and scope | relax the filter or the scope |
| 11 | resume/checkpoint incompatibility | a resume file does not match the run | rerun, or keep the original settings |
| 12 | internal invariant violation | a bug: a panic caught by the guard | report it with the command line and the transcript |
The numeric failure and the learner failure share code 9: both mean that a computation did not produce a usable number. The codes are a public interface: pipelines branch on them, the book documents them, and the validation tests assert them. Adding a code is a MAJOR-level decision; reusing one for a new case is the normal way (page D.13).
Choosing a code
Ask, in order:
- Could the user have prevented it by writing a different command line? Then 2 (usage).
- Is a file or folder the problem? 3 for an operating-system failure, 4 for a settings file whose content is wrong, 6 for the NONMEM artifacts, 7 for the dataset's content.
- Is the computation the problem? 9, with a hint naming a setting that would change it, or 10 if there was simply nothing to compute.
- Is it the program's own fault? Do not choose; the guard assigns 12.
The guard: a bug is a failure, not a crash
The program runs the dispatcher inside a guard that catches a panic from any thread, remembers the first one (a worker's panic is re-raised on the caller, so the first is the cause) and converts it into a failure with code 12. The message says it is a bug in the program, includes where it happened, and asks for the command line and the transcript. The result: the failure goes through the same logging as every other, so the transcript in the output folder records it, and the language's bare abort code and message never reach the user.
How do you know it works? There is an end-to-end test that sets an environment variable which makes the dispatcher panic on purpose, and asserts three things: exit code 12, the text "internal error (a bug in nlmx)", and the absence of the raw panic message (page D.12). A guard that has never been exercised is an assumption.
Writing a good failure
Compare two messages for the same fault.
poor:
invalid configurationgood:
qc-data configurationstudy.qcd: unknown fieldfial_on, expected one offail_on, … (hint: write the annotated reference withnlmx qc-data --print-config qc-data_template.txt)
The good one names the command, the file and the offending key, offers the valid alternatives (from the settings library) and says how to get the template. Three tests follow: can the user find the line to change from the message alone? does the hint tell them the next command to type? would a script be able to branch on the code?
Review checklist
- Does every new failure return a code from the table, with a message that names the offending file, key or value?
- Is there a hint whenever the user can act?
- Does a test assert the exit code (not only that "something failed")?
- If the change adds a failure that a pipeline would treat differently, is it documented in Chapters 38 and 39?
Navigation
See the previous page, the next page and the developer reference.