Skip to content

Commit 0cc88cf

Browse files
committed
Better support of errors
1 parent c76de4e commit 0cc88cf

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

lib/translator_text/client.rb

+12-15
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,19 @@ def post(path, params)
9393
# If success, return the response body
9494
# If failure, raise an error
9595
def handle_response(response)
96-
case response.code
97-
when 200..299
98-
response
99-
else
100-
if response.request.format == :json
101-
raise ServiceError.new(
102-
code: response['error']['code'],
103-
message: response['error']['message']
104-
)
105-
else
106-
raise NetError.new(
107-
code: response.code,
108-
message: response.response.message
109-
)
110-
end
96+
return response if response.code.between?(200, 299)
97+
98+
if response.request.format == :json && response['error']
99+
raise ServiceError.new(
100+
code: response['error']['code'],
101+
message: response['error']['message']
102+
)
111103
end
104+
105+
raise NetError.new(
106+
code: response.code,
107+
message: response.response.message
108+
)
112109
end
113110

114111
def headers

spec/translator_text/client_spec.rb

+33-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)
1111
end
1212

13-
describe '(integrations tests)', integration: true do
13+
describe '(integrations tests)', :integration do
1414
before(:all) do
1515
WebMock.disable!
1616
end
@@ -73,8 +73,8 @@
7373
'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=fr'
7474
end
7575

76-
before do
77-
@stub = stub_request(:post, translate_url).with(
76+
let!(:stub) do
77+
stub_request(:post, translate_url).with(
7878
body: '[{"Text":"First sentence"},{"Text":"Second sentence"}]'
7979
).to_return(
8080
body: load_response('translation_1')
@@ -88,14 +88,14 @@
8888

8989
it 'calls the service' do
9090
results
91-
expect(@stub).to have_been_requested
91+
expect(stub).to have_been_requested
9292
end
9393
end
9494

9595
context 'with Types::Sentence' do
9696
it 'calls the service' do
9797
results
98-
expect(@stub).to have_been_requested
98+
expect(stub).to have_been_requested
9999
end
100100
end
101101

@@ -110,6 +110,18 @@
110110
it 'returns the detected language' do
111111
expect(results.map(&:detected_language)).to eq %i[en en]
112112
end
113+
114+
context 'with a unauthorized request' do
115+
before do
116+
stub_request(:post, translate_url).to_return(
117+
status: 401
118+
)
119+
end
120+
121+
it 'raises a NetError' do
122+
expect { results }.to raise_error(TranslatorText::NetError)
123+
end
124+
end
113125
end
114126

115127
describe '#detect' do
@@ -128,8 +140,8 @@
128140
'https://api.cognitive.microsofttranslator.com/detect?api-version=3.0'
129141
end
130142

131-
before do
132-
@stub = stub_request(:post, detect_url).with(
143+
let!(:stub) do
144+
stub_request(:post, detect_url).with(
133145
body: '[{"Text":"First sentence"},{"Text":"Second sentence"}]'
134146
).to_return(
135147
body: load_response('detection_1')
@@ -143,14 +155,14 @@
143155

144156
it 'calls the service' do
145157
results
146-
expect(@stub).to have_been_requested
158+
expect(stub).to have_been_requested
147159
end
148160
end
149161

150162
context 'with Types::Sentence' do
151163
it 'calls the service' do
152164
results
153-
expect(@stub).to have_been_requested
165+
expect(stub).to have_been_requested
154166
end
155167
end
156168

@@ -161,5 +173,17 @@
161173
it 'returns the detected language' do
162174
expect(results.map(&:language)).to eq %i[en en]
163175
end
176+
177+
context 'with a unauthorized request' do
178+
before do
179+
stub_request(:post, detect_url).to_return(
180+
status: 401
181+
)
182+
end
183+
184+
it 'raises a NetError' do
185+
expect { results }.to raise_error(TranslatorText::NetError)
186+
end
187+
end
164188
end
165189
end

0 commit comments

Comments
 (0)