Skip to content

Commit

Permalink
Adding error handling to dispatchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Mar 20, 2009
1 parent a71312d commit 97ea7aa
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/twitter_auth.rb
Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/twitter_auth/dispatcher/basic.rb
Expand Up @@ -3,6 +3,8 @@
module TwitterAuth
module Dispatcher
class Basic
include TwitterAuth::Dispatcher::Shared

attr_accessor :user

def initialize(user)
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions lib/twitter_auth/dispatcher/oauth.rb
Expand Up @@ -3,6 +3,8 @@
module TwitterAuth
module Dispatcher
class Oauth < OAuth::AccessToken
include TwitterAuth::Dispatcher::Shared

attr_accessor :user

def initialize(user)
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions 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>(.*)<\/error>/)
match[1]
else
'An error occurred processing your Twitter request.'
end
end

raise TwitterAuth::Dispatcher::Error, message
end
end
end
end
end
15 changes: 15 additions & 0 deletions spec/twitter_auth/dispatcher/basic_spec.rb
Expand Up @@ -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 => "<hash>\n<request>/bad_response.xml</request>\n<error>bad response</error>\n</hash>", :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|
Expand Down
15 changes: 15 additions & 0 deletions spec/twitter_auth/dispatcher/oauth_spec.rb
Expand Up @@ -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 => "<hash>\n<request>/bad_response.xml</request>\n<error>bad response</error>\n</hash>", :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
Expand Down

0 comments on commit 97ea7aa

Please sign in to comment.