Skip to content

Commit 1432056

Browse files
Merge pull request #91 from ebsco/results-error-handling
Results error handling
2 parents 404aae0 + fcacbaf commit 1432056

30 files changed

Lines changed: 8330 additions & 9526 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.8] - 2019-08-12
8+
### Fixed
9+
- Throws error when query fails with a 200 status code and html error message (query = `the meaning of life or 4=2`), see #90.
10+
- Raises a specific "record not found" error instead of a generic "bad request" error when a record cannot be retrieved, see #88.
11+
712
## [1.0.7] - 2018-12-05
813
### Changed
914
- logger dependency removed since it's been part of the standard library.
@@ -92,6 +97,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
9297
### Added
9398
- Adds KW (keywords) and SH (subject heading) to solr search fields
9499

100+
[1.0.8]: https://github.com/ebsco/edsapi-ruby/compare/1.0.7...1.0.8
95101
[1.0.7]: https://github.com/ebsco/edsapi-ruby/compare/1.0.6...1.0.7
96102
[1.0.6]: https://github.com/ebsco/edsapi-ruby/compare/1.0.5...1.0.6
97103
[1.0.5]: https://github.com/ebsco/edsapi-ruby/compare/1.0.4...1.0.5

ebsco-eds.gemspec

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,24 @@ Gem::Specification.new do |spec|
3636
spec.add_dependency 'faraday_middleware', '~> 0.11'
3737
spec.add_dependency 'dotenv', '~> 2.2'
3838
spec.add_dependency 'climate_control', '~> 0'
39-
spec.add_dependency 'require_all', '~> 1.3'
40-
spec.add_dependency 'bibtex-ruby', '~> 4.0'
39+
spec.add_dependency 'require_all', '~> 2.0'
40+
spec.add_dependency 'bibtex-ruby', '~> 5.0', '>= 5.0.0'
4141
spec.add_dependency 'citeproc', '>= 1.0.4', '< 2.0'
4242
spec.add_dependency 'csl', '~> 1.4'
4343
spec.add_dependency 'citeproc-ruby', '~> 1.0', '>= 1.0.2'
4444
spec.add_dependency 'csl-styles', '~> 1.0', '>= 1.0.1.5'
45-
spec.add_dependency 'activesupport', '~> 5.0'
46-
spec.add_dependency 'net-http-persistent', '~> 2.9'
47-
spec.add_dependency 'sanitize', '~> 4.5', '>= 4.5.0'
45+
spec.add_dependency 'activesupport', '~> 5.2'
46+
spec.add_dependency 'net-http-persistent', '~> 3.1'
47+
spec.add_dependency 'sanitize', '~> 5.0'
48+
spec.add_dependency 'public_suffix', '~>4.0'
4849

4950
spec.add_development_dependency 'bundler', '~> 1.13'
50-
spec.add_development_dependency 'rake', '~> 12.0'
51+
spec.add_development_dependency 'rake', '~> 12.3'
5152
spec.add_development_dependency 'minitest', '~> 5.0'
52-
spec.add_development_dependency 'simplecov', '~> 0'
53+
spec.add_development_dependency 'simplecov', '~> 0.17.0'
5354
spec.add_development_dependency 'codecov', '~> 0.1'
54-
spec.add_development_dependency 'vcr', '~> 2.8'
55-
spec.add_development_dependency 'minitest-vcr', '~> 0.1.1'
56-
spec.add_development_dependency 'webmock', '~> 1.17'
55+
spec.add_development_dependency 'vcr', '~> 5.0', '>= 5.0.0'
56+
spec.add_development_dependency 'minitest-vcr', '~> 1.4', '>= 1.4.0'
57+
spec.add_development_dependency 'webmock', '~> 3.6'
5758

5859
end

lib/ebsco/eds/record.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,12 @@ def html_decode_and_sanitize(data, config = nil)
961961
html = html.gsub(/\\n /, '')
962962
end
963963

964-
Sanitize.fragment(html, sanitize_config)
964+
sanitized_html = Sanitize.fragment(html, sanitize_config)
965+
# sanitize 5.0 fails to restore element case after doing lowercase
966+
sanitized_html = sanitized_html.gsub(/<searchlink/, '<searchLink')
967+
sanitized_html = sanitized_html.gsub(/<\/searchlink>/, '</searchLink>')
968+
sanitized_html
969+
965970
end
966971

967972
# dynamically add item metadata as 'eds_extra_ItemNameOrLabel'

lib/ebsco/eds/results.rb

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -32,73 +32,81 @@ class Results
3232
def initialize(search_results, eds_config = {}, additional_limiters = {}, options = {})
3333

3434
@results = search_results
35-
@limiters = additional_limiters
36-
@raw_options = options
37-
38-
# titleize facets?
39-
(ENV.has_key? 'EDS_TITLEIZE_FACETS') ?
40-
if %w(y Y yes Yes true True).include?(ENV['EDS_TITLEIZE_FACETS'])
41-
@titleize_facets_on = true
42-
else
43-
@titleize_facets_on = false
44-
end :
45-
@titleize_facets_on = eds_config[:titleize_facets]
46-
47-
# convert all results to a list of records
48-
@records = []
49-
if @results['SearchResult']['Data']['Records']
50-
@results['SearchResult']['Data']['Records'].each { |record|
51-
52-
@records.push(EBSCO::EDS::Record.new(record, eds_config))
53-
54-
# # records hidden in guest mode
55-
# if record['Header']['AccessLevel']
56-
# if record['Header']['AccessLevel'].to_i > 1
57-
# @records.push(EBSCO::EDS::Record.new(record))
58-
# else
59-
# @records.push(EBSCO::EDS::Record.new(record))
60-
# end
61-
# else
62-
# @records.push(EBSCO::EDS::Record.new(record))
63-
# end
6435

65-
}
66-
end
36+
if @results.is_a?(Hash)
37+
38+
@limiters = additional_limiters
39+
@raw_options = options
40+
41+
# titleize facets?
42+
(ENV.has_key? 'EDS_TITLEIZE_FACETS') ?
43+
if %w(y Y yes Yes true True).include?(ENV['EDS_TITLEIZE_FACETS'])
44+
@titleize_facets_on = true
45+
else
46+
@titleize_facets_on = false
47+
end :
48+
@titleize_facets_on = eds_config[:titleize_facets]
49+
50+
# convert all results to a list of records
51+
@records = []
52+
if @results['SearchResult']['Data']['Records']
53+
@results['SearchResult']['Data']['Records'].each { |record|
54+
55+
@records.push(EBSCO::EDS::Record.new(record, eds_config))
56+
57+
# # records hidden in guest mode
58+
# if record['Header']['AccessLevel']
59+
# if record['Header']['AccessLevel'].to_i > 1
60+
# @records.push(EBSCO::EDS::Record.new(record))
61+
# else
62+
# @records.push(EBSCO::EDS::Record.new(record))
63+
# end
64+
# else
65+
# @records.push(EBSCO::EDS::Record.new(record))
66+
# end
67+
68+
}
69+
end
6770

68-
# create a special list of research starter records
69-
@research_starters = []
70-
_related_records = @results.fetch('SearchResult',{}).fetch('RelatedContent',{}).fetch('RelatedRecords',{})
71-
if _related_records.count > 0
72-
_related_records.each do |related_item|
73-
if related_item['Type'] == 'rs'
74-
rs_entries = related_item.fetch('Records',{})
75-
if rs_entries.count > 0
76-
rs_entries.each do |rs_record|
77-
@research_starters.push(EBSCO::EDS::Record.new(rs_record, eds_config))
71+
# create a special list of research starter records
72+
@research_starters = []
73+
_related_records = @results.fetch('SearchResult',{}).fetch('RelatedContent',{}).fetch('RelatedRecords',{})
74+
if _related_records.count > 0
75+
_related_records.each do |related_item|
76+
if related_item['Type'] == 'rs'
77+
rs_entries = related_item.fetch('Records',{})
78+
if rs_entries.count > 0
79+
rs_entries.each do |rs_record|
80+
@research_starters.push(EBSCO::EDS::Record.new(rs_record, eds_config))
81+
end
7882
end
7983
end
8084
end
8185
end
82-
end
8386

84-
# create a special list of exact match publications
85-
@publication_match = []
86-
_related_publications = @results.fetch('SearchResult',{}).fetch('RelatedContent',{}).fetch('RelatedPublications',{})
87-
if _related_publications.count > 0
88-
_related_publications.each do |related_item|
89-
if related_item['Type'] == 'emp'
90-
_publication_matches = related_item.fetch('PublicationRecords',{})
91-
if _publication_matches.count > 0
92-
_publication_matches.each do |publication_record|
93-
@publication_match.push(EBSCO::EDS::Record.new(publication_record, eds_config))
87+
# create a special list of exact match publications
88+
@publication_match = []
89+
_related_publications = @results.fetch('SearchResult',{}).fetch('RelatedContent',{}).fetch('RelatedPublications',{})
90+
if _related_publications.count > 0
91+
_related_publications.each do |related_item|
92+
if related_item['Type'] == 'emp'
93+
_publication_matches = related_item.fetch('PublicationRecords',{})
94+
if _publication_matches.count > 0
95+
_publication_matches.each do |publication_record|
96+
@publication_match.push(EBSCO::EDS::Record.new(publication_record, eds_config))
97+
end
9498
end
9599
end
96100
end
97101
end
98-
end
99102

100-
# titleize facets
101-
@titleize_facets = %w[Language Journal SubjectEDS SubjectGeographic Publisher]
103+
# titleize facets
104+
@titleize_facets = %w[Language Journal SubjectEDS SubjectGeographic Publisher]
105+
106+
else
107+
# response isn't a hash (eg, html error page)
108+
raise EBSCO::EDS::ApiError, 'EBSCO API error: Query failed.'
109+
end
102110

103111
end
104112

lib/ebsco/eds/session.rb

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,24 @@ def get_citation_exports(dbid:, an:, format: 'all')
340340
citation_exports_params = "?an=#{an}&dbid=#{dbid}&format=#{format}"
341341
citation_exports_response = do_request(:get, path: @config[:citation_exports_url] + citation_exports_params)
342342
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: citation_exports_response, eds_config: @config)
343-
rescue EBSCO::EDS::BadRequest => e
344-
custom_error_message = JSON.parse e.message.gsub('=>', ':')
345-
# ErrorNumber 112 - Invalid Argument Value
346-
# ErrorNumber 132 - Record not found
347-
if custom_error_message['ErrorNumber'] == '112'
348-
unknown_export_format = {"Format"=>format, "Label"=>"", "Data"=>"", "Error"=>"Invalid citation export format"}
349-
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: unknown_export_format, eds_config: @config)
350-
elsif custom_error_message['ErrorNumber'] == '132'
351-
record_not_found = {"Format"=>format, "Label"=>"", "Data"=>"", "Error"=>"Record not found"}
352-
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: record_not_found, eds_config: @config)
353-
else
354-
unknown_error = {"Format"=>format, "Label"=>"", "Data"=>"", "Error"=>custom_error_message['ErrorDescription']}
355-
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: unknown_error, eds_config: @config)
343+
rescue EBSCO::EDS::NotFound => e
344+
custom_error_message = JSON.parse e.message.gsub('=>', ':')
345+
# ErrorNumber 132 - Record not found
346+
if custom_error_message['ErrorNumber'] == '132'
347+
record_not_found = {"Format"=>format, "Label"=>"", "Data"=>"", "Error"=>"Record not found"}
348+
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: record_not_found, eds_config: @config)
349+
end
350+
rescue EBSCO::EDS::BadRequest => e
351+
custom_error_message = JSON.parse e.message.gsub('=>', ':')
352+
# ErrorNumber 112 - Invalid Argument Value
353+
if custom_error_message['ErrorNumber'] == '112'
354+
unknown_export_format = {"Format"=>format, "Label"=>"", "Data"=>"", "Error"=>"Invalid citation export format"}
355+
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: unknown_export_format, eds_config: @config)
356+
else
357+
unknown_error = {"Format"=>format, "Label"=>"", "Data"=>"", "Error"=>custom_error_message['ErrorDescription']}
358+
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: unknown_error, eds_config: @config)
359+
end
356360
end
357-
end
358361
end
359362

360363
# fetch the citation from the citation rest endpoint
@@ -363,6 +366,13 @@ def get_citation_styles(dbid:, an:, format: 'all')
363366
citation_styles_params = "?an=#{an}&dbid=#{dbid}&styles=#{format}"
364367
citation_styles_response = do_request(:get, path: @config[:citation_styles_url] + citation_styles_params)
365368
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: citation_styles_response, eds_config: @config)
369+
rescue EBSCO::EDS::NotFound => e
370+
custom_error_message = JSON.parse e.message.gsub('=>', ':')
371+
# ErrorNumber 132 - Record not found
372+
if custom_error_message['ErrorNumber'] == '132'
373+
record_not_found = {"Format"=>format, "Label"=>"", "Data"=>"", "Error"=>"Record not found"}
374+
EBSCO::EDS::Citations.new(dbid: dbid, an: an, citation_result: record_not_found, eds_config: @config)
375+
end
366376
rescue EBSCO::EDS::BadRequest => e
367377
custom_error_message = JSON.parse e.message.gsub('=>', ':')
368378
unknown_error = {"Id"=>format, "Label"=>"", "Data"=>"", "Error"=>custom_error_message['ErrorDescription']}

lib/ebsco/eds/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module EBSCO
22
module EDS
3-
VERSION = '1.0.7'
3+
VERSION = '1.0.8'
44
end
55
end

lib/faraday/eds_exception_middleware.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ def call(env)
1414
case response.status
1515
when 200
1616
when 400
17-
raise EBSCO::EDS::BadRequest.new(error_message(response))
18-
# when 401
19-
# raise EBSCO::EDS::Unauthorized.new
20-
# when 403
21-
# raise EBSCO::EDS::Forbidden.new
22-
# when 404
23-
# raise EBSCO::EDS::NotFound.new
24-
# when 429
25-
# raise EBSCO::EDS::TooManyRequests.new
17+
if response.body['ErrorNumber'] == '132'
18+
raise EBSCO::EDS::NotFound.new(error_message(response))
19+
else
20+
raise EBSCO::EDS::BadRequest.new(error_message(response))
21+
end
22+
# when 401
23+
# raise EBSCO::EDS::Unauthorized.new
24+
# when 403
25+
# raise EBSCO::EDS::Forbidden.new
26+
# when 404
27+
# raise EBSCO::EDS::NotFound.new
28+
# when 429
29+
# raise EBSCO::EDS::TooManyRequests.new
2630
when 500
2731
raise EBSCO::EDS::InternalServerError.new
2832
when 503

test/actions_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
class EdsApiTests < Minitest::Test
44

55
def test_available_actions
6-
VCR.use_cassette('actions_test/profile_1/test_available_actions') do
6+
VCR.use_cassette('actions_test/profile_1/test_available_actions', :allow_playback_repeats => true) do
77
session = EBSCO::EDS::Session.new({use_cache: false, profile: 'eds-api'})
88
refute_nil session.info.available_actions
99
session.end
1010
end
1111
end
1212

1313
def test_add_single_action
14-
VCR.use_cassette('actions_test/profile_1/test_add_single_action', :record => :new_episodes) do
15-
session = EBSCO::EDS::Session.new({use_cache: false, profile: 'eds-api'})
14+
VCR.use_cassette('actions_test/profile_1/test_add_single_action') do
15+
session = EBSCO::EDS::Session.new({use_cache: false, profile: 'edsapi'})
1616
results = session.search({query: 'earthquake'})
1717
results2 = session.add_actions('addfacetfilter(SourceType:Academic Journals,SubjectEDS:earthquakes)')
1818
assert results.stat_total_hits > results2.stat_total_hits
@@ -22,8 +22,8 @@ def test_add_single_action
2222
end
2323

2424
def test_add_multiple_actions
25-
VCR.use_cassette('actions_test/profile_1/test_add_multiple_actions', :record => :new_episodes) do
26-
session = EBSCO::EDS::Session.new({use_cache: false, profile: 'eds-api'})
25+
VCR.use_cassette('actions_test/profile_1/test_add_multiple_actions') do
26+
session = EBSCO::EDS::Session.new({use_cache: false, profile: 'edsapi'})
2727
results = session.search({query: 'patriots', results_per_page: 1})
2828
results2 = session.add_actions(['addfacetfilter(SubjectGeographic:massachusetts)', 'addlimiter(LA99:English)'])
2929
assert results.stat_total_hits > results2.stat_total_hits
@@ -32,8 +32,8 @@ def test_add_multiple_actions
3232
end
3333

3434
def test_add_unknown_action
35-
VCR.use_cassette('actions_test/profile_1/test_add_unknown_action', :record => :new_episodes) do
36-
session = EBSCO::EDS::Session.new({use_cache: false, profile: 'eds-api'})
35+
VCR.use_cassette('actions_test/profile_1/test_add_unknown_action') do
36+
session = EBSCO::EDS::Session.new({use_cache: false, profile: 'edsapi'})
3737
results = session.search({query: 'patriots', results_per_page: 1})
3838
assert results.stat_total_hits > 0
3939
results2 = session.add_actions('addfacetfilter(Bogus:massachusetts)')

0 commit comments

Comments
 (0)