Permalink
Browse files

catch RestClient exceptions and add the Fluidinfo Error Class to Resp…

…onse instead
  • Loading branch information...
1 parent 79772df commit ea2dd9a3dc4d9c2fac3a554802b65ad83a1ffe07 @gridaphobe committed Sep 17, 2011
Showing with 22 additions and 9 deletions.
  1. +10 −0 lib/fluidinfo/client.rb
  2. +3 −0 lib/fluidinfo/response.rb
  3. +9 −9 spec/fluidinfo_spec.rb
View
@@ -59,6 +59,8 @@ def get(path, options={})
url = build_url path, options
headers = options[:headers] || {}
Response.new(@client[url].get headers)
+ rescue RestClient::Exception => e
+ Response.new e.response
end
##
@@ -73,6 +75,8 @@ def head(path, options={})
url = build_url path, options
headers = options[:headers] || {}
Response.new(@client[url].head headers)
+ rescue RestClient::Exception => e
+ Response.new e.response
end
##
@@ -94,6 +98,8 @@ def post(path, options={})
headers.merge! :content_type => "application/json"
end
Response.new(@client[url].post body, headers)
+ rescue RestClient::Exception => e
+ Response.new e.response
end
##
@@ -111,6 +117,8 @@ def put(path, options={})
body, mime = build_payload options
headers = (options[:headers] || {}).merge :content_type => mime
Response.new(@client[url].put body, headers)
+ rescue RestClient::Exception => e
+ Response.new e.response
end
##
@@ -128,6 +136,8 @@ def delete(path, options={})
headers = options[:headers] || {}
# nothing returned in response body for DELETE
Response.new(@client[url].delete headers)
+ rescue RestClient::Exception => e
+ Response.new e.response
end
private
@@ -15,10 +15,13 @@ class Response
attr_reader :content
# [Hash, String] The parsed response if the +Content-Type+ was one of {Fluidinfo::JSON_TYPES}, otherwise equivalent to {#content}.
attr_reader :value
+ # [String] The error, if any, returned by Fluidinfo
+ attr_reader :error
def initialize(response)
@status = response.code
@headers = response.headers
+ @error = @headers[:x_fluiddb_error_class]
@content = response.body
@value = if JSON_TYPES.include? @headers[:content_type]
Yajl.load @content
View
@@ -1,5 +1,5 @@
-require 'fluidinfo'
-require 'uuidtools'
+require "fluidinfo"
+require "uuidtools"
describe Fluidinfo do
describe "GET" do
@@ -10,7 +10,7 @@
it "should return a Fluidinfo::Response" do
r = @fluid.get("/about/fluidinfo")
r.should be_a(Fluidinfo::Response)
- r.should respond_to(:status, :content, :headers, :value)
+ r.should respond_to(:status, :content, :headers, :value, :error)
end
describe "/objects" do
@@ -46,10 +46,10 @@
@fluid.get("/objects/#{uid}").value.should eq(expected)
end
- it "should raise 404 errors on bad request" do
- uid = "1"
- tag = "gridaphobe/given-name"
- expect{@fluid.get "/objects/#{uid}/#{tag}"}.to raise_error(RestClient::ResourceNotFound)
+ it "should contain an error message for failed requests" do
+ r = @fluid.get "/about/1/gridaphobe/given-name"
+ r.status.should eq(404)
+ r.error.should eq("TNoInstanceOnObject")
end
end
@@ -107,7 +107,7 @@
it "should return a Fluidinfo::Response" do
r = @fluid.post("/objects")
r.should be_a(Fluidinfo::Response)
- r.should respond_to(:status, :content, :headers, :value)
+ r.should respond_to(:status, :content, :headers, :value, :error)
end
describe "/namespaces" do
@@ -135,7 +135,7 @@
it "should return a Fluidinfo::Response" do
r = @fluid.put("/about/fluidinfo/test/tag", :body => nil)
r.should be_a(Fluidinfo::Response)
- r.should respond_to(:status, :content, :headers, :value)
+ r.should respond_to(:status, :content, :headers, :value, :error)
@fluid.delete("/about/fluidinfo/test/tag")
end

0 comments on commit ea2dd9a

Please sign in to comment.