Exspotify.Error (Exspotify v0.1.0)

View Source

Error types and utilities for the Exspotify library.

Provides structured error handling with specific error types and helpful context.

Summary

Functions

Creates a configuration error with helpful suggestions.

Converts HTTP status codes and responses to structured errors.

Creates a JSON decode error.

Creates a network error.

Creates a new error struct with the given type and message.

Validates that an ID is present and non-empty.

Validates that a list is present and non-empty.

Validates that a token is present and non-empty.

Types

error_reason()

@type error_reason() ::
  :invalid_id
  | :empty_id
  | :invalid_token
  | :empty_token
  | :invalid_type
  | :empty_list
  | :configuration_error
  | :unauthorized
  | :forbidden
  | :not_found
  | :rate_limited
  | :server_error
  | :bad_request
  | :service_unavailable
  | :network_error
  | :timeout
  | :json_decode_error
  | :unexpected_response

t()

@type t() :: %Exspotify.Error{
  details: map() | nil,
  message: String.t(),
  status: integer() | nil,
  suggestion: String.t() | nil,
  type: error_reason()
}

Functions

configuration_error(message, suggestion \\ nil)

@spec configuration_error(String.t(), String.t() | nil) :: t()

Creates a configuration error with helpful suggestions.

from_http_response(status, body)

@spec from_http_response(integer(), any()) :: t()

Converts HTTP status codes and responses to structured errors.

json_decode_error(data)

@spec json_decode_error(any()) :: t()

Creates a JSON decode error.

network_error(reason)

@spec network_error(any()) :: t()

Creates a network error.

new(type, message, details \\ nil, status \\ nil, suggestion \\ nil)

@spec new(error_reason(), String.t(), map() | nil, integer() | nil, String.t() | nil) ::
  t()

Creates a new error struct with the given type and message.

validate_id(id, field_name)

@spec validate_id(String.t() | nil, String.t()) :: :ok | {:error, t()}

Validates that an ID is present and non-empty.

validate_list(list, field_name)

@spec validate_list([any()], String.t()) :: :ok | {:error, t()}

Validates that a list is present and non-empty.

validate_token(token)

@spec validate_token(String.t() | nil) :: :ok | {:error, t()}

Validates that a token is present and non-empty.