Skip to content

Commit

Permalink
Merge pull request #397 from sul-dlss/rescue_version_errors
Browse files Browse the repository at this point in the history
Rescue versioning exceptions and provide more context
  • Loading branch information
ndushay committed Sep 11, 2019
2 parents b08ac11 + 87f7457 commit 96003dc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/virtual_objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create
parent_id, child_ids = virtual_object.values_at(:parent_id, :child_ids)
# Update the constituent relationship between the parent and child druids
errors << ConstituentService.new(parent_druid: parent_id).add(child_druids: child_ids)
rescue ActiveFedora::ObjectNotFoundError, Rubydora::FedoraInvalidRequest => e
rescue ActiveFedora::ObjectNotFoundError, Rubydora::FedoraInvalidRequest, Dor::Exception => e
errors << { parent_id => [e.message] }
end

Expand Down
6 changes: 3 additions & 3 deletions app/services/version_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def close(opts = {})
work.versionMetadata.save
end

raise Dor::Exception, 'latest version in versionMetadata requires tag and description before it can be closed' unless work.versionMetadata.current_version_closeable?
raise Dor::Exception, 'Trying to close version on an object not opened for versioning' unless open_for_versioning?
raise Dor::Exception, 'accessionWF already created for versioned object' if accessioning?
raise Dor::Exception, "latest version in versionMetadata for #{work.pid} requires tag and description before it can be closed" unless work.versionMetadata.current_version_closeable?
raise Dor::Exception, "Trying to close version on #{work.pid} which is not opened for versioning" unless open_for_versioning?
raise Dor::Exception, "accessionWF already created for versioned object #{work.pid}" if accessioning?

Dor::Config.workflow.client.close_version 'dor', work.pid, opts.fetch(:start_accession, true) # Default to creating accessionWF when calling close_version
work.events.add_event('close', opts[:user_name], "Version #{work.current_version} closed") if opts[:user_name]
Expand Down
15 changes: 15 additions & 0 deletions spec/requests/batch_create_virtual_objects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@
end
end

context 'when virtual_objects contain objects that raise Dor::Exception' do
before do
allow(service).to receive(:add).and_raise(Dor::Exception, 'versioning is messed up')
end

it 'renders an error' do
post '/v1/virtual_objects',
params: { virtual_objects: [{ parent_id: parent_id, child_ids: [child1_id, child2_id] }] },
headers: { 'X-Auth' => "Bearer #{jwt}" }
expect(service).to have_received(:add).with(child_druids: [child1_id, child2_id])
expect(response).to be_unprocessable
expect(response.body).to eq '{"errors":[{"druid:mk420bs7601":["versioning is messed up"]}]}'
end
end

context 'when virtual_objects contain objects that are not combinable' do
before do
allow(service).to receive(:add).and_return(parent_id => ["Item #{child2_id} is not open for modification"])
Expand Down
6 changes: 3 additions & 3 deletions spec/services/version_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,22 @@
context 'when the object has not been opened for versioning' do
it 'raises an exception' do
expect(Dor::Config.workflow.client).to receive(:active_lifecycle).with('dor', druid, 'opened').and_return(nil)
expect { close }.to raise_error(Dor::Exception, 'Trying to close version on an object not opened for versioning')
expect { close }.to raise_error(Dor::Exception, 'Trying to close version on druid:ab12cd3456 which is not opened for versioning')
end
end

context 'when the object already has an active instance of accesssionWF' do
it 'raises an exception' do
expect(Dor::Config.workflow.client).to receive(:active_lifecycle).with('dor', druid, 'opened').and_return(Time.new)
expect(Dor::Config.workflow.client).to receive(:active_lifecycle).with('dor', druid, 'submitted').and_return(true)
expect { close }.to raise_error(Dor::Exception, 'accessionWF already created for versioned object')
expect { close }.to raise_error(Dor::Exception, 'accessionWF already created for versioned object druid:ab12cd3456')
end
end

context 'when the latest version does not have a tag and a description' do
it 'raises an exception' do
vmd_ds.increment_version
expect { close }.to raise_error(Dor::Exception, 'latest version in versionMetadata requires tag and description before it can be closed')
expect { close }.to raise_error(Dor::Exception, 'latest version in versionMetadata for druid:ab12cd3456 requires tag and description before it can be closed')
end
end
end
Expand Down

0 comments on commit 96003dc

Please sign in to comment.