Skip to content

Commit

Permalink
Track failed Academies API events
Browse files Browse the repository at this point in the history
We want to surface when and why the Academies API is failing, like the
Members API we can do this at the point of use in the Project.
  • Loading branch information
mec committed Mar 27, 2024
1 parent fa51e76 commit 63f9267
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,22 @@ def form_a_mat?

private def fetch_establishment(urn)
result = Api::AcademiesApi::Client.new.get_establishment(urn)
raise result.error if result.error.present?

if result.error.present?
track_event(result.error.message)
raise result.error
end

result.object
end

private def fetch_trust(ukprn)
result = Api::AcademiesApi::Client.new.get_trust(ukprn)
raise result.error if result.error.present?

if result.error.present?
track_event(result.error.message)
raise result.error
end

result.object
end
Expand Down
20 changes: 20 additions & 0 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@
it "raises the error" do
expect { subject.establishment }.to raise_error(Api::AcademiesApi::Client::NotFoundError, error_message)
end

it "sends the event to Application Insights" do
ClimateControl.modify(APPLICATION_INSIGHTS_KEY: "fake-application-insights-key") do
telemetry_client = double(ApplicationInsights::TelemetryClient, track_event: true, flush: true)
allow(ApplicationInsights::TelemetryClient).to receive(:new).and_return(telemetry_client)

expect { subject.establishment }.to raise_error(Api::AcademiesApi::Client::NotFoundError)
expect(telemetry_client).to have_received(:track_event)
end
end
end

context "when the Academies API client returns a #{Api::AcademiesApi::Client::Error}" do
Expand Down Expand Up @@ -325,6 +335,16 @@
it "raises the error" do
expect { subject.incoming_trust }.to raise_error(Api::AcademiesApi::Client::NotFoundError, error_message)
end

it "sends the event to Application Insights" do
ClimateControl.modify(APPLICATION_INSIGHTS_KEY: "fake-application-insights-key") do
telemetry_client = double(ApplicationInsights::TelemetryClient, track_event: true, flush: true)
allow(ApplicationInsights::TelemetryClient).to receive(:new).and_return(telemetry_client)

expect { subject.incoming_trust }.to raise_error(Api::AcademiesApi::Client::NotFoundError)
expect(telemetry_client).to have_received(:track_event)
end
end
end

context "when the Academies API client returns a #{Api::AcademiesApi::Client::Error}" do
Expand Down

0 comments on commit 63f9267

Please sign in to comment.