Class: Ksef::HTTP::ErrorHandler

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/ksef/http/error_handler.rb

Overview

Maps KSeF error responses and Faraday transport failures onto the Error hierarchy (DESIGN.md §6.7).

Status coverage follows what the contract actually declares — 400, 401, 403, 410 and 429 (docs/REFERENCE.md §5.4). 5xx is handled defensively: the spec never declares it, so the body shape is unknown and degrades to a raw payload.

Constant Summary collapse

STATUS_MAP =
{
  400 => ApiError,
  401 => AuthenticationError,
  403 => AuthorizationError,
  410 => ResourceGoneError,
  429 => RateLimitedError
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ksef/http/error_handler.rb', line 22

def call(env)
  super
rescue Faraday::TimeoutError => e
  raise Ksef::TimeoutError, "KSeF request timed out: #{e.message}"
rescue Faraday::SSLError => e
  raise Ksef::ConnectionError, "TLS failure talking to KSeF: #{e.message}"
rescue Faraday::ConnectionFailed => e
  # The net_http adapter reports `Net::OpenTimeout` as a connection failure, which
  # loses the distinction a caller most needs after submitting an invoice: a
  # timeout may have been processed server-side, a refused connection was not.
  raise Ksef::TimeoutError, "KSeF connection timed out: #{e.message}" if timeout?(e.wrapped_exception)

  raise Ksef::ConnectionError, "Could not connect to KSeF: #{e.message}"
end

#on_complete(env) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ksef/http/error_handler.rb', line 37

def on_complete(env)
  status = env.status
  return if status < 400

  problem = ProblemDetails.parse(status: status, body: env.body)
  raise build_error(status, problem, env)
end