Class: Common::Client::Middleware::Response::RaiseCustomError

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

Direct Known Subclasses

Rx::Middleware::Response::RxRaiseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RaiseCustomError

Returns a new instance of RaiseCustomError.

[View source] [View on GitHub]

12
13
14
15
16
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 12

def initialize(app, options = {})
  # set the error prefix to something like 'RX' or 'SM'
  @error_prefix = options[:error_prefix] || 'VA'
  super(app)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.

[View on GitHub]

10
11
12
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 10

def body
  @body
end

#error_prefixObject (readonly)

Returns the value of attribute error_prefix.

[View on GitHub]

10
11
12
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 10

def error_prefix
  @error_prefix
end

#statusObject (readonly)

Returns the value of attribute status.

[View on GitHub]

10
11
12
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 10

def status
  @status
end

Instance Method Details

#on_complete(env) ⇒ Object

[View source] [View on GitHub]

18
19
20
21
22
23
24
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 18

def on_complete(env)
  return if env.success?

  @body = env[:body]
  @status = env.status.to_i
  raise_error!
end

#raise_error!Object (private)

[View source] [View on GitHub]

28
29
30
31
32
33
34
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 28

def raise_error!
  if status&.between?(400, 599)
    raise Common::Exceptions::BackendServiceException.new(service_i18n_key, response_values, status, body)
  else
    raise BackendUnhandledException, "Unhandled Exception - status: #{status}, body: #{body}"
  end
end

#response_valuesObject (private)

[View source] [View on GitHub]

44
45
46
47
48
49
50
51
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 44

def response_values
  {
    status:,
    detail: body['detail'],
    code: service_i18n_key,
    source: body['source']
  }
end

#service_i18n_keyObject (private)

[View source] [View on GitHub]

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

def service_i18n_key
  if body['code']
    "#{error_prefix.upcase}#{body['code']}"
  else
    "#{error_prefix.upcase}_#{status}"
  end
end