Skip to content

Commit 575e86d

Browse files
committed
Check additional errors key
`errors` is returned in the validation message when attempting to create a translation for a locale that is not supported.
1 parent 2bd2fd2 commit 575e86d

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

lib/zendesk_api/error.rb

Lines changed: 4 additions & 4 deletions
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]) || response[:body]["error"]
23+
@errors = response[:body]["details"] || generate_error_msg(response[:body])
2424
end
2525

2626
@errors ||= {}
@@ -33,11 +33,11 @@ def to_s
3333
private
3434

3535
def generate_error_msg(response_body)
36-
return unless response_body["description"] || response_body["message"]
37-
3836
[
3937
response_body["description"],
40-
response_body["message"]
38+
response_body["message"],
39+
response_body["error"],
40+
response_body["errors"]
4141
].compact.join(" - ")
4242
end
4343
end

spec/core/middleware/response/raise_error_spec.rb

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,18 @@
9393
fail # didn't raise an error
9494
end
9595

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
96+
{
97+
error: 'There was an error',
98+
errors: 'There were several errors'
99+
}.each do |key, message|
100+
context "with only an #{key} key" do
101+
let(:body) { JSON.dump(key => message) }
102+
103+
it "should return RecordInvalid with proper message" do
104+
expect { client.connection.get "/non_existent" }.to raise_error do |error|
105+
expect(error).to be_a(ZendeskAPI::Error::RecordInvalid)
106+
expect(error.errors).to eq(message)
107+
end
107108
end
108109
end
109110
end
@@ -129,17 +130,18 @@
129130
fail # didn't raise an error
130131
end
131132

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
133+
{
134+
error: 'There was an error',
135+
errors: 'There were several errors'
136+
}.each do |key, message|
137+
context "with only an #{key} key" do
138+
let(:body) { JSON.dump(key => message) }
139+
140+
it "should return RecordInvalid with proper message" do
141+
expect { client.connection.get "/non_existent" }.to raise_error do |error|
142+
expect(error).to be_a(ZendeskAPI::Error::RecordInvalid)
143+
expect(error.errors).to eq(message)
144+
end
143145
end
144146
end
145147
end

0 commit comments

Comments
 (0)