Spectral.Error exception (Spectral v0.4.0)

View Source

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)

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

error_type()

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

t()

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

Functions

from_erlang(arg)

Converts an Erlang error record from :spectra to a Spectral.Error struct.

Parameters

  • erlang_error - An error record from the :spectra library

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
}

from_erlang_list(erlang_errors)

Converts a list of Erlang error records to Elixir structs.

Parameters

  • erlang_errors - A list of error records from the :spectra library

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}]