Spectral.Error exception (Spectral v0.4.0)
View SourceException 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)
Example
%Spectral.Error{
location: ["user", "age"],
type: :type_mismatch,
context: %{expected: :integer, got: "not a number"}
}
Summary
Functions
Converts an Erlang error record from :spectra to a Spectral.Error struct.
Converts a list of Erlang error records to Elixir structs.
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(), type: error_type() }
Functions
Converts an Erlang error record from :spectra to a Spectral.Error struct.
Parameters
erlang_error- An error record from the:spectralibrary
Returns
%Spectral.Error{}- The error as an Elixir struct
Example
iex> erlang_error = {:sp_error, ["user", "age"], :type_mismatch, %{expected: :integer}}
iex> Spectral.Error.from_erlang(erlang_error)
%Spectral.Error{
location: ["user", "age"],
type: :type_mismatch,
context: %{expected: :integer},
message: nil
}
Converts a list of Erlang error records to Elixir structs.
Parameters
erlang_errors- A list of error records from the:spectralibrary
Returns
[%Spectral.Error{}]- List of errors as Elixir structs
Example
iex> errors = [{:sp_error, [], :decode_error, %{reason: "invalid JSON"}}]
iex> Spectral.Error.from_erlang_list(errors)
[%Spectral.Error{location: [], type: :decode_error, context: %{reason: "invalid JSON"}, message: nil}]