Class: EVSS::ErrorMiddleware

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

Defined Under Namespace

Classes: EVSSBackendServiceError, EVSSError

Instance Method Summary collapse

Instance Method Details

#handle_xml_body(env) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/evss/error_middleware.rb', line 19

def handle_xml_body(env)
  resp = Hash.from_xml(env.body)
  inner_resp = resp[resp.keys[0]]
  if %w[fatal error].include?(inner_resp&.dig('messages', 'severity')&.downcase)
    raise EVSSError.new(inner_resp['messages']['text'], inner_resp['messages']['text'])
  end
end

#on_complete(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/evss/error_middleware.rb', line 27

def on_complete(env)
  status = env[:status]
  resp = env.body
  Sentry.set_extras(body: resp)
  case status
  when 200
    if env.response_headers['content-type'].downcase.include?('xml')
      handle_xml_body(env)
    elsif resp['success'] == false || resp['messages']&.find { |m| m['severity'] =~ /fatal|error/i }
      raise EVSSError.new(resp['messages'], resp['messages'], resp)
    end
  when 503, 504
    raise EVSSBackendServiceError.new("EVSS#{status}", { status: }, status, resp)
  end
end