Class: NovaposhtaApi::Middlewares::ErrorHandling

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/novaposhta_api/middlewares/error_handling.rb

Constant Summary collapse

SEPARATOR =
', '

Instance Method Summary collapse

Instance Method Details

#error_message(body) ⇒ Object



14
15
16
# File 'lib/novaposhta_api/middlewares/error_handling.rb', line 14

def error_message(body)
  (from_codes(body['errorCodes']) || body['errors'] || body['message']).join(SEPARATOR)
end

#from_codes(codes = []) ⇒ Object



18
19
20
21
22
# File 'lib/novaposhta_api/middlewares/error_handling.rb', line 18

def from_codes(codes = [])
  return nil if codes.blank?

  codes.map { |code| I18n.t(code, scope: %i[novaposhta errors]) }
end

#on_complete(env) ⇒ Object



8
9
10
11
12
# File 'lib/novaposhta_api/middlewares/error_handling.rb', line 8

def on_complete(env)
  return if env.response_body['success']

  fail(NovaposhtaApi::ResponseError, error_message(env.body))
end