# `AshVault.Errors.SerializationFailed`
[🔗](https://github.com/jakeschurch/ash_vault/blob/main/lib/ash_vault/errors/serialization_failed.ex#L1)

Raised when a value could not be dumped to (or cast back from) its embedded
representation while being encrypted or decrypted.

Lives in its own file rather than in `AshVault.Errors` because the crypto core owns
that module; this error belongs to the Ash extension layer.

## The `:reason` field is redacted

`:reason` is the raw return of `Ash.Type.dump_to_embedded/3` or
`Ash.Type.cast_from_embedded/3`, tagged with which callback produced it. That return is
**type-controlled**: every built-in Ash type returns a bare `:error` or
`{:error, index: 0}`, but the `Ash.Type` contract permits
`{:error, message: "...", value: value}` — and a custom type that takes that option puts
the rejected value, which for AshVault is plaintext, into an `Ash.Error` that gets
logged, rendered and shipped to APM.

So `exception/1` rewrites `:reason` before it is ever stored on the struct — not in
`message/1`, because `inspect(error)` and Ash's own error aggregation read the struct
directly. The rewrite is an **allowlist**: the tuple/list/map *shape* is preserved,
keyword and map *keys* are preserved verbatim, and every leaf value survives only if it
is an atom or an integer. Anything else — binaries, floats, structs, nested data — is
replaced by `{:redacted, kind}`, which records what was dropped without recording it.

That keeps the whole diagnostic the task of debugging a custom type actually needs:

    {:dump_to_embedded, :error}
    {:dump_to_embedded, {:error, [index: 0]}}
    {:cast_from_embedded, {:error, [field: :email, message: {:redacted, :binary}]}}

The one deliberate loss is `:message`. A custom type is free to write
`{:error, message: "bad SSN: #{value}"}`, so a message is as untrustworthy as a
`:value`, and truncating it would still leak a prefix — which, for a short secret, is
the whole secret. The error already carries `:resource`, `:field` and `:type`, so the
operator knows exactly which type module to look at; read its message there, against a
value you are allowed to see.

# `exception`

```elixir
@spec exception(opts :: Keyword.t()) :: %AshVault.Errors.SerializationFailed{
  __exception__: true,
  bread_crumbs: term(),
  class: term(),
  field: term(),
  path: term(),
  reason: term(),
  resource: term(),
  splode: term(),
  stacktrace: term(),
  type: term(),
  vars: term()
}
```

Build the error, redacting `:reason` so no type-controlled value is retained.

See the moduledoc for exactly what survives.

# `redact_reason`

```elixir
@spec redact_reason(term()) :: term()
```

Redact a raw `Ash.Type` callback return down to its shape.

Exposed so the sanitizer can be tested directly, and so anywhere else that wants to
put a type-controlled term in an operator-facing place can reuse the same rule.

---

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