Skip to content

Commit

Permalink
Specify expected errors in BagCourier.deliver rescue; add MultipartUp…
Browse files Browse the repository at this point in the history
…loadError to rescue in AwsS3RemoteClient.send_file and related test
  • Loading branch information
ssciolla committed Apr 23, 2024
1 parent 3882dc7 commit 92ba083
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/bag_courier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def deliver

deposit(export_tar_file_path)
FileUtils.rm(export_tar_file_path) if @remove_export
rescue => e
rescue RemoteClient::RemoteClientError, TarFileCreator::TarFileCreatorError, BagValidationError => e
note = "failed with error #{e.class}: #{e.full_message}"
track!(status: BagStatus::FAILED, note: note)
logger.error("BagCourier.deliver #{note}")
Expand Down
6 changes: 3 additions & 3 deletions lib/remote_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def send_file(local_file_path:, remote_path: nil)
logger.debug("Sending file \"#{file_name}\" to \"#{remote_path}\"")
aws_object = @bucket.object(object_key)
aws_object.upload_file(local_file_path, progress_callback: UPLOAD_PROGRESS)
rescue Aws::S3::Errors::ServiceError => e
raise RemoteClientError, "Error occurred while uploading file to AWS S3: #{e}"
rescue Aws::S3::MultipartUploadError, Aws::S3::Errors::ServiceError => e
raise RemoteClientError, "Error occurred while uploading file to AWS S3: #{e.full_message}"
end

def retrieve_file(remote_file_path:, local_dir_path:)
Expand All @@ -114,7 +114,7 @@ def retrieve_file(remote_file_path:, local_dir_path:)
aws_object = @bucket.object(remote_file_path)
aws_object.download_file(File.join(local_dir_path, file_name))
rescue Aws::S3::Errors::ServiceError => e
raise RemoteClientError, "Error occurred while downloading file from AWS S3: #{e}"
raise RemoteClientError, "Error occurred while downloading file from AWS S3: #{e.full_message}"
end

def get_files_at_path(remote_path = nil)
Expand Down
22 changes: 20 additions & 2 deletions test/test_remote_client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "logger"

require "bundler/setup"

require "aws-sdk-s3"
Expand Down Expand Up @@ -233,6 +231,26 @@ def test_retrieve_file_with_no_key_error
end
end

def test_send_file_with_multipart_upload_error
raise_error = proc do
raise Aws::S3::MultipartUploadError.new(
"some context", "Your multi-part upload failed, oh no!"
)
end

fake_object = Object.new
fake_object.define_singleton_method(:upload_file) do |path|
"faking it!"
end

@mock_bucket.expect(:object, fake_object, ["file.txt"])
fake_object.stub :upload_file, raise_error do
assert_raises RemoteClient::RemoteClientError do
@client_with_mock.send_file(local_file_path: "/export/file.txt")
end
end
end

def test_retrieve_from_path
remote_path = "/special/"

Expand Down

0 comments on commit 92ba083

Please sign in to comment.