Module: SecApi::CallbackHelper

Included in:
Client, Middleware::Instrumentation
Defined in:
lib/sec_api/callback_helper.rb

Overview

Shared helper methods for callback invocation and error handling.

This module provides consistent callback error logging across all middleware and client code. All callback invocations should use this module’s helpers to ensure consistent structured logging when callbacks fail.

Examples:

Including in a middleware class

class MyMiddleware < Faraday::Middleware
  include SecApi::CallbackHelper

  def call(env)
    invoke_callback_safely("my_callback") do
      @config.my_callback&.call(data: "value")
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#log_callback_error(callback_name, error, config = nil) ⇒ void

This method returns an undefined value.

Logs callback errors to the configured logger with structured JSON format.

Parameters:

  • callback_name (String)

    Name of the callback that failed

  • error (Exception)

    The exception that was raised

  • config (SecApi::Config, nil) (defaults to: nil)

    Config object with logger (optional)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sec_api/callback_helper.rb', line 30

def log_callback_error(callback_name, error, config = nil)
  # Use instance variable @config if config not passed
  cfg = config || (defined?(@config) ? @config : nil) || (defined?(@_config) ? @_config : nil)
  return unless cfg&.logger

  begin
    cfg.logger.error do
      {
        event: "secapi.callback_error",
        callback: callback_name,
        error_class: error.class.name,
        error_message: error.message
      }.to_json
    end
  rescue
    # Don't let logging errors break anything
  end
end