Class: Common::Client::Middleware::Response::MHVXmlHtmlErrors

Inherits:
Faraday::Middleware
  • Object
show all
Includes:
SentryLogging
Defined in:
lib/common/client/middleware/response/mhv_xml_html_errors.rb

Overview

Faraday response middleware that checks the MHV service XML/HTML response for errors and raises the appropriate 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

#statusObject (readonly)

Returns the value of attribute status.



15
16
17
# File 'lib/common/client/middleware/response/mhv_xml_html_errors.rb', line 15

def status
  @status
end

Instance Method Details

#on_complete(env) ⇒ Faraday::Env

Returns:

  • (Faraday::Env)

Raises:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/common/client/middleware/response/mhv_xml_html_errors.rb', line 21

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

  @status = env.status.to_i
  @body = env.body.delete('%') # strip percentages from html because Sentry uses it for interpolation

  extra_context = { original_status: @status, original_body: @body }
  log_message_to_sentry('Could not parse XML/HTML response from MHV', :warn, extra_context)
  raise Common::Exceptions::BackendServiceException.new('VA900', response_values, @status, @body)
end

#response_valuesObject (private)



35
36
37
38
39
40
41
42
# File 'lib/common/client/middleware/response/mhv_xml_html_errors.rb', line 35

def response_values
  {
    status:,
    detail: 'Received an error response that could not be processed',
    code: 'VA900',
    source: 'MHV provided unparsable error response, check logs for original request body.'
  }
end