# `Tink.Error`
[🔗](https://github.com/iamkanishka/tink.ex/blob/v0.1.1/lib/tink/error.ex#L1)

Error struct and handling for Tink.

All API errors are wrapped in this struct for consistent error handling.

## Error Types

- `:api_error` - Tink API returned an error response
- `:network_error` - Network/connection error
- `:timeout` - Request timed out
- `:authentication_error` - Auth failure
- `:rate_limit_error` - Rate limit exceeded
- `:validation_error` - Invalid parameters
- `:decode_error` - Failed to decode response
- `:market_mismatch` - Provider not available in requested market
- `:unknown` - Unknown error

## Examples

    case Tink.Accounts.list(client) do
      {:ok, accounts} ->
        # Success

      {:error, %Tink.Error{type: :rate_limit_error}} ->
        # Handle rate limit

      {:error, %Tink.Error{status: 401}} ->
        # Handle unauthorized

      {:error, error} ->
        IO.inspect(error)
    end

# `error_type`

```elixir
@type error_type() ::
  :api_error
  | :network_error
  | :timeout
  | :authentication_error
  | :rate_limit_error
  | :validation_error
  | :decode_error
  | :market_mismatch
  | :unknown
```

# `t`

```elixir
@type t() :: %Tink.Error{
  error_code: String.t() | nil,
  error_details: map() | nil,
  message: String.t(),
  original_error: term() | nil,
  request_id: String.t() | nil,
  status: integer() | nil,
  type: error_type()
}
```

# `format`

```elixir
@spec format(t()) :: String.t()
```

Returns a human-readable error message.

## Examples

    iex> error = Tink.Error.from_response(429, %{"errorMessage" => "Rate limit exceeded"})
    iex> Tink.Error.format(error)
    "[429] Rate limit exceeded (RATE_LIMIT_ERROR)"

# `from_http_error`

```elixir
@spec from_http_error(map() | term()) :: t()
```

Creates an error from an HTTP client error.

## Examples

    iex> Tink.Error.from_http_error(%{type: :timeout, reason: "Request timed out"})
    %Tink.Error{type: :timeout, message: "Request timed out"}

# `from_response`

```elixir
@spec from_response(integer(), map() | String.t()) :: t()
```

Creates an error from an HTTP response.

## Examples

    iex> Tink.Error.from_response(400, %{"errorCode" => "INVALID_REQUEST"})
    %Tink.Error{type: :api_error, status: 400, error_code: "INVALID_REQUEST"}

# `new`

```elixir
@spec new(keyword()) :: t()
```

Creates a new error struct.

## Examples

    iex> Tink.Error.new(type: :network_error, message: "Connection failed")
    %Tink.Error{type: :network_error, message: "Connection failed"}

# `retryable?`

```elixir
@spec retryable?(t()) :: boolean()
```

Checks if an error is retryable.

## Examples

    iex> error = %Tink.Error{type: :network_error, message: ""}
    iex> Tink.Error.retryable?(error)
    true

    iex> error = %Tink.Error{type: :validation_error, message: ""}
    iex> Tink.Error.retryable?(error)
    false

---

*Consult [api-reference.md](api-reference.md) for complete listing*
