Class: Preneeds::Middleware::Response::EoasXmlErrors

Inherits:
Faraday::Response::Middleware
  • Object
show all
Includes:
SentryLogging
Defined in:
lib/preneeds/middleware/response/eoas_xml_errors.rb

Overview

Faraday response middleware that checks the EOAS service response for errors and raises the appropirate exception for our application.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



11
12
13
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 11

def code
  @code
end

#detailObject (readonly)

Returns the value of attribute detail.



11
12
13
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 11

def detail
  @detail
end

#faultObject (readonly)

Returns the value of attribute fault.



11
12
13
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 11

def fault
  @fault
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 11

def status
  @status
end

Instance Method Details

#on_complete(env) ⇒ Faraday::Env

Checks the response for service errors and raises an exception if appropriate

Returns:

  • (Faraday::Env)

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 17

def on_complete(env)
  return unless env.response_headers['content-type']&.match?(/\b(xml)/)

  @fault = fault_string(env)
  @code = return_code(env)

  return unless backend_error?(env) || status_200_error?(env)

  @status = status_200_error?(env) ? return_code(env) : env.status
  @detail = fault || return_description(env)

  # strip percentages from xml because Sentry uses it for interpolation
  extra_context = { original_status: status, original_body: env.body&.delete('%') }
  log_message_to_sentry('Generalized XML error response from EOAS', :warn, extra_context)
  raise Common::Exceptions::BackendServiceException.new('VA900', response_values, @status, env.body)
end