From 63f9267160f99226bcd7ad2d68d6b0b28d4cced4 Mon Sep 17 00:00:00 2001 From: meyric Date: Wed, 27 Mar 2024 09:52:24 +0000 Subject: [PATCH] Track failed Academies API events 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. --- app/models/project.rb | 12 ++++++++++-- spec/models/project_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 4c9e7b4fb2..517fbcb24b 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 18c3bae54d..5ed7c8f743 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -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 @@ -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