# Failures

The failure object dmog writes on standard error, every failure code, and the exit status each one gives.

A command that fails writes one JSON object on standard error, on one line, and exits with a status other than 0. Standard output then holds what was written before the failure, which may be nothing. A library returns the same failure as a value, with the same fields and codes, and no exit status: see [The libraries](https://dmog.tabnas.dev/reference/library.html#failures).

## The failure object

| Field | Always | Value |
|---|---|---|
| `code` | yes | The failure's code, from the table under [Codes](#codes). |
| `message` | yes | A sentence for a person. Where a finer code applies, the message starts with it and a colon, as in `unknown_name: jsn is not defined`. |
| `output` | yes | `"none"` when nothing reached standard output, `"partial"` when some output did. |
| `row`, `col` | no | The place in a program the failure concerns, from 1, columns counted in characters. |
| `file` | no | The program file `row` and `col` are in, where it is not the one named on the command line. |
| `path` | no | The place in the document, as `.name` or `.items[0]`. |
| `limit` | no | The limit that was reached: `name` and `value`. |

A code is never renamed or reused for another meaning. A code or a field may be added, so a script should ignore a field it does not know.

## Codes

| Code | Status | Meaning |
|---|---|---|
| `DSL_PARSE_ERROR` | 2 | The program's text does not read: its layout, its brackets, or a string. |
| `DSL_TYPE_ERROR` | 2 | The program does not check: an unknown name, a wrong number of arguments, a type or protocol that does not match. |
| `STREAM_REUSED` | 2 | The program reads one stream twice. |
| `STREAMABILITY_UNKNOWN` | 2 | The program's streaming cannot be established, or a document cannot be streamed and needs `--whole`. |
| `INPUT_INVALID` | 1 | The document does not parse as its format, a path names nothing, or the program called `fail`. |
| `INPUT_ORDER_VIOLATION` | 1 | The document's parts arrived in an order the program forbids, such as a row before the table's columns. |
| `CAPTURE_OVERLAP_UNSUPPORTED` | 1 | Two of the program's captures select overlapping parts of the document. |
| `MISSING_VALUE` | 1 | A value the program requires is absent. |
| `DUPLICATE_MEMBER` | 1 | An object repeats a key where the run cannot keep both. |
| `INVALID_NUMBER` | 1 | A number cannot be written in the target format. |
| `PROTOCOL_ORDER_ERROR` | 1 | Events arrived out of sequence, such as a row of the wrong width. |
| `TARGET_VALUE_UNREPRESENTABLE` | 1 | The target format cannot hold the value. |
| `OUTPUT_FAILED` | 3 | Writing to standard output failed. |
| `RESOURCE_LIMIT_EXCEEDED` | 5 | A limit was reached, such as `--max-output-bytes`. |
| `ABORTED` | 6 | The run was stopped. |

## Two exceptions to the table

A command line dmog cannot read, and a file it cannot read, fail with `INPUT_INVALID` and exit with status 2, not 1. Both are faults of the command line, not of the document. Bytes that are not UTF-8, met part way through a document, are a file that cannot be read.

## Exit statuses

| Status | Meaning |
|---|---|
| 0 | Success. |
| 1 | The input, or what the program made of it, could not be written. |
| 2 | The program, the command line, or a file is at fault. |
| 3 | The output could not be written. |
| 5 | A limit was reached. |
| 6 | The run was stopped. |

A standard error that cannot be written loses the failure object, and the exit status is still given.
