From 97ea7aaff2fa955171caf52491a4f2636bc7fc93 Mon Sep 17 00:00:00 2001 From: Michael Bleigh Date: Fri, 20 Mar 2009 18:31:52 -0400 Subject: [PATCH] Adding error handling to dispatchers. --- lib/twitter_auth.rb | 4 ++++ lib/twitter_auth/dispatcher/basic.rb | 6 ++--- lib/twitter_auth/dispatcher/oauth.rb | 7 +++--- lib/twitter_auth/dispatcher/shared.rb | 28 ++++++++++++++++++++++ spec/twitter_auth/dispatcher/basic_spec.rb | 15 ++++++++++++ spec/twitter_auth/dispatcher/oauth_spec.rb | 15 ++++++++++++ 6 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 lib/twitter_auth/dispatcher/shared.rb diff --git a/lib/twitter_auth.rb b/lib/twitter_auth.rb index 1415182..03b6dce 100644 --- a/lib/twitter_auth.rb +++ b/lib/twitter_auth.rb @@ -67,3 +67,7 @@ def self.net require 'twitter_auth/controller_extensions' require 'twitter_auth/cryptify' require 'twitter_auth/dispatcher/oauth' +require 'twitter_auth/dispatcher/basic' +require 'twitter_auth/dispatcher/shared' + +class TwitterAuth::Dispatcher::Error < StandardError; end diff --git a/lib/twitter_auth/dispatcher/basic.rb b/lib/twitter_auth/dispatcher/basic.rb index 3ffdb59..bcfbb08 100644 --- a/lib/twitter_auth/dispatcher/basic.rb +++ b/lib/twitter_auth/dispatcher/basic.rb @@ -3,6 +3,8 @@ module TwitterAuth module Dispatcher class Basic + include TwitterAuth::Dispatcher::Shared + attr_accessor :user def initialize(user) @@ -20,9 +22,7 @@ def request(http_method, path, body=nil, *arguments) http.request(req) } - JSON.parse(response.body) - rescue JSON::ParserError - response.body + handle_response(response) end def get(path, *arguments) diff --git a/lib/twitter_auth/dispatcher/oauth.rb b/lib/twitter_auth/dispatcher/oauth.rb index ed8d3fe..ca18742 100644 --- a/lib/twitter_auth/dispatcher/oauth.rb +++ b/lib/twitter_auth/dispatcher/oauth.rb @@ -3,6 +3,8 @@ module TwitterAuth module Dispatcher class Oauth < OAuth::AccessToken + include TwitterAuth::Dispatcher::Shared + attr_accessor :user def initialize(user) @@ -14,9 +16,8 @@ def initialize(user) def request(http_method, path, *arguments) path << '.json' unless path.match(/\.(:?xml|json)\z/i) response = super - JSON.parse(response.body) - rescue JSON::ParserError - response.body + + handle_response(response) end end end diff --git a/lib/twitter_auth/dispatcher/shared.rb b/lib/twitter_auth/dispatcher/shared.rb new file mode 100644 index 0000000..4bc36a2 --- /dev/null +++ b/lib/twitter_auth/dispatcher/shared.rb @@ -0,0 +1,28 @@ +module TwitterAuth + module Dispatcher + module Shared + def handle_response(response) + case response + when Net::HTTPOK + begin + JSON.parse(response.body) + rescue JSON::ParserError + response.body + end + else + message = begin + JSON.parse(response.body)['error'] + rescue JSON::ParserError + if match = response.body.match(/(.*)<\/error>/) + match[1] + else + 'An error occurred processing your Twitter request.' + end + end + + raise TwitterAuth::Dispatcher::Error, message + end + end + end + end +end diff --git a/spec/twitter_auth/dispatcher/basic_spec.rb b/spec/twitter_auth/dispatcher/basic_spec.rb index f51bc8d..59b6b29 100644 --- a/spec/twitter_auth/dispatcher/basic_spec.rb +++ b/spec/twitter_auth/dispatcher/basic_spec.rb @@ -47,6 +47,21 @@ @net.should_receive(:start) lambda{@dispatcher.request(:get, '/fake')}.should raise_error(NoMethodError) end + + it "should raise a TwitterAuth::Dispatcher::Error if response code isn't 200" do + FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized']) + lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error) + end + + it 'should set the error message to the JSON message' do + FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized']) + lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response') + end + + it 'should set the error message to the XML message' do + FakeWeb.register_uri('https://twitter.com:443/bad_response.xml', :string => "\n/bad_response.xml\nbad response\n", :status => ['401', 'Unauthorized']) + lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response') + end end %w(get post delete put).each do |method| diff --git a/spec/twitter_auth/dispatcher/oauth_spec.rb b/spec/twitter_auth/dispatcher/oauth_spec.rb index f837732..05a3013 100644 --- a/spec/twitter_auth/dispatcher/oauth_spec.rb +++ b/spec/twitter_auth/dispatcher/oauth_spec.rb @@ -45,6 +45,21 @@ @dispatcher.request(:get, '/fake').should == @dispatcher.request(:get, '/fake.json') end + it "should raise a TwitterAuth::Dispatcher::Error if response code isn't 200" do + FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized']) + lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error) + end + + it 'should set the error message to the JSON message' do + FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized']) + lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response') + end + + it 'should set the error message to the XML message' do + FakeWeb.register_uri('https://twitter.com:443/bad_response.xml', :string => "\n/bad_response.xml\nbad response\n", :status => ['401', 'Unauthorized']) + lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response') + end + it 'should work with verb methods' do @dispatcher.get('/fake').should == @dispatcher.request(:get, '/fake') end