Skip to content

Commit

Permalink
- Add error handling to SES requests, such that a 500 page is shown w…
Browse files Browse the repository at this point in the history
…hen data cannot be returned
  • Loading branch information
j-corry committed Apr 17, 2024
1 parent ecb28a7 commit 8e1aa3b
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 78 deletions.
4 changes: 4 additions & 0 deletions app/controllers/content_objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def show
@ses_data = @object.ses_data
@page_title = @object.object_title

if @ses_data.has_key?(:error)
render template: 'layouts/shared/error/500', locals: { status: 500, message: 'There was an error resolving names using the SES service' }
else
render template: @object.template, :locals => { :object => @object }
end
end

end
Expand Down
13 changes: 12 additions & 1 deletion app/models/ses_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ def evaluated_responses
# returns a (flattened) array
output = []
lookup_id_groups.each do |id_group|
response = JSON.parse(api_response(id_group))

begin
response = JSON.parse(api_response(id_group))
rescue JSON::ParserError
# if SES fails it returns an error as XML
response = Hash.from_xml(api_response(id_group))
return [{ error: response }]
end

output << response['terms']
end

Expand All @@ -54,6 +62,9 @@ def data
# each hash is the parsed response from individual lookups (one per [group_size] IDs)
# the hashes contain a nested 'term' hash containing 'id' and 'name'

# If SES returns an error, we'll get an error key returned from evaluated_response
return responses.first if responses.first.has_key?(:error)

unless responses.compact.blank?
responses.each do |response|
ret[response['term']['id'].to_i] = response['term']['name']
Expand Down
2 changes: 1 addition & 1 deletion coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"result": {
"line": 89.55
"line": 89.34
}
}
17 changes: 16 additions & 1 deletion coverage/.resultset.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@
206,
null,
206,
0,
null,
206,
null,
null,
null,
null,
Expand Down Expand Up @@ -2370,7 +2374,15 @@
null,
1,
1,
null,
null,
2,
null,
null,
0,
0,
null,
null,
2,
null,
null,
Expand All @@ -2388,6 +2400,9 @@
null,
null,
null,
null,
2,
null,
2,
2,
3,
Expand Down Expand Up @@ -2751,6 +2766,6 @@
]
}
},
"timestamp": 1713278200
"timestamp": 1713348198
}
}

0 comments on commit 8e1aa3b

Please sign in to comment.