Permalink
Browse files

Error handling on requests.

  • Loading branch information...
1 parent 48d94d3 commit 9434b7ad70d41f875cd0307bf519ea49f79b6e8d @kevintuhumury kevintuhumury committed Mar 25, 2012
View
@@ -6,10 +6,11 @@
require "cameraplus/api/page"
require "cameraplus/user"
-require "cameraplus/page_metadata"
require "cameraplus/page"
+require "cameraplus/page_metadata"
require "cameraplus/photo"
require "cameraplus/photo_exif"
require "cameraplus/photo_recipe"
require "cameraplus/comment"
+require "cameraplus/exceptions"
require "cameraplus/version"
@@ -5,25 +5,39 @@ class Request
base_uri "http://camerapl.us"
- format :json
-
def self.call(url, options = {})
- new(url, options).response
+ new(url, options).parsed_response
end
def initialize(url, options = {})
@url = url
@options = options
end
- def response
- request.parsed_response
+ def parsed_response
+ if code == 200
+ @response.parsed_response
+ else
+ raise InvalidResponseError, message
+ end
end
private
def request
- self.class.get @url, query: @options
+ @response ||= self.class.get @url, query: @options
+ end
+
+ def response
+ request.response
+ end
+
+ def code
+ response.code.to_i
+ end
+
+ def message
+ "#{@response.message} (#{@response.code}): #{@response.body}\n#{@response.inspect}"
end
end
@@ -0,0 +1,7 @@
+module Cameraplus
+ class InvalidArgumentError < StandardError
+ end
+
+ class InvalidResponseError < StandardError
+ end
+end
@@ -10,15 +10,11 @@
subject.base_uri.should eq "http://camerapl.us"
end
- it "should know it's response format" do
- subject.format.should eq :json
- end
-
end
context "connecting with the api" do
- use_vcr_cassette :request_api
+ use_vcr_cassette :api_request_valid
let(:response) { subject.call "/user/mostlylisa:pages" }
@@ -36,4 +32,16 @@
end
+ context "connecting with an invalid api call" do
+
+ use_vcr_cassette :api_request_invalid
+
+ let(:response) { subject.call "/non-existing-page" }
+
+ it "should raise an error" do
+ expect { response }.to raise_error Cameraplus::InvalidResponseError
+ end
+
+ end
+
end

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 9434b7a

Please sign in to comment.