Macaw.Base.Fault (macaw_base v0.1.0)

Copy Markdown View Source

A fault, {path, reason}, and the one formatter that words it (spec/01 §3).

The path runs from the value's root: atoms or strings for keys, non-negative integers for list positions. A key the type does not know stays at its own spelling, a string. Every fault is reported, never just the first, ordered by path (sort/1).

format/2 words a fault as path: words, and sentence/2 an {:error, reason}'s term as one sentence. The words come from the caller first: each function given as words: in turn, answering words or nil for a reason it doesn't word; then base's general words (general/1); then the reason as its term. So a package words its own reasons, and may word a general one in its own terms:

iex> Macaw.Base.Fault.format({[:frames, "main", "wdth"], :unknown_key})
"frames.main.wdth: is not a key of this node"
iex> Macaw.Base.Fault.format({[], {:newer_schema, 4, 3}}, root: "design")
"design: needs schema v4, this is v3"
iex> Macaw.Base.Fault.format({[:x], :unknown_key}, words: &if(&1 == :unknown_key, do: "is not a key it takes"))
"x: is not a key it takes"

The formatter is visualize's Visualize.Chart.Validator.format/1 and the general part of its wording, by way of Macaw.Layers.Fault, with Macaw.Data.Fault's sentence and its order of words merged in.

Summary

Types

format/2's and sentence/2's options.

Where a fault is: keys and list positions from the value's root.

t()

A fault: where, and why.

A package's words for a reason, nil for a reason it doesn't word.

Functions

A fault as path: words.

A path as a person reads it: keys joined by ., an index as [i], and the empty path as the root's name.

The general words for a reason every package may answer, nil for any other: a package's own reasons are its own to word, given as words:.

An {:error, reason}'s term as one sentence: each fault of a list as path: words, joined by "; ", one fault the same way, or a bare reason's words. The empty path is named "value" unless root: says otherwise; words: is as format/2's.

Faults ordered by path, keeping the given order of faults at one path.

A type's words as {:type, type} reads them after "must be": base's general types, and the compounds of any type, each part worded by base or by leaf, a package's words for its own leaf types (nil for one it doesn't word). nil when a part has no words, so the whole reason falls to the package.

A reason's words: the caller's words: in turn, then general/1, then the reason shown with inspect/1. Never fails, whatever the reason.

Types

option()

@type option() :: {:root, String.t()} | {:words, words() | [words()]}

format/2's and sentence/2's options.

path()

@type path() :: [atom() | String.t() | non_neg_integer()]

Where a fault is: keys and list positions from the value's root.

t()

@type t() :: {path(), reason :: term()}

A fault: where, and why.

words()

@type words() :: (term() -> String.t() | nil)

A package's words for a reason, nil for a reason it doesn't word.

Functions

format(arg, opts \\ [])

@spec format(t(), [option()]) :: String.t()

A fault as path: words.

Options:

  • root: the name of the empty path, "document" by default;
  • words: a function, or a list of them tried in order, that words a reason or answers nil. A reason none of them words is worded by general/1, else shown with inspect/1.

format_path(path, opts \\ [])

@spec format_path(path(), [option()]) :: String.t()

A path as a person reads it: keys joined by ., an index as [i], and the empty path as the root's name.

iex> Macaw.Base.Fault.format_path([:marks, 0, :channels])
"marks[0].channels"
iex> Macaw.Base.Fault.format_path([])
"document"

A module in a path reads as Elixir writes it, and an element that is no name or index is shown as its term:

iex> Macaw.Base.Fault.format_path([:readers, String])
"readers.String"
iex> Macaw.Base.Fault.format({[[nil], %{}], :unknown_key})
"[nil].%{}: is not a key of this node"

general(arg1)

@spec general(term()) :: String.t() | nil

The general words for a reason every package may answer, nil for any other: a package's own reasons are its own to word, given as words:.

iex> Macaw.Base.Fault.general({:enum, [:asc, :desc]})
"must be one of :asc, :desc"
iex> Macaw.Base.Fault.general({:limit, :bytes, 10, 12})
nil

sentence(reason, opts \\ [])

@spec sentence(term(), [option()]) :: String.t()

An {:error, reason}'s term as one sentence: each fault of a list as path: words, joined by "; ", one fault the same way, or a bare reason's words. The empty path is named "value" unless root: says otherwise; words: is as format/2's.

iex> Macaw.Base.Fault.sentence([{[:temp], :required}, {[:at, 1], {:type, :integer}}])
"temp: is required; at[1]: must be an integer"
iex> Macaw.Base.Fault.sentence(:required)
"is required"

sort(faults)

@spec sort([t()]) :: [t()]

Faults ordered by path, keeping the given order of faults at one path.

type_words(type, leaf \\ fn _type -> nil end)

@spec type_words(term(), (term() -> String.t() | nil)) :: String.t() | nil

A type's words as {:type, type} reads them after "must be": base's general types, and the compounds of any type, each part worded by base or by leaf, a package's words for its own leaf types (nil for one it doesn't word). nil when a part has no words, so the whole reason falls to the package.

iex> Macaw.Base.Fault.type_words({:list, :pos_integer})
"a list of a positive integer"
iex> Macaw.Base.Fault.type_words({:list, :color}, &if(&1 == :color, do: "a color"))
"a list of a color"
iex> Macaw.Base.Fault.type_words({:list, :color})
nil

words(reason, opts \\ [])

@spec words(term(), [option()]) :: String.t()

A reason's words: the caller's words: in turn, then general/1, then the reason shown with inspect/1. Never fails, whatever the reason.