# `CRAM.Error`
[🔗](https://github.com/baoulo/cram-elixir/blob/v0.1.0/lib/cram/error.ex#L1)

Structured CRAM error.

`reason` is one of a fixed set of atoms chosen by this library. It is never
created from bytes in a file. `detail` is a short explanation written by the
library. `data` holds safe integers and flags, not sequence bases.

# `t`

```elixir
@type t() :: %CRAM.Error{
  __exception__: term(),
  data: map(),
  detail: String.t(),
  format: :cram,
  offset: non_neg_integer() | nil,
  path: Path.t() | nil,
  reason: atom()
}
```

A CRAM error.

  * `:format` — always `:cram`
  * `:path` — file path when the source has one, otherwise `nil`
  * `:offset` — byte offset when one is known, otherwise `nil`
  * `:reason` — fixed atom, never derived from file bytes
  * `:detail` — short library text. Empty when there is nothing to add
  * `:data` — small map of integers, names, or checksums. Not sequence bases

    iex> %CRAM.Error{reason: :truncated, detail: "file ended", offset: 4}
    ...> |> Map.get(:reason)
    :truncated

# `message`

Format the error for `Exception.message/1` and `raise/1`.

The text is `cram`, the path and offset when they are set, then the reason
and detail. An empty detail is omitted. Raising `CRAM.Error` uses this text.

    iex> error = CRAM.Error.new(path: "example.cram", offset: 26, reason: :truncated, detail: "header ended")
    iex> Exception.message(error)
    "cram example.cram at offset 26: truncated: header ended"

    iex> raise CRAM.Error, reason: :overflow, detail: "ITF-8 value is outside int32"
    ** (CRAM.Error) cram: overflow: ITF-8 value is outside int32

# `new`

```elixir
@spec new(keyword() | map()) :: t()
```

Build an error struct without raising.

`attrs` is a keyword list or a map of the struct fields. Unknown keys raise
`KeyError`. This function does not raise `CRAM.Error`.

    iex> error = CRAM.Error.new(reason: :truncated, detail: "file ended", offset: 4)
    iex> error.reason
    :truncated

---

*Consult [api-reference.md](api-reference.md) for complete listing*
