Skip to content

Commit 0aafd7e

Browse files
committed
Implement Content Store fallback into content item loader
For HTTP Error responses and timeouts on Graphql requests, fall back to content store and attach error information to prometheus labels.
1 parent 1dfcec5 commit 0aafd7e

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

lib/govuk_content_item_loader/govuk_conditional_content_item_loader.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ def initialize(request:, content_store_client: GdsApi.content_store, publishing_
99
end
1010

1111
def load
12-
can_load_from_graphql? ? content_item_from_graphql : content_item_from_content_store
12+
return content_item_from_content_store unless can_load_from_graphql?
13+
14+
content_item_from_graphql || content_item_from_content_store
1315
end
1416

1517
def can_load_from_graphql?
@@ -32,10 +34,10 @@ def content_item_from_graphql
3234
publishing_api_client.graphql_live_content_item(base_path)
3335
rescue GdsApi::HTTPErrorResponse => e
3436
set_prometheus_labels(graphql_status_code: e.code)
35-
raise e
36-
rescue GdsApi::TimedOutException => e
37+
false
38+
rescue GdsApi::TimedOutException
3739
set_prometheus_labels(graphql_api_timeout: true)
38-
raise e
40+
false
3941
end
4042

4143
def content_item_from_content_store

spec/lib/govuk_conditional_content_item_loader_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def initialize(code)
3737
context "when can load from GraphQL" do
3838
before do
3939
allow(loader).to receive(:can_load_from_graphql?).and_return(true)
40+
allow(content_store_client).to receive(:content_item).with(base_path).and_return("item_from_cs")
4041
allow(publishing_api_client).to receive(:graphql_live_content_item).with(base_path).and_return("graphql_item")
4142
end
4243

@@ -50,19 +51,19 @@ def initialize(code)
5051
expect(request.env["govuk.prometheus_labels"]["graphql_status_code"]).to eq(200)
5152
end
5253

53-
it "sets Prometheus labels and raises error for GdsApi::HTTPErrorResponse" do
54+
it "sets Prometheus labels with error code information and falls back to content store" do
5455
error = GdsApi::HTTPErrorResponse.new(503)
5556
allow(publishing_api_client).to receive(:graphql_live_content_item).and_raise(error)
5657

57-
expect { loader.load }.to raise_error(GdsApi::HTTPErrorResponse)
58+
expect(loader.load).to eq("item_from_cs")
5859
expect(request.env["govuk.prometheus_labels"]["graphql_status_code"]).to eq(503)
5960
end
6061

61-
it "sets Prometheus labels and raises error for GdsApi::TimedOutException" do
62+
it "sets Prometheus labels with timeout information and falls back to content store" do
6263
error = GdsApi::TimedOutException.new
6364
allow(publishing_api_client).to receive(:graphql_live_content_item).and_raise(error)
6465

65-
expect { loader.load }.to raise_error(GdsApi::TimedOutException)
66+
expect(loader.load).to eq("item_from_cs")
6667
expect(request.env["govuk.prometheus_labels"]["graphql_api_timeout"]).to be(true)
6768
end
6869
end

0 commit comments

Comments
 (0)