Class: Minds::MiddlewareErrors

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/minds.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

Handles API error responses and provides detailed logging

Parameters:

  • env (Hash)

    The Faraday environment hash

Raises:

  • (Faraday::Error)

    Re-raises the original error after logging



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/minds.rb', line 20

def call(env)
  @app.call(env)
rescue Faraday::Error => e
  raise e unless e.response.is_a?(Hash)

  logger = Logger.new($stdout)
  logger.formatter = proc do |_severity, datetime, _progname, msg|
    timestamp = datetime.strftime("%Y-%m-%d %H:%M:%S.%L")

    error_prefix = "\033[31m[Minds #{VERSION}] #{timestamp} ERROR"
    error_suffix = "\033[0m"

    formatted_message = msg.split("\n").map do |line|
      "#{' ' * 14}#{line}"
    end.join("\n")

    "#{error_prefix} Minds Client Error\n#{formatted_message}#{error_suffix}\n"
  end

  logger.error(e.response[:body])
  raise e
end