Permalink
Browse files

Remove ClientError, ServerError, and ParserError

Closes #440.
  • Loading branch information...
1 parent 549e738 commit 72843948d8a6f66345adc254fa91cf1097592b22 @sferik committed Aug 18, 2013
View
@@ -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'
@@ -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
View
@@ -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'
@@ -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
@@ -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
@@ -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
@@ -1,9 +0,0 @@
-require 'twitter/error'
-
-module Twitter
- class Error
- # Raised when Twitter returns a 4xx HTTP status code or there's an error in Faraday
- class ClientError < Twitter::Error
- end
- end
-end
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -1,9 +0,0 @@
-require 'twitter/error'
-
-module Twitter
- class Error
- # Raised when JSON parsing fails
- class ParserError < Twitter::Error
- end
- end
-end
@@ -1,9 +0,0 @@
-require 'twitter/error'
-
-module Twitter
- class Error
- # Raised when Twitter returns a 5xx HTTP status code
- class ServerError < Twitter::Error
- end
- end
-end
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -1,50 +0,0 @@
-require 'helper'
-
-describe Twitter::Error::ClientError do
-
- before do
- @client = Twitter::Client.new
- end
-
- Twitter::Error::ClientError.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
@@ -1,20 +0,0 @@
-require 'helper'
-
-describe Twitter::Error::ServerError do
-
- before do
- @client = Twitter::Client.new
- end
-
- Twitter::Error::ServerError.errors.each do |status, exception|
- context "when HTTP status is #{status}" do
- before do
- stub_get("/1.1/statuses/user_timeline.json").with(:query => {:screen_name => "sferik"}).to_return(:status => status)
- end
- it "raises #{exception.name}" do
- expect{@client.user_timeline("sferik")}.to raise_error exception
- end
- end
- end
-
-end
View
@@ -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
@@ -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.