Skip to content

Commit 9fdeea5

Browse files
authored
Merge pull request #6716 from NHSDigital/location-no-results-production
Only report `LocationPositionUpdater::NoResults` in production
2 parents 8e92a0b + a6d3278 commit 9fdeea5

11 files changed

Lines changed: 82 additions & 19 deletions

app/jobs/location_position_updater_job.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ def perform(location_id)
99
location = Location.find(location_id)
1010
LocationPositionUpdater.call(location)
1111
rescue LocationPositionUpdater::NoResults => e
12-
Sentry.capture_exception(e, level: "warning")
12+
if Settings.location_position_updater_job.capture_exception
13+
Sentry.capture_exception(e, level: "warning")
14+
else
15+
Rails.logger.warn(
16+
"Could not fetch position for: #{location.name} (#{location.id})"
17+
)
18+
Rails.logger.warn(e.backtrace.join("\n"))
19+
end
1320
end
1421
end

app/lib/location_position_updater.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def self.call(...) = new(...).call
2828

2929
private
3030

31-
def full_address = location.address_parts.join(", ")
31+
def query = [location.name, location.address_parts].join(", ")
3232

3333
def position
34-
response = OrdnanceSurvey::PlacesAPI.find(full_address)
34+
response = OrdnanceSurvey::PlacesAPI.find(query)
3535

3636
results = response[:results]
3737
raise NoResults if results.blank?

app/lib/ordnance_survey/places_api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def initialize
99
@base_url = "https://api.os.uk"
1010
end
1111

12-
def find(query)
13-
params = { query:, format: "json" }
12+
def find(query, max_results: 1, output_srs: "EPSG:4326")
13+
params = { query:, maxresults: max_results, output_srs:, format: "json" }
1414
response = connection.get("/search/places/v1/find", params)
1515
response.body.deep_transform_keys(&:downcase).deep_symbolize_keys
1616
end

config/settings.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ ordnance_survey:
6464
api_key: <%= Rails.application.credentials.ordnance_survey&.api_key %>
6565
secret_key: <%= Rails.application.credentials.ordnance_survey&.secret_key %>
6666

67+
location_position_updater_job:
68+
capture_exception: true
69+
6770
# Set a value to override the default values set in config/initializers/devise.rb
6871
devise:
6972
timeout_in_seconds:

config/settings/development.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ reporting_api:
2929

3030
careplus:
3131
base_url: http://localhost:8080
32+
33+
location_position_updater_job:
34+
capture_exception: false

config/settings/end_to_end.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ splunk:
2626
reporting_api:
2727
client_app:
2828
root_url: http://localhost:4101
29+
30+
location_position_updater_job:
31+
capture_exception: false

config/settings/staging.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ pds:
2020

2121
careplus:
2222
base_url: <%= ENV.fetch("MOCK_CAREPLUS_URL", nil) %>
23+
24+
location_position_updater_job:
25+
capture_exception: false

config/settings/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ reporting_api:
2727

2828
careplus:
2929
base_url: http://localhost:8080
30+
31+
location_position_updater_job:
32+
capture_exception: false

spec/jobs/location_position_updater_job_spec.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,35 @@
3939
allow(LocationPositionUpdater).to receive(:call).and_raise(error)
4040
end
4141

42-
it "captures the exception in Sentry at warning level" do
43-
expect(Sentry).to receive(:capture_exception).with(
44-
error,
45-
level: "warning"
46-
)
47-
perform
42+
context "when not capturing the exception" do
43+
it "doesn't capture the exception in Sentry" do
44+
expect(Sentry).not_to receive(:capture_exception)
45+
perform
46+
end
47+
48+
it "does not raise the error" do
49+
expect { perform }.not_to raise_error
50+
end
4851
end
4952

50-
it "does not raise the error" do
51-
expect { perform }.not_to raise_error
53+
context "when capturing the exception" do
54+
before do
55+
Settings.location_position_updater_job.capture_exception = true
56+
end
57+
58+
after { Settings.reload! }
59+
60+
it "captures the exception in Sentry at warning level" do
61+
expect(Sentry).to receive(:capture_exception).with(
62+
error,
63+
level: "warning"
64+
)
65+
perform
66+
end
67+
68+
it "does not raise the error" do
69+
expect { perform }.not_to raise_error
70+
end
5271
end
5372
end
5473

spec/lib/location_position_updater_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
let(:location) do
88
create(
99
:community_clinic,
10+
name: "Westminster Clinic",
1011
address_line_1: "1 High Street",
1112
address_town: "London",
1213
address_postcode: "SW1A 1AA",
@@ -49,7 +50,7 @@
4950

5051
it "calls the API with the address" do
5152
expect(OrdnanceSurvey::PlacesAPI).to receive(:find).with(
52-
"1 High Street, London, SW1A 1AA"
53+
"Westminster Clinic, 1 High Street, London, SW1A 1AA"
5354
)
5455
call
5556
end
@@ -93,6 +94,7 @@
9394
let(:location) do
9495
create(
9596
:community_clinic,
97+
name: "Westminster Clinic",
9698
address_line_1: "1 High Street",
9799
address_town: nil,
98100
address_postcode: "SW1A 1AA",
@@ -115,7 +117,7 @@
115117

116118
it "calls the API with partial address" do
117119
expect(OrdnanceSurvey::PlacesAPI).to receive(:find).with(
118-
"1 High Street, SW1A 1AA"
120+
"Westminster Clinic, 1 High Street, SW1A 1AA"
119121
)
120122
call
121123
end

0 commit comments

Comments
 (0)