Skip to content

A platform for community discussion. Free, open, simple.

FIX: Log S3 asset deletion errors instead of raising

Jake Goldsboroughd1be2da6ba02
diff --git a/lib/s3_helper.rb b/lib/s3_helper.rb
index 2bf36fe3384..9f38b6f366a 100644
--- a/lib/s3_helper.rb
+++ b/lib/s3_helper.rb
@@ -110,7 +110,19 @@ class S3Helper
end
def delete_objects(keys)
- s3_bucket.delete_objects({ delete: { objects: keys.map { |k| { key: k } }, quiet: true } })
+ return if keys.empty?
+
+ response =
+ s3_bucket.delete_objects({ delete: { objects: keys.map { |k| { key: k } }, quiet: false } })
+
+ if response.errors.any?
+ error_codes = response.errors.map(&:code).tally.map { |code, n| "#{code} (#{n})" }.join(", ")
+ sample = response.errors.first(5).map { |err| " #{err.key}: #{err.code} - #{err.message}" }
+ sample << " ... and #{response.errors.size - 5} more" if response.errors.size > 5
+ raise "Failed to delete #{response.errors.size} S3 objects: #{error_codes}\n#{sample.join("\n")}"
+ end
+
+ response
end
def copy(source, destination, options: {})
diff --git a/spec/lib/s3_helper_spec.rb b/spec/lib/s3_helper_spec.rb
index 6672e3e0f0d..c065853c258 100644
--- a/spec/lib/s3_helper_spec.rb
+++ b/spec/lib/s3_helper_spec.rb
@@ -247,6 +247,54 @@ RSpec.describe "S3Helper" do
# If the request were invalid, this spec would raise an error
s3_helper.delete_objects(%w[object/one.txt object/two.txt])
end
+
+ it "does nothing when given empty array" do
+ expect { s3_helper.delete_objects([]) }.not_to raise_error
+ end
+
+ it "raises error with summary and sample when deletions fail" do
+ client.stub_responses(
+ :delete_objects,
+ {
+ deleted: [{ key: "object/one.txt" }],
+ errors: [{ key: "object/two.txt", code: "AccessDenied", message: "Access Denied" }],
+ },
+ )
+
+ expect { s3_helper.delete_objects(%w[object/one.txt object/two.txt]) }.to raise_error(
+ RuntimeError,
+ ) do |error|
+ expect(error.message).to include("Failed to delete 1 S3 objects: AccessDenied (1)")
+ expect(error.message).to include("object/two.txt: AccessDenied - Access Denied")
+ end
+ end
+
+ it "tallies error codes and caps sample at 5" do
+ client.stub_responses(
+ :delete_objects,
+ {
+ deleted: [],
+ errors: [
+ { key: "object/one.txt", code: "AccessDenied", message: "Access Denied" },
+ {
+ key: "object/two.txt",
+ code: "NoSuchKey",
+ message: "The specified key does not exist",
+ },
+ ],
+ },
+ )
+
+ expect { s3_helper.delete_objects(%w[object/one.txt object/two.txt]) }.to raise_error(
+ RuntimeError,
+ ) do |error|
+ expect(error.message).to include("Failed to delete 2 S3 objects")
+ expect(error.message).to include("AccessDenied (1)")
+ expect(error.message).to include("NoSuchKey (1)")
+ expect(error.message).to include("object/one.txt: AccessDenied")
+ expect(error.message).to include("object/two.txt: NoSuchKey")
+ end
+ end
end
describe "#presigned_url" do