Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit acb73e1

Browse files
committed
PERL-784: If the insert_one into 'fs.files' fails, cleanup chunks and rethrow error.
1 parent d21144c commit acb73e1

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/MongoDB/GridFS.pm

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ our $VERSION = 'v1.7.0';
2525
use MongoDB::GridFS::File;
2626
use Digest::MD5;
2727
use Moo;
28+
use Try::Tiny;
2829
use MongoDB::Error;
2930
use MongoDB::WriteConcern;
3031
use MongoDB::_Types qw(
@@ -400,7 +401,13 @@ sub insert {
400401
$copy{"chunkSize"} = $MongoDB::GridFS::chunk_size;
401402
$copy{"uploadDate"} = DateTime->now;
402403
$copy{"length"} = $length;
403-
return $files->insert_one(\%copy)->inserted_id;
404+
try {
405+
return $files->insert_one(\%copy)->inserted_id;
406+
} catch {
407+
# cleanup and rethrow the error that caused the insert to fail
408+
$chunks->delete_many({files_id => $id});
409+
die $_;
410+
};
404411
}
405412

406413
=method drop

0 commit comments

Comments
 (0)