Permalink
Browse files

Integrate error status code with exceptions

  • Loading branch information...
kyledrake committed Jul 8, 2011
1 parent 40eb744 commit 9be4fd023cfec412ed22e59390127aed00a938e7
Showing with 18 additions and 9 deletions.
  1. +5 −4 lib/geoloqi/error.rb
  2. +1 −1 lib/geoloqi/session.rb
  3. +1 −1 lib/geoloqi/version.rb
  4. +11 −3 spec/geoloqi_spec.rb
View
@@ -1,16 +1,17 @@
module Geoloqi
class ApiError < StandardError
- attr_reader :type
- attr_reader :reason
- def initialize(type, reason=nil)
+ attr_reader :status, :type, :reason
+ def initialize(status, type, reason=nil)
+ @status = status
@type = type
@reason = reason
message = type
message += " - #{reason}" if reason
+ message += " (#{status})"
super message
end
end
class Error < StandardError; end
class ArgumentError < ArgumentError; end
-end
+end
View
@@ -55,7 +55,7 @@ def run(meth, path, query=nil)
end
hash = JSON.parse response.body
- raise ApiError.new(hash['error'], hash['error_description']) if hash.is_a?(Hash) && hash['error']
+ raise ApiError.new(response.status, hash['error'], hash['error_description']) if hash.is_a?(Hash) && hash['error']
rescue Geoloqi::ApiError
raise Error.new('Unable to procure fresh access token from API on second attempt') if retry_attempt > 0
if hash['error'] == 'expired_token'
View
@@ -1,5 +1,5 @@
module Geoloqi
def self.version
- '0.9.10'
+ '0.9.11'
end
end
View
@@ -28,10 +28,11 @@
describe Geoloqi::ApiError do
it 'throws exception properly and allows drill-down of message' do
- error = Geoloqi::ApiError.new 'not_enough_cats', 'not enough cats to complete this request'
+ error = Geoloqi::ApiError.new 405, 'not_enough_cats', 'not enough cats to complete this request'
+ expect { error.status == 405 }
expect { error.type == 'not_enough_cats' }
expect { error.reason == 'not enough cats to complete this request' }
- expect { error.message == "#{error.type} - #{error.reason}" }
+ expect { error.message == "#{error.type} - #{error.reason} (405)" }
end
end
@@ -182,7 +183,14 @@
end
it 'fails with an exception' do
- expect { rescuing { @session.get 'message/send' }.message == 'invalid_token' }
+ begin
+ @session.get 'message/send'
+ rescue Exception => e
+ expect { e.class == Geoloqi::ApiError }
+ expect { e.status == 401 }
+ expect { e.type == 'invalid_token' }
+ expect { e.message == 'invalid_token (401)' }
+ end
end
end

0 comments on commit 9be4fd0

Please sign in to comment.