SelectoComponents.Form.ErrorHandling (selecto_components v0.4.5)

Error handling utilities for SelectoComponents form operations.

This module provides a consistent error handling wrapper for form event handlers, capturing and categorizing errors to provide meaningful feedback to users while preventing application crashes.

features

Features

  • Wraps operations in try/rescue blocks
  • Categorizes errors for better user feedback
  • Supports both development and production error modes
  • Maintains error history (up to 5 most recent)
  • Integrates with ErrorCategorizer for detailed error analysis

usage

Usage

defmodule MyLive do
  use SelectoComponents.Form
  import SelectoComponents.Form.ErrorHandling

  def handle_event("some_operation", params, socket) do
    with_error_handling(socket, "some_operation", fn ->
      # Your operation logic here
      {:noreply, updated_socket}
    end)
  end
end

Link to this section Summary

Functions

Handles a component error by categorizing it and adding it to the socket's error list.

Wraps an operation in comprehensive error handling.

Link to this section Functions

Link to this function

handle_component_error(socket, error, operation_name, error_type)

Handles a component error by categorizing it and adding it to the socket's error list.

This function is typically called internally by with_error_handling/3, but can be used directly if you need custom error handling logic.

parameters

Parameters

  • socket - The LiveView socket
  • error - The error/exception that occurred
  • operation_name - A string identifying the operation
  • error_type - An atom categorizing the error type

returns

Returns

A {:noreply, socket} tuple with the error added to component_errors.

Link to this function

with_error_handling(socket, operation_name, fun)

Wraps an operation in comprehensive error handling.

Catches all exceptions and errors, categorizes them, and adds them to the socket's component_errors list for display to the user.

parameters

Parameters

  • socket - The LiveView socket
  • operation_name - A string identifying the operation (for debugging/logging)
  • fun - A zero-arity function containing the operation to execute

returns

Returns

The result of the function if successful, or a {:noreply, socket} tuple with error information added to the socket assigns.

examples

Examples

with_error_handling(socket, "view-apply", fn ->
  socket = process_params(params, socket)
  {:noreply, socket}
end)