Skip to content

Commit

Permalink
catch RestClient exceptions and add the Fluidinfo Error Class to Resp…
Browse files Browse the repository at this point in the history
…onse instead
  • Loading branch information
gridaphobe committed Sep 17, 2011
1 parent 79772df commit ea2dd9a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
10 changes: 10 additions & 0 deletions lib/fluidinfo/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand All @@ -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

##
Expand All @@ -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

##
Expand All @@ -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

##
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/fluidinfo/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions spec/fluidinfo_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'fluidinfo'
require 'uuidtools'
require "fluidinfo"
require "uuidtools"

describe Fluidinfo do
describe "GET" do
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ea2dd9a

Please sign in to comment.