Method: RubyBox::Session#handle_errors

Defined in:
lib/ruby-box/session.rb

#handle_errors(response, raw) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ruby-box/session.rb', line 108

def handle_errors( response, raw )
  status = response.code.to_i
  body = response.body
  begin
    parsed_body = JSON.parse(body)
  rescue
    msg = body.nil? || body.empty? ? "no data returned" : body
    parsed_body = { "message" =>  msg }
  end

  # status is used to determine whether
  # we need to refresh the access token.
  parsed_body["status"] = status

  case status / 100
  when 3
    # 302 Found. We should return the url
    parsed_body["location"] = response["Location"] if status == 302
  when 4
    raise(RubyBox::ItemNameInUse.new(parsed_body, status, body), parsed_body["message"]) if parsed_body["code"] == "item_name_in_use"
    raise(RubyBox::AuthError.new(parsed_body, status, body), parsed_body["message"]) if parsed_body["code"] == "unauthorized" || status == 401
    raise(RubyBox::RequestError.new(parsed_body, status, body), parsed_body["message"])
  when 5
    raise(RubyBox::ServerError.new(parsed_body, status, body), parsed_body["message"])
  end
  raw ? body : parsed_body
end