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



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

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

#on_complete(response) ⇒ Object



5
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
# File 'lib/action_kit_rest/response/raise_error.rb', line 5

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 JSON.parse(response_body)['errors'] == {'mailing_id' => ['Unable to associate this mailing ID with account.']}
        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.new(response[:url].to_s)
    elsif status_code == 401
      raise ActionKitRest::Response::Unauthorized.new(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.new(error_message(response))
      else
        raise StandardError.new(error_message(response))
      end
    else
      raise StandardError.new(error_message(response))
    end
  end
end