Skip to content
Browse files

Allow for dynamic error class generation (works better with Airbrake)

  • Loading branch information...
1 parent e71c5d1 commit 535dec874e7adb6b742ed25cac656da660be00dc @kyledrake kyledrake committed Oct 6, 2011
Showing with 35 additions and 3 deletions.
  1. +1 −1 lib/geoloqi/config.rb
  2. +11 −1 lib/geoloqi/session.rb
  3. +1 −1 lib/geoloqi/version.rb
  4. +22 −0 spec/geoloqi_session_spec.rb
View
2 lib/geoloqi/config.rb
@@ -1,6 +1,6 @@
module Geoloqi
class Config
- attr_accessor :client_id, :client_secret, :redirect_uri, :adapter, :logger, :use_hashie_mash, :throw_exceptions
+ attr_accessor :client_id, :client_secret, :redirect_uri, :adapter, :logger, :use_hashie_mash, :throw_exceptions, :use_dynamic_exceptions
def initialize(opts={})
self.use_hashie_mash ||= false
self.throw_exceptions ||= true
View
12 lib/geoloqi/session.rb
@@ -45,7 +45,17 @@ def run(meth, path, query=nil, headers={})
begin
response = execute meth, path, query, headers
hash = JSON.parse response.body
- raise ApiError.new(response.status, hash['error'], hash['error_description']) if hash.is_a?(Hash) && hash['error'] && @config.throw_exceptions
+
+ if hash.is_a?(Hash) && hash['error'] && @config.throw_exceptions
+ if @config.use_dynamic_exceptions && !hash['error'].nil? && !hash['error'].empty?
+ exception_class_name = hash['error'].split('_').collect {|w| w.capitalize}.join+'Error'
+ Geoloqi.const_set exception_class_name, Class.new(Geoloqi::ApiError) unless Geoloqi.const_defined? exception_class_name
+ raise_class = Geoloqi.const_get exception_class_name
+ else
+ raise_class = ApiError
+ end
+ raise raise_class.new(response.status, hash['error'], hash['error_description'])
+ end
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' && !(hash['error_description'] =~ /The auth code expired/)
View
2 lib/geoloqi/version.rb
@@ -1,5 +1,5 @@
module Geoloqi
def self.version
- '0.9.27'
+ '0.9.28'
end
end
View
22 spec/geoloqi_session_spec.rb
@@ -21,6 +21,28 @@
end
end
+ describe 'custom exceptions scheme' do
+ before do
+ @session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:use_dynamic_exceptions => true}
+ end
+
+ it 'should throw api error exception with custom name' do
+ stub_request(:get, api_url('specialerror')).
+ with(:headers => auth_headers).
+ to_return(:status => 404, :body => {'error' => 'not_found'}.to_json)
+
+ expect { rescuing {@session.get('specialerror')}.class == Geoloqi::NotFoundError }
+ end
+
+ it 'should throw api error exception without custom name if empty' do
+ stub_request(:get, api_url('specialerror')).
+ with(:headers => auth_headers).
+ to_return(:status => 404, :body => {'error' => ''}.to_json)
+
+ expect { rescuing {@session.get('specialerror')}.class == Geoloqi::ApiError }
+ end
+ end
+
describe 'with access token and throw exceptions false' do
before do
@session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:throw_exceptions => false}

0 comments on commit 535dec8

Please sign in to comment.
Something went wrong with that request. Please try again.