Class: EngagingNetworks::Response::RaiseError

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

Instance Method Summary collapse

Instance Method Details

#error_message(response) ⇒ Object



23
24
25
# File 'lib/engaging_networks/response/raise_error.rb', line 23

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

def on_complete(response)
  status_code = response[:status].to_i
  
  # check standard http error responses
  if (400...600).include? status_code
    if status_code == 400
      raise EngagingNetworks::Response::ValidationError.new(url: response[:url].to_s, body: response[:body])
    elsif status_code == 404
      raise EngagingNetworks::Response::NotFound.new(response[:url].to_s)
    else
      raise Exception.new(error_message(response))
    end
  end

  # check for object not found responses, which are status_code == 200
  # TODO
end