From 72d46c4804a30b28ab351a5a0d37d6bc664e577e Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Sun, 5 Apr 2009 13:01:35 -0400 Subject: [PATCH] Added more data to some of the exceptions to help with debugging. --- .gitignore | 1 + History | 6 +++++- examples/connect.rb | 8 +++----- examples/helpers/config_store.rb | 8 ++++++++ lib/twitter.rb | 22 ++++++++++++++++------ lib/twitter/request.rb | 8 +++++--- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 5949266da..74c966749 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ pkg log doc twitter-*.gem +rdoc \ No newline at end of file diff --git a/History b/History index 678ab7e20..a29a19dc3 100644 --- a/History +++ b/History @@ -1,4 +1,8 @@ -0.5.0 - March 3, 2009 +0.5.1 - April 5, 2009 +* 1 minor change + * Added data error hash returned from twitter to a few of the exceptions to help with debugging + +0.5.0 - April 3, 2009 * 1 major rewrite for OAuth * Backwards compatibility thrown to the wind * Proxy no longer supported (someone please add it back in, I never use proxies) diff --git a/examples/connect.rb b/examples/connect.rb index c0274260b..dae9d8b49 100644 --- a/examples/connect.rb +++ b/examples/connect.rb @@ -7,20 +7,18 @@ if config['atoken'] && config['asecret'] oauth.authorize_from_access(config['atoken'], config['asecret']) - # puts oauth.access_token.get("/statuses/friends_timeline.json") twitter = Twitter::Base.new(oauth) pp twitter.friends_timeline elsif config['rtoken'] && config['rsecret'] oauth.authorize_from_request(config['rtoken'], config['rsecret']) - puts oauth.access_token.get("/statuses/friends_timeline.json") + twitter = Twitter::Base.new(oauth) + pp twitter.friends_timeline config.update({ 'atoken' => oauth.access_token.token, 'asecret' => oauth.access_token.secret, - 'rtoken' => nil, - 'rsecret' => nil, - }) + }).delete('rtoken', 'rsecret') else config.update({ 'rtoken' => oauth.request_token.token, diff --git a/examples/helpers/config_store.rb b/examples/helpers/config_store.rb index a5de76eb0..6ffb20b49 100644 --- a/examples/helpers/config_store.rb +++ b/examples/helpers/config_store.rb @@ -19,12 +19,20 @@ def []=(key, value) @config[key] = value end + def delete(*keys) + keys.each { |key| @config.delete(key) } + save + self + end + def update(c={}) @config.merge!(c) save + self end def save File.open(file, 'w') { |f| f.write(YAML.dump(@config)) } + self end end \ No newline at end of file diff --git a/lib/twitter.rb b/lib/twitter.rb index 5f50b52a7..0d8a81f17 100644 --- a/lib/twitter.rb +++ b/lib/twitter.rb @@ -7,12 +7,22 @@ end module Twitter - class RateLimitExceeded < StandardError; end - class Unauthorized < StandardError; end - class Unavailable < StandardError; end - class InformTwitter < StandardError; end - class NotFound < StandardError; end - class General < StandardError; end + class TwitterError < StandardError + attr_reader :data + + def initialize(data) + @data = data + super + end + end + + class RateLimitExceeded < TwitterError; end + class Unauthorized < TwitterError; end + class General < TwitterError; end + + class Unavailable < StandardError; end + class InformTwitter < StandardError; end + class NotFound < StandardError; end def self.firehose diff --git a/lib/twitter/request.rb b/lib/twitter/request.rb index 2d7fcd318..9efc60637 100644 --- a/lib/twitter/request.rb +++ b/lib/twitter/request.rb @@ -51,11 +51,13 @@ def make_friendly(response) def raise_errors(response) case response.code.to_i when 400 - raise RateLimitExceeded, "(#{response.code}): #{response.message}" + raise RateLimitExceeded.new(parse(response)), "(#{response.code}): #{response.message} - #{data['error']}" when 401 - raise Unauthorized, "(#{response.code}): #{response.message}" + data = parse(response) + raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data['error']}" when 403 - raise General, "(#{response.code}): #{response.message}" + data = parse(response) + raise General.new(data), "(#{response.code}): #{response.message} - #{data['error']}" when 404 raise NotFound, "(#{response.code}): #{response.message}" when 500