WebsockexNova.Defaults.DefaultErrorHandler (WebsockexNova v0.1.1)

View Source

Default implementation of the ErrorHandler behavior.

This module provides sensible default implementations for all ErrorHandler callbacks, including:

  • Error classification by type
  • Reconnection logic with exponential backoff
  • Standardized error logging
  • State tracking of errors and reconnection attempts (single source of truth)

Usage

You can use this module directly or as a starting point for your own implementation:

defmodule MyApp.CustomErrorHandler do
  use WebsockexNova.Defaults.DefaultErrorHandler

  # Override specific callbacks as needed
  def should_reconnect?(error, attempt, state) do
    # Custom reconnection logic
    {attempt < 10, attempt * 1000}
  end
end

Configuration

The default handler supports configuration via the :reconnection key in the state (preferred), or legacy keys for backward compatibility:

  • :reconnection - map or keyword list with keys:
    • :max_attempts or :max_reconnect_attempts (default: 5)
    • :base_delay or :initial_delay (default: 1000)
    • :max_delay (default: 30000)
    • :strategy (currently only exponential supported)
  • Legacy keys (for backward compatibility):
    • :max_reconnect_attempts, :base_delay, :max_delay

Reconnection Attempt Tracking

This handler tracks the reconnection attempt count in the adapter_state under the :reconnect_attempts key. Use increment_reconnect_attempts/1 and reset_reconnect_attempts/1 to update this count.

Summary

Functions

Get the reconnection attempt count from the adapter_state.

Increment the reconnection attempt count in the adapter_state.

Reset the reconnection attempt count in the adapter_state.

Functions

error_init(opts)

get_reconnect_attempts(client_conn)

Get the reconnection attempt count from the adapter_state.

increment_reconnect_attempts(conn)

Increment the reconnection attempt count in the adapter_state.

reset_reconnect_attempts(conn)

Reset the reconnection attempt count in the adapter_state.