Phoenix.SessionProcess.Error (phoenix_session_process v1.0.1)

Copy Markdown View Source

Comprehensive error handling for Phoenix.SessionProcess operations.

This module provides structured error types, helper functions for creating errors, and utilities for converting errors to human-readable messages. All public functions in the Phoenix.SessionProcess library return standardized error tuples that can be handled using this module.

Error Types

The library returns errors in the format {:error, error_details} where error_details follows these patterns:

Session Management Errors

  • {:invalid_session_id, session_id} - Session ID format is invalid
  • {:session_limit_reached, max_sessions} - Maximum concurrent sessions exceeded
  • {:session_not_found, session_id} - Session doesn't exist or has expired

Process Communication Errors

  • {:process_not_found, session_id} - Session process not found in registry
  • {:timeout, timeout} - Operation timed out
  • {:call_failed, {module, function, args, reason}} - GenServer call failed
  • {:cast_failed, {module, function, args, reason}} - GenServer cast failed

Usage Examples

Handling Errors

Use pattern matching to handle different error types returned by Phoenix.SessionProcess functions:

case Phoenix.SessionProcess.start("session_123") do
  {:ok, _pid} ->
    # Success case

  {:error, {:invalid_session_id, _id}} ->
    # Handle invalid session ID
    {:error, :invalid_session}

  {:error, {:session_limit_reached, _max}} ->
    # Handle session limit exceeded
    {:error, :too_many_sessions}

  {:error, _reason} ->
    # Handle other errors
    {:error, :session_failed}
end

Creating Custom Errors

Use the helper functions to create standardized errors:

Phoenix.SessionProcess.Error.invalid_session_id("invalid@id")
Phoenix.SessionProcess.Error.session_limit_reached(1000)

Converting to Human-Readable Messages

Use the message/1 function to convert errors to human-readable strings:

error = Phoenix.SessionProcess.Error.invalid_session_id("invalid@id")
Phoenix.SessionProcess.Error.message(error)
# Returns: "Invalid session ID format: "invalid@id""

Error Recovery Strategies

Session Expiration

When {:session_not_found, session_id} is encountered, it typically means the session has expired due to TTL. The appropriate response is to redirect the user to login or create a new session.

Rate Limiting

When {:session_limit_reached, max_sessions} occurs, implement backoff strategies and consider increasing the :max_sessions configuration if appropriate.

Timeout Handling

Timeout errors may indicate system overload. Consider implementing retry logic with exponential backoff for transient failures.

Summary

Types

t()

Custom error types returned by Phoenix.SessionProcess functions.

Functions

Creates a call failed error.

Creates a cast failed error.

Creates an invalid session ID error.

Returns a human-readable error message for the given error.

Converts a generic error to a SessionProcess error when possible.

Creates a process not found error.

Creates a rate limit exceeded error.

Creates a session limit reached error.

Creates a session not found error.

Creates a timeout error.

Types

t()

@type t() ::
  {:error,
   {:invalid_session_id, String.t()}
   | {:session_limit_reached, non_neg_integer()}
   | {:rate_limit_exceeded, non_neg_integer()}
   | {:session_not_found, String.t()}
   | {:process_not_found, String.t()}
   | {:timeout, timeout()}
   | {:call_failed, {module(), atom(), any(), any()}}
   | {:cast_failed, {module(), atom(), any(), any()}}}

Custom error types returned by Phoenix.SessionProcess functions.

Functions

call_failed(module, function, args, reason)

@spec call_failed(module(), atom(), any(), any()) :: {:error, {:call_failed, tuple()}}

Creates a call failed error.

cast_failed(module, function, args, reason)

@spec cast_failed(module(), atom(), any(), any()) :: {:error, {:cast_failed, tuple()}}

Creates a cast failed error.

invalid_session_id(session_id)

@spec invalid_session_id(String.t()) :: {:error, {:invalid_session_id, String.t()}}

Creates an invalid session ID error.

Examples

iex> Phoenix.SessionProcess.Error.invalid_session_id("invalid@id")
{:error, {:invalid_session_id, "invalid@id"}}

message(arg)

@spec message(t()) :: String.t()

Returns a human-readable error message for the given error.

normalize_error(error)

@spec normalize_error(any()) :: {:error, any()}

Converts a generic error to a SessionProcess error when possible.

process_not_found(session_id)

@spec process_not_found(String.t()) :: {:error, {:process_not_found, String.t()}}

Creates a process not found error.

rate_limit_exceeded(rate_limit)

@spec rate_limit_exceeded(non_neg_integer()) ::
  {:error, {:rate_limit_exceeded, non_neg_integer()}}

Creates a rate limit exceeded error.

session_limit_reached(max_sessions)

@spec session_limit_reached(non_neg_integer()) ::
  {:error, {:session_limit_reached, non_neg_integer()}}

Creates a session limit reached error.

session_not_found(session_id)

@spec session_not_found(String.t()) :: {:error, {:session_not_found, String.t()}}

Creates a session not found error.

timeout(timeout)

@spec timeout(timeout()) :: {:error, {:timeout, timeout()}}

Creates a timeout error.