Skip to content

Commit

Permalink
return useful error info when embargo date format is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
JackBlackLight committed Sep 13, 2023
1 parent ed45c3c commit 38f81bd
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/controllers/solr_documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def update
location_url = aggregator ? solr_document_url(solr_doc['cul_doi_ssi']) : content_download_url(solr_doc['cul_doi_ssi'])
response.headers['Location'] = location_url
render status: :ok, plain: ''
rescue AcademicCommons::Exceptions::DescriptiveMetadataValidationError => e
render status: :bad_request, json: { error_message: e.message }
rescue ActiveFedora::ObjectNotFoundError => e
render status: :not_found, plain: ''
end
Expand Down
23 changes: 14 additions & 9 deletions app/models/concerns/academic_commons/desc_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ def index_descmetadata(solr_doc = {})

add_field.call 'related_url_ssi', mods.at_css('location > url')

# EMBARGO RELEASE DATE
if (free_to_read = mods.at_css('> extension > free_to_read'))
start_date = free_to_read.attribute('start_date').to_s
if start_date.present?
begin
formatted_date_string = Time.zone.local(*start_date.split('-')).utc.iso8601
rescue ArgumentError
raise AcademicCommons::Exceptions::DescriptiveMetadataValidationError, 'There was an error parsing the embargo release date field.'
end
add_field.call 'free_to_read_start_date_ssi', start_date
add_field.call 'free_to_read_start_date_dtsi', formatted_date_string
end
end

# TITLE
# Note: We are intentionally including the non-sort portion in the title_sort field to
# preserve existing app behavior. This is what the AC app owners want.
Expand Down Expand Up @@ -305,15 +319,6 @@ def index_descmetadata(solr_doc = {})
add_field.call 'suggest', dep
end

# EMBARGO RELEASE DATE
if (free_to_read = mods.at_css('> extension > free_to_read'))
start_date = free_to_read.attribute('start_date').to_s
if start_date.present?
add_field.call 'free_to_read_start_date_ssi', start_date
add_field.call 'free_to_read_start_date_dtsi', Time.zone.local(*start_date.split('-')).utc.iso8601
end
end

# DEGREE INFO
if (degree = mods.at_css('> extension > degree'))
add_field.call 'degree_name_ssim', degree.at_css('name')
Expand Down
9 changes: 9 additions & 0 deletions lib/academic_commons/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module AcademicCommons
module Exceptions
class AcademicCommonsError < StandardError; end

class DescriptiveMetadataValidationError < AcademicCommonsError; end
end
end
18 changes: 18 additions & 0 deletions spec/controllers/solr_documents_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@
expect(subject).to be 200
end
end
context 'descriptive metadata does not validate' do
let(:mock_object) do
ContentAggregator.new
end
let(:params) { { id: 'good:id' } }
before do
allow(ActiveFedora::Base).to receive(:find).with('good:id').and_return(mock_object)
allow(mock_object).to receive(:pid).and_return('good:id')
allow(mock_object).to receive(:to_solr).and_raise(
AcademicCommons::Exceptions::DescriptiveMetadataValidationError,
'There was an error parsing the embargo release date field.'
)
end
it do
expect(subject).to be 400
expect(response.body['error_message']).not_to be_empty
end
end
end
end

Expand Down
68 changes: 68 additions & 0 deletions spec/fixtures/desc_metadata/error_mods.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="ISO-8859-1"?>

<mods:mods xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mods="http://www.loc.gov/mods/v3" version="3.6" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-6.xsd">
<mods:titleInfo>
<mods:title>Test Dissertation</mods:title>
</mods:titleInfo>
<mods:abstract>This dissertation focuses on the testing of a dissertation record.</mods:abstract>
<mods:relatedItem type="host" displayLabel="Project">
<mods:titleInfo>
<mods:title>Academic Commons</mods:title>
</mods:titleInfo>
</mods:relatedItem>
<mods:name type="personal" ID="ad31453" valueURI="temp:fc669aef5f12ea515ae82b75f11b77b8fe24c20f067c8c790943f5acafeceb3b">
<mods:namePart>Doe, Ashley</mods:namePart>
<mods:role>
<mods:roleTerm type="text" valueURI="http://id.loc.gov/vocabulary/relators/aut" authority="marcrelator">Author</mods:roleTerm>
</mods:role>
</mods:name>
<mods:name type="personal" ID="as000" valueURI="temp:fb4e2d1fce9d57eb34473a9092805b1835425d9dca3e6eaedef3ad688e4fb740">
<mods:namePart>Smith, Alan</mods:namePart>
<mods:role>
<mods:roleTerm type="text" valueURI="http://id.loc.gov/vocabulary/relators/ths" authority="marcrelator">Thesis advisor</mods:roleTerm>
</mods:role>
</mods:name>
<mods:name type="corporate" valueURI="temp:ea1cc077df24fbc74a96019811e6546692848b85a63cf6189c3d45b7bbdeaf17">
<mods:namePart>Columbia University. English and Comparative Literature</mods:namePart>
<mods:role>
<mods:roleTerm type="text" valueURI="http://id.loc.gov/vocabulary/relators/org" authority="marcrelator">Originator</mods:roleTerm>
</mods:role>
</mods:name>
<mods:genre type="text" valueURI="http://vocab.getty.edu/aat/300028028">Thesis</mods:genre>
<mods:subject valueURI="http://id.worldcat.org/fast/911989">
<mods:topic>English literature</mods:topic>
</mods:subject>
<mods:subject authority="fast" valueURI="http://id.worldcat.org/fast/1058561">
<mods:topic>Persona (Literature)</mods:topic>
</mods:subject>
<mods:subject authority="fast" valueURI="http://id.worldcat.org/fast/972484">
<mods:topic>Influence (Literary, artistic, etc.)</mods:topic>
</mods:subject>
<mods:language>
<mods:languageTerm type="text" valueURI="http://id.loc.gov/vocabulary/iso639-2/eng" authority="iso639-2b">English</mods:languageTerm>
</mods:language>
<mods:typeOfResource>text</mods:typeOfResource>
<mods:originInfo>
<mods:dateIssued encoding="w3cdtf">2015</mods:dateIssued>
</mods:originInfo>
<mods:recordInfo>
<mods:recordCreationDate encoding="w3cdtf">2016-09-14T16:31:33Z</mods:recordCreationDate>
<mods:recordChangeDate encoding="w3cdtf">2016-09-14T16:48:05Z</mods:recordChangeDate>
<mods:languageOfCataloging>
<mods:languageTerm valueUri="http://id.loc.gov/vocabulary/iso639-2/eng" authority="iso639-2b">eng</mods:languageTerm>
</mods:languageOfCataloging>
</mods:recordInfo>
<mods:identifier type="Hypatia ID">1963542</mods:identifier>
<mods:identifier type="DOI">10.7916/TEST123</mods:identifier>
<mods:extension xmlns:etdms="http://www.ndltd.org/standards/metadata/etdms/1.1/" xsi:schemaLocation="http://www.ndltd.org/standards/metadata/etdms/1.1/">
<etdms:degree>
<etdms:name>Ph.D.</etdms:name>
<etdms:level>2</etdms:level>
<etdms:discipline>English and Comparative Literature</etdms:discipline>
<etdms:grantor>Columbia University</etdms:grantor>
</etdms:degree>
</mods:extension>
<mods:extension xmlns:ali="http://www.niso.org/schemas/ali/1.0/" xsi:schemaLocation="http://www.niso.org/schemas/ali/1.0/ali.xsd">
<ali:free_to_read start_date="-"/>
</mods:extension>
</mods:mods>
9 changes: 9 additions & 0 deletions spec/models/concerns/academic_commons/desc_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def pid; end
expect { indexable.index_descmetadata(start_solr_doc) }.to raise_error StandardError
end
end

context 'invalid data submitted' do
let(:start_solr_doc) { { 'doi_ssim' => '10.7916/TESTTEST' } }
let(:mods_fixture) { fixture_to_str('desc_metadata/error_mods.xml') }
it "raises an error" do
expect { indexable.index_descmetadata(start_solr_doc) }
.to raise_error AcademicCommons::Exceptions::DescriptiveMetadataValidationError
end
end
end
end

Expand Down

0 comments on commit 38f81bd

Please sign in to comment.