Class: ActionNetworkRest::Response::RaiseError

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/action_network_rest/response/raise_error.rb

Instance Method Summary collapse

Instance Method Details

#error_message(response) ⇒ Object

rubocop:enable Style/GuardClause



29
30
31
# File 'lib/action_network_rest/response/raise_error.rb', line 29

def error_message(response)
  "#{response[:method].to_s.upcase} #{response[:url]}: #{response[:status]} \n\n #{response[:body]}"
end

#on_complete(response) ⇒ Object

rubocop:disable Style/GuardClause



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/action_network_rest/response/raise_error.rb', line 7

def on_complete(response)
  status_code = response[:status].to_i

  if (400...600).cover? status_code
    if status_code == 400 && response[:body].include?('error')
      error_hsh = JSON.parse(response[:body])
      error_message = error_hsh['error']

      if error_message == 'You must specify a valid person id'
        raise MustSpecifyValidPersonId, error_message(response)
      end
    elsif status_code == 404
      raise NotFoundError, error_message(response)
    elsif status_code == 429
      raise TooManyRequests, error_message(response)
    else
      raise ResponseError, error_message(response)
    end
  end
end