Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue versioning exceptions and provide more context #397

Merged
merged 1 commit into from Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/virtual_objects_controller.rb
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaaaaaaas

errors << { parent_id => [e.message] }
end

Expand Down
6 changes: 3 additions & 3 deletions app/services/version_service.rb
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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaaaaaaas


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
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
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