Skip to content

Commit

Permalink
Remove ClientError, ServerError, and ParserError
Browse files Browse the repository at this point in the history
Closes #440.
  • Loading branch information
sferik committed Aug 18, 2013
1 parent 549e738 commit 7284394
Show file tree
Hide file tree
Showing 20 changed files with 76 additions and 133 deletions.
9 changes: 3 additions & 6 deletions lib/twitter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
require 'twitter/api/undocumented'
require 'twitter/api/users'
require 'twitter/configurable'
require 'twitter/error/client_error'
require 'twitter/error/parser_error'
require 'twitter/error'
require 'simple_oauth'
require 'base64'
require 'uri'
Expand Down Expand Up @@ -104,10 +103,8 @@ def request_setup(method, path, params, signature_params)
def request(method, path, params={}, signature_params=params)
request_setup = request_setup(method, path, params, signature_params)
connection.send(method.to_sym, path, params, &request_setup).env
rescue Faraday::Error::ClientError
raise Twitter::Error::ClientError
rescue JSON::ParserError
raise Twitter::Error::ParserError
rescue Faraday::Error::ClientError, JSON::ParserError
raise Twitter::Error
end

# Returns a Faraday::Connection object
Expand Down
9 changes: 3 additions & 6 deletions lib/twitter/default.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'faraday'
require 'faraday/request/multipart'
require 'twitter/error/client_error'
require 'twitter/error/server_error'
require 'twitter/error'
require 'twitter/request/multipart_with_file'
require 'twitter/response/parse_json'
require 'twitter/response/raise_error'
Expand All @@ -27,10 +26,8 @@ module Default
builder.use Faraday::Request::Multipart
# Convert request params to "www-form-urlencoded"
builder.use Faraday::Request::UrlEncoded
# Handle 4xx server responses
builder.use Twitter::Response::RaiseError, Twitter::Error::ClientError
# Handle 5xx server responses
builder.use Twitter::Response::RaiseError, Twitter::Error::ServerError
# Handle error responses
builder.use Twitter::Response::RaiseError, Twitter::Error
# Parse JSON response bodies
builder.use Twitter::Response::ParseJson
# Set Faraday's HTTP adapter
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/bad_gateway.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/server_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 502
class BadGateway < Twitter::Error::ServerError
class BadGateway < Twitter::Error
HTTP_STATUS_CODE = 502
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/bad_request.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/client_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 400
class BadRequest < Twitter::Error::ClientError
class BadRequest < Twitter::Error
HTTP_STATUS_CODE = 400
end
end
Expand Down
9 changes: 0 additions & 9 deletions lib/twitter/error/client_error.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/twitter/error/forbidden.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/client_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 403
class Forbidden < Twitter::Error::ClientError
class Forbidden < Twitter::Error
HTTP_STATUS_CODE = 403
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/gateway_timeout.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/server_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 504
class GatewayTimeout < Twitter::Error::ServerError
class GatewayTimeout < Twitter::Error
HTTP_STATUS_CODE = 504
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/internal_server_error.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/server_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 500
class InternalServerError < Twitter::Error::ServerError
class InternalServerError < Twitter::Error
HTTP_STATUS_CODE = 500
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/not_acceptable.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/client_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 406
class NotAcceptable < Twitter::Error::ClientError
class NotAcceptable < Twitter::Error
HTTP_STATUS_CODE = 406
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/not_found.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/client_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 404
class NotFound < Twitter::Error::ClientError
class NotFound < Twitter::Error
HTTP_STATUS_CODE = 404
end
end
Expand Down
9 changes: 0 additions & 9 deletions lib/twitter/error/parser_error.rb

This file was deleted.

9 changes: 0 additions & 9 deletions lib/twitter/error/server_error.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/twitter/error/service_unavailable.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/server_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 503
class ServiceUnavailable < Twitter::Error::ServerError
class ServiceUnavailable < Twitter::Error
HTTP_STATUS_CODE = 503
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/too_many_requests.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/client_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 429
class TooManyRequests < Twitter::Error::ClientError
class TooManyRequests < Twitter::Error
HTTP_STATUS_CODE = 429
end
EnhanceYourCalm = TooManyRequests
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/unauthorized.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/client_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 401
class Unauthorized < Twitter::Error::ClientError
class Unauthorized < Twitter::Error
HTTP_STATUS_CODE = 401
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error/unprocessable_entity.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'twitter/error/client_error'
require 'twitter/error'

module Twitter
class Error
# Raised when Twitter returns the HTTP status code 422
class UnprocessableEntity < Twitter::Error::ClientError
class UnprocessableEntity < Twitter::Error
HTTP_STATUS_CODE = 422
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@
end
it "catches Faraday errors" do
allow(subject).to receive(:connection).and_raise(Faraday::Error::ClientError.new("Oops"))
expect{subject.send(:request, :get, "/path")}.to raise_error Twitter::Error::ClientError
expect{subject.send(:request, :get, "/path")}.to raise_error Twitter::Error
end
it "catches JSON::ParserError errors" do
allow(subject).to receive(:connection).and_raise(JSON::ParserError.new("unexpected token"))
expect{subject.send(:request, :get, "/path")}.to raise_error Twitter::Error::ParserError
expect{subject.send(:request, :get, "/path")}.to raise_error Twitter::Error
end
end

Expand Down
50 changes: 0 additions & 50 deletions spec/twitter/error/client_error_spec.rb

This file was deleted.

20 changes: 0 additions & 20 deletions spec/twitter/error/server_error_spec.rb

This file was deleted.

46 changes: 46 additions & 0 deletions spec/twitter/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe Twitter::Error do

before do
@client = Twitter::Client.new
end

describe "#initialize" do
it "wraps another error class" do
begin
Expand All @@ -17,4 +21,46 @@
end
end

Twitter::Error.errors.each do |status, exception|

[nil, "error", "errors"].each do |body|
context "when HTTP status is #{status} and body is #{body.inspect}" do
before do
body_message = '{"' + body + '":"Client Error"}' unless body.nil?
stub_get("/1.1/statuses/user_timeline.json").with(:query => {:screen_name => "sferik"}).to_return(:status => status, :body => body_message)
end
it "raises #{exception.name}" do
expect{@client.user_timeline("sferik")}.to raise_error exception
end
end
end

context "when HTTP status is #{status} and body is errors" do
context "when errors is an array of hashes" do
context "when error code is nil" do
before do
body_message = '{"errors":[{"message":"Client Error"}]}'
stub_get("/1.1/statuses/user_timeline.json").with(:query => {:screen_name => "sferik"}).to_return(:status => status, :body => body_message)
end
it "raises #{exception.name}" do
expect{@client.user_timeline("sferik")}.to raise_error { |error|
expect(error.code).to eq nil
}
end
context "when error code is 187" do
before do
body_message = '{"errors":[{"message":"Client Error","code": 187 }]}'
stub_get("/1.1/statuses/user_timeline.json").with(:query => {:screen_name => "sferik"}).to_return(:status => status, :body => body_message)
end
it "raises #{exception.name}" do
expect{@client.user_timeline("sferik")}.to raise_error { |error|
expect(error.code).to eq 187
}
end
end
end
end
end
end

end

0 comments on commit 7284394

Please sign in to comment.