Skip to content

Commit a80a1f7

Browse files
authored
Merge pull request #378 from MITLibraries/use-420
Make Primo errors friendlier
2 parents 8ea4dfd + 3b27cb6 commit a80a1f7

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

app/controllers/search_controller.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,25 @@ def validate_geobox_values!
388388
end
389389

390390
def handle_primo_errors(error)
391-
Rails.logger.error("Primo search error: #{error.message}")
392-
393-
if error.is_a?(ArgumentError)
394-
[{ 'message' => 'Primo search is not properly configured.' }]
395-
elsif error.is_a?(HTTP::TimeoutError)
396-
[{ 'message' => 'Sorry, that took longer than expected. Please try your search again.' }]
397-
else
398-
[{ 'message' => error.message }]
399-
end
391+
Rails.logger.error("Primo search error: #{error.class}: #{error.message}")
392+
393+
# For timeouts, prompt users to try again.
394+
return [{ 'message' => 'Sorry, that took longer than expected. Please try your search again.' }] if error.is_a?(HTTP::TimeoutError)
395+
396+
# For other errors, Primo is probably down, and we should direct users elsewhere.
397+
[{
398+
'code' => 'primo_unavailable',
399+
'message' => 'Hmm, we seem to be having difficulties...',
400+
'description' => 'In the meantime, try searching these tools directly.',
401+
'links' => [
402+
{ 'label' => "MIT's WorldCat", 'description' => 'Books and media', 'url' => 'https://libraries.mit.edu/worldcat' },
403+
{ 'label' => 'Google Scholar', 'description' => 'Articles', 'url' => 'https://scholar.google.com/' },
404+
{ 'label' => 'ArchivesSpace', 'description' => 'MIT archives', 'url' => 'https://archivesspace.mit.edu/' },
405+
{ 'label' => 'DSpace@MIT', 'description' => 'MIT research', 'url' => 'https://dspace.mit.edu/' }
406+
],
407+
'help_label' => 'Ask Us',
408+
'help_url' => 'https://libraries.mit.edu/ask/'
409+
}]
400410
end
401411

402412
# validate_format_token is only applicable to requests for JSON-format results. It takes no action so long as the

app/views/shared/_error.html.erb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
<% return unless error['message'].present? %>
2+
<% primo_unavailable = error['code'] == 'primo_unavailable' %>
23

3-
<aside class="alert alert-banner error" role="alert">
4-
<i></i>
5-
<div><h3><%= error['message'] %></h3>
6-
<%= error&.dig('extensions')&.dig('problems')&.pluck("explanation")&.join %>
7-
</div>
8-
</aside>
4+
<% if primo_unavailable %>
5+
<aside role="alert" class="primo-unavailable">
6+
<h3><%= error['message'] %></h3>
7+
8+
<p><%= error['description'] %></p>
9+
<ul>
10+
<% Array(error['links']).each do |link| %>
11+
<% next unless link['label'].present? && link['url'].present? %>
12+
<li>
13+
<a href="<%= link['url'] %>"><%= link['label'] %></a><% if link['description'].present? %>: <%= link['description'] %><% end %>
14+
</li>
15+
<% end %>
16+
</ul>
17+
<% if error['help_label'].present? && error['help_url'].present? %>
18+
<p>If you need help, <a href="<%= error['help_url'] %>"><%= error['help_label'] %></a>.</p>
19+
<% end %>
20+
</aside>
21+
<% else %>
22+
<aside class="alert alert-banner error" role="alert">
23+
<i></i>
24+
<div>
25+
<h3><%= error['message'] %></h3>
26+
27+
<%= error&.dig('extensions')&.dig('problems')&.pluck('explanation')&.join %>
28+
</div>
29+
</aside>
30+
<% end %>

test/controllers/search_controller_test.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,18 @@ def source_filter_count(controller)
851851

852852
get '/results?q=test&tab=primo'
853853
assert_response :success
854-
assert_select '.alert', count: 1
855-
assert_select '.alert', text: /API Error/
854+
assert_select 'aside[role="alert"]', count: 1
855+
assert_select 'aside.primo-unavailable h3', text: /Hmm, we seem to be having difficulties/
856+
assert_select 'aside.primo-unavailable', text: /In the meantime, try searching these tools directly/
857+
assert_select 'aside.primo-unavailable', text: /MIT's WorldCat: Books and media/
858+
assert_select 'aside.primo-unavailable', text: /Google Scholar: Articles/
859+
assert_select 'aside.primo-unavailable', text: /ArchivesSpace: MIT archives/
860+
assert_select 'aside.primo-unavailable', text: /DSpace@MIT: MIT research/
861+
assert_select 'aside.primo-unavailable a[href="https://libraries.mit.edu/worldcat"]', text: /MIT's WorldCat/
862+
assert_select 'aside.primo-unavailable a[href="https://scholar.google.com/"]', text: /Google Scholar/
863+
assert_select 'aside.primo-unavailable a[href="https://archivesspace.mit.edu/"]', text: /ArchivesSpace/
864+
assert_select 'aside.primo-unavailable a[href="https://dspace.mit.edu/"]', text: /DSpace@MIT/
865+
assert_select 'aside.primo-unavailable a[href="https://libraries.mit.edu/ask/"]', text: /Ask Us/
856866
end
857867

858868
test 'results uses simplified search summary for USE app' do
@@ -974,7 +984,8 @@ def source_filter_count(controller)
974984

975985
get '/results?q=test&tab=all'
976986
assert_response :success
977-
assert_select '.alert', text: /Primo API Error/
987+
assert_select 'aside.primo-unavailable h3', text: /Hmm, we seem to be having difficulties/
988+
assert_select 'aside.primo-unavailable', text: /In the meantime, try searching these tools directly/
978989
end
979990

980991
test 'all tab is default when no tab specified' do

0 commit comments

Comments
 (0)