Skip to content

Commit faad7b0

Browse files
Merge pull request #5 from alphagov/content-store-fallback
Implement Content Store fallback into content item loader
2 parents 7f50443 + e631d1a commit faad7b0

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.0
4+
5+
- Add Content Store fallback for failed Graphql requests
6+
37
## 1.0.0
48

59
- Add GraphQL traffic rates initializer.

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
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module GovukContentItemLoader
2-
VERSION = "1.0.0".freeze
2+
VERSION = "1.1.0".freeze
33
end

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)