Skip to content

Commit 0b9b855

Browse files
committed
return a useful message from RecordInvalid
fixes zendesk#111
1 parent a04db70 commit 0b9b855

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/zendesk_api/error.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def initialize(response)
1414
@errors = {}
1515
end
1616
end
17+
18+
def to_s
19+
"#{self.class.name}: #{@errors.to_s}"
20+
end
1721
end
1822

1923
class NetworkError < ClientError; end

spec/core/middleware/response/raise_error_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
end
2525

2626
context "status errors" do
27+
let(:body) { "" }
28+
2729
before(:each) do
28-
stub_request(:any, /.*/).to_return(:status => status)
30+
stub_request(:any, /.*/).to_return(:status => status, :body => body,
31+
:headers => { :content_type => "application/json" })
2932
end
3033

3134
context "with status = 404" do
@@ -50,6 +53,21 @@
5053
it "should raise RecordInvalid" do
5154
expect { client.connection.get "/non_existent" }.to raise_error(ZendeskAPI::Error::RecordInvalid)
5255
end
56+
57+
context "with a body" do
58+
let(:body) { MultiJson.dump(:details => "hello") }
59+
60+
it "should return RecordInvalid with proper message" do
61+
begin
62+
client.connection.get "/non_existent"
63+
rescue ZendeskAPI::Error::RecordInvalid => e
64+
e.errors.should == "hello"
65+
e.to_s.should == "ZendeskAPI::Error::RecordInvalid: hello"
66+
else
67+
fail # didn't raise an error
68+
end
69+
end
70+
end
5371
end
5472

5573
context "with status = 200" do

0 commit comments

Comments
 (0)