Class: ActionKitRest::Response::RaiseError

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

Instance Method Summary collapse

Instance Method Details

#error_message(response) ⇒ Object



35
36
37
# File 'lib/action_kit_rest/response/raise_error.rb', line 35

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

#on_complete(response) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/action_kit_rest/response/raise_error.rb', line 6

def on_complete(response)
  status_code = response[:status].to_i
  if (400...600).include? status_code
    if status_code == 400
      response_body = response[:body]
      if ActionKitRest::Response::InvalidAkidError.matches?(JSON.parse(response_body)['errors'])
        raise ActionKitRest::Response::InvalidAkidError.new(url: response[:url].to_s, body: response_body)
      else
        raise ActionKitRest::Response::ValidationError.new(url: response[:url].to_s, body: response_body)
      end
    elsif status_code == 404
      raise ActionKitRest::Response::NotFound, response[:url].to_s
    elsif status_code == 401
      raise ActionKitRest::Response::Unauthorized, response[:url].to_s
    elsif status_code == 500 && response[:body] =~ /"error"/
      error_hsh = JSON.parse(response[:body])
      error_message = error_hsh['error']

      if error_message == 'Sorry, this request could not be processed. Please try again later.'
        raise ActionKitRest::Response::TryAgainLater, error_message(response)
      else
        raise ActionKitRest::Response::UnknownServerError, error_message(response)
      end
    else
      raise ActionKitRest::Response::UnknownServerError, error_message(response)
    end
  end
end