Packages

UCUM, the Unified Code for Units of Measure: parse, check and convert unit codes such as kg/m2, mm[Hg] and {request}/s.

Current section

Files

Jump to
ucum spec 03-errors.md
Raw

spec/03-errors.md

# 03 — Errors {#errors}

Every refusal is a value, `{:error, reason}` (spec/00 §3). `Ucum.format_error/1` writes
any reason below as a sentence a person reads, and each `!` twin raises `Ucum.Error` with
`reason` and that sentence as `message`.

## 1. A string that is not a code {#errors-parse}

| Reason | When |
|---|---|
| `:empty` | the string is empty |
| `{:syntax, rest}` | the code does not read at `rest`, the first part that does not; for a term that is no string, its text as `inspect/1` writes it, in at most 40 characters (§4) |
| `{:unknown_atom, symbol}` | `symbol` is neither an atom of the table nor a prefix and an atom (spec/01 §3) |
| `{:not_metric, prefix, atom}` | `prefix` is on an atom UCUM does not call metric |
| `{:too_large, code}` | `code` writes a number of more than 19,729 digits (spec/01 §6) |

These are `parse/1`'s, and so every function's that takes a code: `dimension/1`,
`factor/1`, `kind/1`, `symbol/2`, `name/1`, `canonical/1`, and each code of
`conversion/2`, `convert/3`, `multiply/2` and `divide/2`. `case_sensitive/1` answers the
same for a string that is no case-insensitive code (spec/01 §5).

`{:too_large, code}` is also the reason of `factor/1`, `canonical/1`, `conversion/2`,
`convert/3`, `multiply/2` and `divide/2` for a code that reads and whose factor is past
65,536 bits, or whose numbers are too many to work (spec/01 §6); `parse/1` and the functions that take no factor answer that
code.

`atom/1` refuses a string that is not one atom of the table with `{:unknown_atom, string}`,
and `prefix/1` one that is not a prefix with `{:unknown_prefix, string}` (spec/01 §4); each
refuses a term that is no string with `{:syntax, text}`.

## 2. A pair that does not convert {#errors-conversion}

| Reason | When |
|---|---|
| `{:dimension, from, to}` | the two codes measure different dimensions |
| `{:not_linear, from, to}` | one dimension, but a special unit's function stands between the two, so there is no scale and offset; `conversion/2` only, since `convert/3` converts a value through the function (spec/02 §4, §6) |
| `{:special_in_term, code}` | `code` holds a special unit inside a term, where it has nothing to convert by (spec/02 §4) |
| `{:out_of_domain, value, from, to}` | `convert/3` only: a special unit's function has no answer for `value` (spec/02 §6) |
| `{:out_of_range, value, from, to}` | `convert/3` only: `value` in `to` is past what a float holds (spec/02 §9) |
| `:out_of_range` | `convert/2` only: the converted value is past what a float holds (spec/02 §9) |
| `:not_a_number` | `convert/2` or `convert/3`'s value is no number |
| `{:not_a_conversion, term}` | `convert/2` only: `term` is not `{scale, offset}` as `conversion/2` answers them, a positive `{n, d}` or `{n, d, k}` of integers and an offset `{n, d}` with `d` positive (the value is checked first) |

Either code of a pair that does not read is its own §1 reason.

## 3. Two quantities that do not multiply or divide {#errors-arithmetic}

| Reason | When |
|---|---|
| `{:special_in_term, code}` | `code` holds a special unit, which takes part in no product (spec/02 §8) |
| `{:not_a_quantity, term}` | `term`, an argument, is no `{value, code}` pair (the first of the two that is none) |
| `:not_a_number` | a quantity's value is no number |
| `:division_by_zero` | `divide/2`'s divisor has the value zero |
| `:out_of_range` | the product or the quotient is past what a float holds (spec/02 §9) |

A quantity's code that does not read is its own §1 reason.

## 4. An argument of the wrong shape {#errors-shape}

A function given a term of a shape it does not take answers a reason, as for any other
refusal, and does not raise (spec/00 §3). A term that is no string where a code goes is
`{:syntax, text}` (§1), from `parse/1` as from every other function of a code; an
argument of `multiply/2` or `divide/2` that is no `{value, code}` pair is
`{:not_a_quantity, term}` (§3); a conversion `convert/2` does not take is
`{:not_a_conversion, term}` (§2).

| Reason | When |
|---|---|
| `{:bad_options, opts}` | `symbol/2`'s options are no keyword list, hold an option other than `:annotations`, or an `:annotations` that is no boolean (spec/02 §5) |

`format_error/1` answers a sentence for every term: a reason of this library in its own
words, and any other term, or one of these tags with fields of other types, by a sentence
saying it is no reason this library answers.

**A refusal costs no more to write than to answer.** A caller’s term that a reason or a
sentence shows (a value, a code, the unread rest of one, a term of another shape) is
shown in at most 40 characters, cut with `…`; an integer past 10¹⁸ by its size alone
(`<an integer of 3986314 bits>`), a collection by its first items. So 10^1,200,000 `m`
in `m` is refused, and put in words, in milliseconds, and a sentence is at most a few
hundred characters whatever it was given. A short term reads in full: 1.0e300 `Gm` in
`nm` is past what a float holds. A reason that holds a term (`{:out_of_range, value,
from, to}`) holds it as given, so a program that matches on it loses nothing.

```test:units-shape
every public function answers its declared shape for arguments of any shape, never a raise; each reason of a wrong shape is answered where it applies, and its ! twin raises it; format_error/1 answers a bounded sentence for every term
```