Skip to content

Commit 2bd2fd2

Browse files
conradbeachcodealchemy
authored andcommitted
feature: Improve error message for record invalid errors
While attempting to merge 2 users together in Zendesk, I received a 422 response with the response body `{"error"=>"You cannot merge Customer Name while they have an external ID."}`. However, this error message was not included in the raised exception. As a result, our exception monitoring tool only showed `ZendeskAPI::Error::RecordInvalid: {}` without any details. This made determining the actual issue challenging. I think modifying the code that generates the message for `ZendeskAPI::Error::RecordInvalid` errors to use the message in the `error` key of the response body, if necessary, will improve the usability of this exception.
1 parent d5cec19 commit 2bd2fd2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/zendesk_api/error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(*)
2020
super
2121

2222
if response[:body].is_a?(Hash)
23-
@errors = response[:body]["details"] || generate_error_msg(response[:body])
23+
@errors = response[:body]["details"] || generate_error_msg(response[:body]) || response[:body]["error"]
2424
end
2525

2626
@errors ||= {}

spec/core/middleware/response/raise_error_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@
9292
else
9393
fail # didn't raise an error
9494
end
95+
96+
context "with only an error key" do
97+
let(:body) { JSON.dump(:error => "something went wrong") }
98+
99+
it "should return RecordInvalid with proper message" do
100+
begin
101+
client.connection.get "/non_existent"
102+
rescue ZendeskAPI::Error::RecordInvalid => e
103+
expect(e.errors).to eq("something went wrong")
104+
expect(e.to_s).to eq("ZendeskAPI::Error::RecordInvalid: something went wrong")
105+
else
106+
fail # didn't raise an error
107+
end
108+
end
109+
end
95110
end
96111
end
97112

@@ -113,6 +128,21 @@
113128
else
114129
fail # didn't raise an error
115130
end
131+
132+
context "with only an error key" do
133+
let(:body) { JSON.dump(:error => "something went wrong") }
134+
135+
it "should return RecordInvalid with proper message" do
136+
begin
137+
client.connection.get "/non_existent"
138+
rescue ZendeskAPI::Error::RecordInvalid => e
139+
expect(e.errors).to eq("something went wrong")
140+
expect(e.to_s).to eq("ZendeskAPI::Error::RecordInvalid: something went wrong")
141+
else
142+
fail # didn't raise an error
143+
end
144+
end
145+
end
116146
end
117147
end
118148

0 commit comments

Comments
 (0)