Exception: Common::Exceptions::BackendServiceException

Inherits:
BaseError
  • Object
show all
Defined in:
lib/common/exceptions/backend_service_exception.rb

Overview

This will return a generic error, to customize you must define the minor code in the locales file and call this class from raise_error middleware.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseError

#i18n_data, #i18n_field, #i18n_interpolated, #log_to_sentry?, #sentry_type, #status_code

Constructor Details

#initialize(key = nil, response_values = {}, original_status = nil, original_body = nil) ⇒ BackendServiceException

rubocop:disable Metrics/ParameterLists



15
16
17
18
19
20
21
# File 'lib/common/exceptions/backend_service_exception.rb', line 15

def initialize(key = nil, response_values = {}, original_status = nil, original_body = nil)
  @response_values = response_values
  @key = key || 'VA900'
  @original_status = original_status
  @original_body = original_body
  validate_arguments!
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



12
13
14
# File 'lib/common/exceptions/backend_service_exception.rb', line 12

def key
  @key
end

#original_bodyObject (readonly)

Returns the value of attribute original_body.



12
13
14
# File 'lib/common/exceptions/backend_service_exception.rb', line 12

def original_body
  @original_body
end

#original_statusObject (readonly)

Returns the value of attribute original_status.



12
13
14
# File 'lib/common/exceptions/backend_service_exception.rb', line 12

def original_status
  @original_status
end

#response_valuesObject (readonly)

Returns the value of attribute response_values.



12
13
14
# File 'lib/common/exceptions/backend_service_exception.rb', line 12

def response_values
  @response_values
end

Instance Method Details

#codeObject (private)

REQUIRED - This is the i18n code returned from raise_error middleware. If it exists in I18n then it should be like RX139 or EVSS144, otherwise VA900



64
65
66
67
68
69
70
# File 'lib/common/exceptions/backend_service_exception.rb', line 64

def code
  if @key.present? && I18n.exists?("common.exceptions.#{@key}")
    @key
  else
    'VA900'
  end
end

#detailObject (private)

OPTIONAL - This is the detail or message that is rendered in JSON response Not providing detail will render a detail the same as title, ‘Operation failed’ NOTE: in the future, detail will only work via i18n, not the value from response_values



84
85
86
# File 'lib/common/exceptions/backend_service_exception.rb', line 84

def detail
  i18n_data[:detail].presence || response_values[:detail]
end

#errorsObject



30
31
32
# File 'lib/common/exceptions/backend_service_exception.rb', line 30

def errors
  Array(SerializableError.new(i18n_data.merge(render_overides)))
end

#i18n_keyObject (private)



99
100
101
# File 'lib/common/exceptions/backend_service_exception.rb', line 99

def i18n_key
  "common.exceptions.#{code}"
end

#messageObject

The message will be the actual backend service response from middleware, not the I18n version.



26
27
28
# File 'lib/common/exceptions/backend_service_exception.rb', line 26

def message
  "BackendServiceException: #{response_values.merge(code:)}"
end

#render_overidesObject (private)



58
59
60
# File 'lib/common/exceptions/backend_service_exception.rb', line 58

def render_overides
  { status:, detail:, code:, source: }
end

#sourceObject (private)

OPTIONAL - This should usually be a developer message of some sort from the backend service if one is not provided by the backend this can be nil and the key will not be rendered



90
91
92
# File 'lib/common/exceptions/backend_service_exception.rb', line 90

def source
  response_values[:source]
end

#statusObject (private)

REQUIRED - This is the http status code. unless you’ve specified that you want the status code to be something other then 400 explicitly it will default to 400. IT WILL NOT DEFAULT to whatever was provided by the backend service, because the backend service response might not always be relevant



77
78
79
# File 'lib/common/exceptions/backend_service_exception.rb', line 77

def status
  i18n_data[:status].presence || 400
end

#va900?Boolean Also known as: generic_error?

VA900 is characterized as a generic type of exception. See exceptions.en.yml for what JSON will render

Returns:

  • (Boolean)


35
36
37
# File 'lib/common/exceptions/backend_service_exception.rb', line 35

def va900?
  code == 'VA900'
end

#va900_hintObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/common/exceptions/backend_service_exception.rb', line 45

def va900_hint
  <<-MESSAGE.strip_heredoc
    Add the following to exceptions.en.yml
    #{response_values[:code]}:
      code: '#{response_values[:code]}'
      detail: '#{response_values[:detail]}'
      status: <http status code you want rendered (400, 422, etc)>
      source: ~
  MESSAGE
end

#va900_warningObject



41
42
43
# File 'lib/common/exceptions/backend_service_exception.rb', line 41

def va900_warning
  "Unmapped VA900 (Backend Response: { status: #{original_status}, message: #{original_body}) }"
end

#validate_arguments!Object (private)

Raises:

  • (ArgumentError)


94
95
96
97
# File 'lib/common/exceptions/backend_service_exception.rb', line 94

def validate_arguments!
  raise ArgumentError, "i18n key (#{@key}) is invalid" unless I18n.exists?(i18n_key)
  raise ArgumentError, "status (#{status}) is not in range" unless status.between?(400, 599)
end