Skip to content

Commit

Permalink
Change error handling to always have a copy of response headers
Browse files Browse the repository at this point in the history
added some methods related to parsing rate limit headers
  • Loading branch information
thoughtafter committed Mar 31, 2011
1 parent 1ff1a57 commit 6db6fe2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/faraday/raise_http_4xx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ def self.register_on_complete(env)
env[:response].on_complete do |response|
case response[:status].to_i
when 400
raise Twitter::BadRequest, error_message(response)
raise Twitter::BadRequest.new(error_message(response), response[:response_headers])
when 401
raise Twitter::Unauthorized, error_message(response)
raise Twitter::Unauthorized.new(error_message(response), response[:response_headers])
when 403
raise Twitter::Forbidden, error_message(response)
raise Twitter::Forbidden.new(error_message(response), response[:response_headers])
when 404
raise Twitter::NotFound, error_message(response)
raise Twitter::NotFound.new(error_message(response), response[:response_headers])
when 406
raise Twitter::NotAcceptable, error_message(response)
raise Twitter::NotAcceptable.new(error_message(response), response[:response_headers])
when 420
raise Twitter::EnhanceYourCalm.new error_message(response), response[:response_headers]
raise Twitter::EnhanceYourCalm.new(error_message(response), response[:response_headers])
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/faraday/raise_http_5xx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def self.register_on_complete(env)
env[:response].on_complete do |response|
case response[:status].to_i
when 500
raise Twitter::InternalServerError, error_message(response, "Something is technically wrong.")
raise Twitter::InternalServerError.new(error_message(response, "Something is technically wrong."), response[:response_headers])
when 502
raise Twitter::BadGateway, error_message(response, "Twitter is down or being upgraded.")
raise Twitter::BadGateway.new(error_message(response, "Twitter is down or being upgraded."), response[:response_headers])
when 503
raise Twitter::ServiceUnavailable, error_message(response, "(__-){ Twitter is over capacity.")
raise Twitter::ServiceUnavailable.new(error_message(response, "(__-){ Twitter is over capacity."), response[:response_headers])
end
end
end
Expand Down
31 changes: 24 additions & 7 deletions lib/twitter/error.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
module Twitter
# Custom error class for rescuing from all Twitter errors
class Error < StandardError; end
class Error < StandardError
attr_reader :http_headers

def initialize(message, http_headers)
@http_headers = Hash[http_headers]
super message
end

def ratelimit_reset
Time.at(@http_headers.values_at('x-ratelimit-reset', 'X-RateLimit-Reset').detect {|value| value }.to_i)
end

def ratelimit_limit
@http_headers.values_at('x-ratelimit-limit', 'X-RateLimit-Limit').detect {|value| value }.to_i
end

def ratelimit_remaining
@http_headers.values_at('x-ratelimit-limit', 'X-RateLimit-Limit').detect {|value| value }.to_i
end

def retry_after
[(ratelimit_reset - Time.now).ceil, 0].max
end
end

# Raised when Twitter returns the HTTP status code 400
class BadRequest < Error; end
Expand All @@ -19,12 +42,6 @@ class NotAcceptable < Error; end

# Raised when Twitter returns the HTTP status code 420
class EnhanceYourCalm < Error
# Creates a new EnhanceYourCalm error
def initialize(message, http_headers)
@http_headers = Hash[http_headers]
super message
end

# The number of seconds your application should wait before requesting date from the Search API again
#
# @see http://dev.twitter.com/pages/rate-limiting
Expand Down

0 comments on commit 6db6fe2

Please sign in to comment.