Class: Common::Client::Middleware::Response::RaiseCustomError
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Common::Client::Middleware::Response::RaiseCustomError
- Defined in:
- lib/common/client/middleware/response/raise_custom_error.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#error_prefix ⇒ Object
readonly
Returns the value of attribute error_prefix.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(app, options = {}) ⇒ RaiseCustomError
constructor
A new instance of RaiseCustomError.
- #on_complete(env) ⇒ Object
- #raise_error! ⇒ Object private
- #response_values ⇒ Object private
- #service_i18n_key ⇒ Object private
Constructor Details
permalink #initialize(app, options = {}) ⇒ RaiseCustomError
Returns a new instance of RaiseCustomError.
12 13 14 15 16 |
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 12 def initialize(app, = {}) # set the error prefix to something like 'RX' or 'SM' @error_prefix = [:error_prefix] || 'VA' super(app) end |
Instance Attribute Details
permalink #body ⇒ Object (readonly)
Returns the value of attribute body.
10 11 12 |
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 10 def body @body end |
permalink #error_prefix ⇒ Object (readonly)
Returns the value of attribute error_prefix.
10 11 12 |
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 10 def error_prefix @error_prefix end |
permalink #status ⇒ Object (readonly)
Returns the value of attribute status.
10 11 12 |
# File 'lib/common/client/middleware/response/raise_custom_error.rb', line 10 def status @status end |
Instance Method Details
permalink #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 |
permalink #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 |
permalink #response_values ⇒ Object (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 |
permalink #service_i18n_key ⇒ Object (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 |