Class: VistarClient::Middleware::ErrorHandler

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/vistar_client/middleware/error_handler.rb

Overview

Faraday response middleware that intercepts HTTP responses and raises appropriate VistarClient exceptions based on status codes.

This middleware handles:

  • 401 Unauthorized -> AuthenticationError

  • 4xx/5xx errors -> APIError (with status code and response body)

  • Network/connection failures -> ConnectionError

Examples:

Faraday.new do |f|
  f.response :json
  f.use VistarClient::Middleware::ErrorHandler
  f.adapter Faraday.default_adapter
end

Constant Summary collapse

CLIENT_ERROR_RANGE =

HTTP status codes that should raise exceptions

(400..499)
SERVER_ERROR_RANGE =

HTTP status codes for server errors

(500..599)

Instance Method Summary collapse

Instance Method Details

#call(request_env) ⇒ Faraday::Response

Process the request and handle any errors

Parameters:

  • request_env (Faraday::Env)

    the request environment

Returns:

  • (Faraday::Response)

    the response

Raises:



42
43
44
45
46
47
48
49
50
# File 'lib/vistar_client/middleware/error_handler.rb', line 42

def call(request_env)
  response = @app.call(request_env)
  check_for_errors(response)
  response
rescue Faraday::TimeoutError, Faraday::ConnectionFailed => e
  raise ConnectionError, "Connection failed: #{e.message}"
rescue Faraday::Error => e
  raise ConnectionError, "Network error: #{e.message}"
end