Skip to content

Commit

Permalink
Trap for a nil pointer and give a helpful message
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 7, 2019
1 parent 93c01d7 commit 1e70e14
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/services/public_xml_service.rb
Expand Up @@ -104,6 +104,13 @@ def add_data_from_src(external_file)
# locate and extract the resourceId/fileId elements
doc = src_item.contentMetadata.ng_xml
src_resource = doc.at_xpath("//resource[@id=\"#{src_resource_id}\"]")

unless src_resource
raise Dor::DataError, "The contentMetadata of #{object.pid} has an exernalFile "\
"reference to #{src_druid}, #{src_resource_id}, but #{src_druid} doesn't have " \
"a matching resource node in it's contentMetadata"
end

src_file = src_resource.at_xpath("file[@id=\"#{src_file_id}\"]")
raise Dor::DataError, "Unable to find a file node with id=\"#{src_file_id}\" (child of #{object.pid})" unless src_file

Expand Down
30 changes: 30 additions & 0 deletions spec/services/public_xml_service_spec.rb
Expand Up @@ -216,6 +216,36 @@
expect(ng_xml.at_xpath('/publicObject/thumb').to_xml).to be_equivalent_to('<thumb>jw923xn5254/2542B.jp2</thumb>')
end

context 'when the referenced object does not have the referenced resource' do
let(:cover_item) { instance_double(Dor::Item, pid: 'druid:cg767mn6478', contentMetadata: contentMetadata) }
let(:contentMetadata) { instance_double(Dor::ContentMetadataDS, ng_xml: Nokogiri::XML(cm_xml)) }
let(:cm_xml) do
<<-EOXML
<contentMetadata objectId="cg767mn6478" type="map">
</contentMetadata>
EOXML
end

before do
item.contentMetadata.content = <<-EOXML
<contentMetadata objectId="hj097bm8879" type="map">
<resource id="hj097bm8879_1" sequence="1" type="image">
<externalFile fileId="2542A.jp2" objectId="druid:cg767mn6478" resourceId="cg767mn6478_1" />
<relationship objectId="druid:cg767mn6478" type="alsoAvailableAs"/>
</resource>
</contentMetadata>
EOXML

allow(Dor).to receive(:find).with(cover_item.pid).and_return(cover_item)
end

it 'raises an error' do
expect { xml }.to raise_error(Dor::DataError, 'The contentMetadata of druid:ab123cd4567 has an exernalFile ' \
"reference to druid:cg767mn6478, cg767mn6478_1, but druid:cg767mn6478 doesn't have " \
"a matching resource node in it's contentMetadata")
end
end

context 'when the referenced object does not have the referenced image' do
let(:cover_item) { instance_double(Dor::Item, pid: 'druid:cg767mn6478', contentMetadata: contentMetadata) }
let(:contentMetadata) { instance_double(Dor::ContentMetadataDS, ng_xml: Nokogiri::XML(cm_xml)) }
Expand Down

0 comments on commit 1e70e14

Please sign in to comment.