# `Spectral.Error`
[🔗](https://github.com/andreashasse/spectral/blob/main/lib/spectral/error.ex#L11)

Exception for Spectral operations.

This exception represents errors returned from the underlying `:spectra` library,
converted to an idiomatic Elixir format.

Can be used both in `{:error, [%Spectral.Error{}]}` tuples and raised as an exception.

## Fields

- `:location` - Path to where the error occurred (list of strings or atoms)
- `:type` - Type of error (`:decode_error`, `:type_mismatch`, `:no_match`, `:missing_data`, `:not_matched_fields`)
- `:context` - Additional context information about the error (runtime-determined type)
- `:message` - Human-readable error message (auto-generated for exceptions)

## Inspecting errors

`context.value` holds the input that failed, which may contain personal data.
`inspect/2` (and so `Logger` and IEx) shows it as `**redacted**`, also inside
the nested errors of a `:no_match` union error. The field itself is unchanged,
so code can still read `error.context.value`. `Exception.message/1` does not
include `context.value`.

Other parts of an error can still carry input data:

- `location` holds the map keys on the path to the failure, so a map keyed
  by email addresses gives a message like `"type_mismatch at alice@example.com"`.
- A `:decode_error` context keeps a short piece of the input bytes from the
  JSON parser.
- Redaction happens only in the Elixir `Inspect` protocol.
  `inspect(error, structs: false)` and Erlang formatting such as `~p` print
  the value.

## Example

    %Spectral.Error{
      location: ["user", "age"],
      type: :type_mismatch,
      context: %{expected: :integer, got: "not a number"}
    }

# `error_type`

```elixir
@type error_type() ::
  :decode_error
  | :type_mismatch
  | :no_match
  | :missing_data
  | :not_matched_fields
```

# `t`

```elixir
@type t() :: %Spectral.Error{
  __exception__: true,
  context: dynamic(),
  location: [String.t() | atom()],
  message: String.t() | nil,
  type: error_type()
}
```

---

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