Permalink
Browse files

[FIX] better error reporting for login

  • Loading branch information...
1 parent 5409535 commit 8b8b0e0f3e7f4c73ceafdb443e6d0e45842557c0 Elad Meidar committed Jul 8, 2014
Showing with 24 additions and 5 deletions.
  1. +3 −3 Gemfile.lock
  2. +21 −2 lib/nest_thermostat/nest.rb
View
6 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- nest_thermostat (0.0.3)
+ nest_thermostat (0.0.4)
httparty (~> 0.8.3)
GEM
@@ -15,8 +15,8 @@ GEM
multi_json (~> 1.0)
multi_xml
method_source (0.8.1)
- multi_json (1.3.6)
- multi_xml (0.5.1)
+ multi_json (1.10.1)
+ multi_xml (0.5.5)
pry (0.9.12)
coderay (~> 1.0.5)
method_source (~> 0.8)
View
23 lib/nest_thermostat/nest.rb
@@ -5,6 +5,13 @@
module NestThermostat
class Nest
+
+ module Exceptions
+ class InvalidLoginCredentials < StandardError; end
+ class NestLoginError < StandardError; end
+ class TooManyRequests < StandardError; end
+ end
+
attr_accessor :email, :password, :login_url, :user_agent, :auth,
:temperature_scale, :login, :token, :user_id, :transport_url,
:transport_host, :structure_id, :device_id, :headers
@@ -126,8 +133,20 @@ def perform_login
headers: { 'User-Agent' => self.user_agent }
)
- self.auth ||= JSON.parse(login_request.body) rescue nil
- raise 'Invalid login credentials' if self.auth.has_key?('error') && self.auth['error'] == "access_denied"
+ case login_request.code
+ when 200..202
+ self.auth ||= JSON.parse(login_request.body) rescue nil
+ when 429
+ raise NestThermostat::Nest::Exceptions::TooManyRequests, login_request.body
+ when 500
+ if self.auth.respond_to?(:has_key?) && self.auth.has_key?('error') && self.auth['error'] == "access_denied"
+ raise NestThermostat::Nest::Exceptions::InvalidLoginCredentials, self.auth["error_description"]
+ else
+ raise NestThermostat::Nest::Exceptions::NestLoginError, login_request.body
+ end
+ else
+ raise NestThermostat::Nest::Exceptions::NestLoginError, "Unknown Error #{login_request.code}"
+ end
end
def convert_temp_for_get(degrees)

0 comments on commit 8b8b0e0

Please sign in to comment.