Class: Response::RaiseTumblRbError

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/response/raise_tumbl_rb_error.rb

Instance Method Summary collapse

Instance Method Details

#error_message(response) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/faraday/response/raise_tumbl_rb_error.rb', line 16

def error_message(response)
  message = if (body = response[:body]) && !body.empty?
    if body.is_a?(String)
      body = MultiJson.load(body, :symbolize_keys => true)
    end
    ": #{body[:error] || body[:message] || ''}"
  else
    ''
  end
  "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{message}"
end

#on_complete(response) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/faraday/response/raise_tumbl_rb_error.rb', line 7

def on_complete(response)
  case response[:status].to_i
  when 401
    raise TumblRb::Unauthorized, error_message(response)
  when 404
    raise TumblRb::NotFound, error_message(response)
  end
end