Skip to content

Commit

Permalink
Provide error handling for when the directory can't be deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Nov 1, 2019
1 parent 4aa52f5 commit d5b3ed1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/controllers/workspaces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def destroy
head :no_content
rescue Errno::ENOENT => e
render build_error('Unable to remove directory', e)
rescue Errno::ENOTEMPTY => e
render build_error('Unable to remove directory', e)
end

# Once an object has been transferred to preservation clean up the workspace.
Expand Down
1 change: 1 addition & 0 deletions app/services/cleanup_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def self.cleanup_by_druid(druid)
# @param [String] druid The identifier for the object whose data is to be removed
# @param [String] base The base directory to delete from
# @return [void] remove the object's data files from the workspace area
# @raises [Errno::ENOTEMPTY] if the directory is not empty
def self.cleanup_workspace_content(druid, base)
PruneService.new(druid: DruidTools::Druid.new(druid, base)).prune!
end
Expand Down
1 change: 1 addition & 0 deletions app/services/prune_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(druid:)
@druid = druid
end

# @raises [Errno::ENOTEMPTY] if the directory is not empty
def prune!
this_path = druid.pathname
parent = this_path.parent
Expand Down
19 changes: 18 additions & 1 deletion spec/requests/cleanup_workspace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end
end

context 'when an error is raised' do
context "when the directory doesn't exist" do
before do
allow(CleanupService).to receive(:cleanup_by_druid)
.and_raise(Errno::ENOENT, 'dir_s_rmdir - /dor/workspace/aa/222')
Expand All @@ -34,4 +34,21 @@
)
end
end

context 'when the directory is not empty' do
before do
allow(CleanupService).to receive(:cleanup_by_druid)
.and_raise(Errno::ENOTEMPTY, 'dir_s_rmdir - /dor/assembly/pw/569/pw/4290')
end

it 'returns JSON-API error' do
delete "/v1/objects/#{object_id}/workspace",
headers: { 'Authorization' => "Bearer #{jwt}" }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.body).to eq(
'{"errors":[{"status":"422","title":"Unable to remove directory",' \
'"detail":"Directory not empty - dir_s_rmdir - /dor/assembly/pw/569/pw/4290"}]}'
)
end
end
end

0 comments on commit d5b3ed1

Please sign in to comment.