ExCodecs.Error exception (ex_codecs v0.1.0)

Copy Markdown View Source

Standardized error types for ExCodecs.

All ExCodecs functions return {:ok, result} or {:error, reason} tuples. The reason will be one of the atoms defined in this module, or a structured error when additional context is needed.

Summary

Functions

Creates an {:error, ExCodecs.Error.t()} tuple.

Checks if an error matches a specific reason.

Creates a new error struct.

Types

error_reason()

@type error_reason() ::
  :unsupported_codec
  | :codec_unavailable
  | :invalid_data
  | :invalid_options
  | :compression_failed
  | :decompression_failed
  | :nif_not_loaded

t()

@type t() :: %ExCodecs.Error{
  __exception__: true,
  codec: atom() | nil,
  details: term() | nil,
  message: String.t(),
  reason: error_reason()
}

Functions

error(reason, opts \\ [])

@spec error(
  error_reason(),
  keyword()
) :: {:error, t()}

Creates an {:error, ExCodecs.Error.t()} tuple.

iex> {:error, error} = ExCodecs.Error.error(:unsupported_codec)
iex> error.reason
:unsupported_codec

matches?(arg1, reason)

@spec matches?(
  {:error, t()},
  error_reason()
) :: boolean()

Checks if an error matches a specific reason.

iex> {:error, error} = ExCodecs.Error.error(:unsupported_codec)
iex> ExCodecs.Error.matches?({:error, error}, :unsupported_codec)
true
iex> ExCodecs.Error.matches?({:error, error}, :invalid_data)
false

new(reason, opts \\ [])

@spec new(
  error_reason(),
  keyword()
) :: t()

Creates a new error struct.

iex> error = ExCodecs.Error.new(:unsupported_codec)
iex> error.reason
:unsupported_codec
iex> error.message
"The specified codec is not supported"
iex> error.codec
nil

iex> error = ExCodecs.Error.new(:invalid_options, codec: :zstd)
iex> error.codec
:zstd