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:
locationholds 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_errorcontext keeps a short piece of the input bytes from the JSON parser. - Redaction happens only in the Elixir
Inspectprotocol.inspect(error, structs: false)and Erlang formatting such as~pprint the value.
Example
%Spectral.Error{
location: ["user", "age"],
type: :type_mismatch,
context: %{expected: :integer, got: "not a number"}
}
Summary
Types
@type error_type() ::
:decode_error
| :type_mismatch
| :no_match
| :missing_data
| :not_matched_fields
@type t() :: %Spectral.Error{ __exception__: true, context: dynamic(), location: [String.t() | atom()], message: String.t() | nil, type: error_type() }