From 92ba0831fdd79409deefa372a7687cc82dd8fbf4 Mon Sep 17 00:00:00 2001 From: Samuel Sciolla Date: Tue, 23 Apr 2024 12:27:42 -0400 Subject: [PATCH] Specify expected errors in BagCourier.deliver rescue; add MultipartUploadError to rescue in AwsS3RemoteClient.send_file and related test --- lib/bag_courier.rb | 2 +- lib/remote_client.rb | 6 +++--- test/test_remote_client.rb | 22 ++++++++++++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/bag_courier.rb b/lib/bag_courier.rb index a67895e..d9cebcf 100644 --- a/lib/bag_courier.rb +++ b/lib/bag_courier.rb @@ -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}") diff --git a/lib/remote_client.rb b/lib/remote_client.rb index 7ae95a8..b53d443 100644 --- a/lib/remote_client.rb +++ b/lib/remote_client.rb @@ -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:) @@ -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) diff --git a/test/test_remote_client.rb b/test/test_remote_client.rb index 32e0fb2..c1331ff 100644 --- a/test/test_remote_client.rb +++ b/test/test_remote_client.rb @@ -1,5 +1,3 @@ -require "logger" - require "bundler/setup" require "aws-sdk-s3" @@ -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/"