Skip to content

Commit 6fd1dd8

Browse files
authored
fix(forest-requester): don't swallow error anymore (#710)
* fix(forest-requester): don't swallow error anymore * fix(forest-requester): don't swallow error anymore * test: forest api requester
1 parent 726fcb7 commit 6fd1dd8

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

app/services/forest_liana/forest_api_requester.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ def self.get(route, query: nil, headers: {})
1010
query: query,
1111
}).response
1212
rescue
13-
raise "Cannot reach Forest API at #{forest_api_url}#{route}, it seems to be down right now."
13+
message = "Cannot reach Forest API at #{forest_api_url}#{route}, it seems to be down right now."
14+
FOREST_LOGGER.error message
15+
FOREST_REPORTER.report message
16+
raise
1417
end
1518
end
1619

@@ -29,7 +32,10 @@ def self.post(route, body: nil, query: nil, headers: {})
2932
body: body.to_json,
3033
}).response
3134
rescue
32-
raise "Cannot reach Forest API at #{post_route}, it seems to be down right now."
35+
message = "Cannot reach Forest API at #{post_route}, it seems to be down right now."
36+
FOREST_LOGGER.error message
37+
FOREST_REPORTER.report message
38+
raise
3339
end
3440
end
3541

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module ForestLiana
2+
describe ForestApiRequester do
3+
describe 'when an error occurs' do
4+
before do
5+
allow(HTTParty).to receive(:get).and_raise(StandardError, 'Custom error message')
6+
allow(HTTParty).to receive(:post).and_raise(StandardError, 'Custom error message')
7+
end
8+
9+
describe 'Get' do
10+
it 'should keep the original error and raise it with backtrace' do
11+
err = nil
12+
13+
begin
14+
ForestApiRequester.get('/incorrect_url')
15+
rescue => error
16+
err = error
17+
end
18+
19+
expect(error)
20+
.to be_instance_of(StandardError)
21+
.and have_attributes(:message => 'Custom error message')
22+
.and have_attributes(:backtrace => be_truthy)
23+
end
24+
end
25+
26+
describe 'Post' do
27+
it 'should keep the original error and raise it with backtrace' do
28+
err = nil
29+
30+
begin
31+
ForestApiRequester.post('/incorrect_url')
32+
rescue => error
33+
err = error
34+
end
35+
36+
expect(error)
37+
.to be_instance_of(StandardError)
38+
.and have_attributes(:message => 'Custom error message')
39+
.and have_attributes(:backtrace => be_truthy)
40+
end
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)