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

Inherits:
Faraday::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, #set_sentry_metadata

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

#backend_error?(env) ⇒ Boolean (private)

Returns:

  • (Boolean)


36
37
38
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 36

def backend_error?(env)
  env.status != 200 && fault.present?
end

#fault_string(env) ⇒ Object (private)



44
45
46
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 44

def fault_string(env)
  env.body&.scan(%r{<faultstring[^<>]*>(.*)</faultstring[^<>]*>}i)&.first&.first
end

#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

#response_valuesObject (private)



56
57
58
59
60
61
62
63
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 56

def response_values
  {
    status:,
    detail:,
    code: 'VA900',
    source: 'EOAS provided a general error response, check logs for original request body.'
  }
end

#return_code(env) ⇒ Object (private)



48
49
50
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 48

def return_code(env)
  env.body&.scan(%r{<returnCode[^<>]*>(.*)</returnCode[^<>]*>}i)&.first&.first&.to_i
end

#return_description(env) ⇒ Object (private)



52
53
54
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 52

def return_description(env)
  env.body&.scan(%r{<returnDescription[^<>]*>(.*)</returnDescription[^<>]*>}i)&.first&.first
end

#status_200_error?(env) ⇒ Boolean (private)

Returns:

  • (Boolean)


40
41
42
# File 'lib/preneeds/middleware/response/eoas_xml_errors.rb', line 40

def status_200_error?(env)
  env.status == 200 && code&.nonzero?
end