Skip to content

Commit 32600bb

Browse files
committed
Relocate GridFS exception classes and use static constructors
1 parent c3e4774 commit 32600bb

File tree

8 files changed

+107
-47
lines changed

8 files changed

+107
-47
lines changed

src/Exception/GridFSCorruptFileException.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Exception/GridFSFileNotFoundException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/GridFS/Bucket.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use MongoDB\Driver\Manager;
88
use MongoDB\Driver\ReadPreference;
99
use MongoDB\Driver\WriteConcern;
10-
use MongoDB\Exception\GridFSFileNotFoundException;
1110
use MongoDB\Exception\InvalidArgumentException;
11+
use MongoDB\GridFS\Exception\FileNotFoundException;
1212
use MongoDB\Operation\Find;
1313

1414
/**
@@ -84,7 +84,7 @@ public function __construct(Manager $manager, $databaseName, array $options = []
8484
* attempt to delete orphaned chunks.
8585
*
8686
* @param ObjectId $id ObjectId of the file
87-
* @throws GridFSFileNotFoundException
87+
* @throws FileNotFoundException
8888
*/
8989
public function delete(ObjectId $id)
9090
{
@@ -93,7 +93,7 @@ public function delete(ObjectId $id)
9393
$this->collectionsWrapper->getChunksCollection()->deleteMany(['files_id' => $id]);
9494

9595
if ($file === null) {
96-
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
96+
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
9797
}
9898

9999
}
@@ -103,7 +103,7 @@ public function delete(ObjectId $id)
103103
*
104104
* @param ObjectId $id ObjectId of the file
105105
* @param resource $destination Writable Stream
106-
* @throws GridFSFileNotFoundException
106+
* @throws FileNotFoundException
107107
*/
108108
public function downloadToStream(ObjectId $id, $destination)
109109
{
@@ -113,7 +113,7 @@ public function downloadToStream(ObjectId $id, $destination)
113113
);
114114

115115
if ($file === null) {
116-
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
116+
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
117117
}
118118

119119
$gridFsStream = new GridFSDownload($this->collectionsWrapper, $file);
@@ -142,7 +142,7 @@ public function downloadToStream(ObjectId $id, $destination)
142142
* @param string $filename File name
143143
* @param resource $destination Writable Stream
144144
* @param array $options Download options
145-
* @throws GridFSFileNotFoundException
145+
* @throws FileNotFoundException
146146
*/
147147
public function downloadToStreamByName($filename, $destination, array $options = [])
148148
{
@@ -207,7 +207,7 @@ public function getIdFromStream($stream)
207207
*
208208
* @param ObjectId $id ObjectId of the file
209209
* @return resource
210-
* @throws GridFSFileNotFoundException
210+
* @throws FileNotFoundException
211211
*/
212212
public function openDownloadStream(ObjectId $id)
213213
{
@@ -217,7 +217,7 @@ public function openDownloadStream(ObjectId $id)
217217
);
218218

219219
if ($file === null) {
220-
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
220+
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
221221
}
222222

223223
return $this->openDownloadStreamByFile($file);
@@ -245,7 +245,7 @@ public function openDownloadStream(ObjectId $id)
245245
* @param string $filename File name
246246
* @param array $options Download options
247247
* @return resource
248-
* @throws GridFSFileNotFoundException
248+
* @throws FileNotFoundException
249249
*/
250250
public function openDownloadStreamByName($filename, array $options = [])
251251
{
@@ -293,7 +293,7 @@ public function rename(ObjectId $id, $newFilename)
293293
$filesCollection = $this->collectionsWrapper->getFilesCollection();
294294
$result = $filesCollection->updateOne(['_id' => $id], ['$set' => ['filename' => $newFilename]]);
295295
if($result->getModifiedCount() == 0) {
296-
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
296+
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
297297
}
298298
}
299299

@@ -339,7 +339,7 @@ private function findFileRevision($filename, $revision)
339339
);
340340

341341
if ($file === null) {
342-
throw new GridFSFileNotFoundException($filename, $filesCollection->getNameSpace());
342+
throw FileNotFoundException::byFilenameAndRevision($filename, $revision, $filesCollection->getNameSpace());
343343
}
344344

345345
return $file;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace MongoDB\GridFS\Exception;
4+
5+
use MongoDB\Exception\RuntimeException;
6+
7+
class CorruptFileException extends RuntimeException
8+
{
9+
/**
10+
* Thrown when a chunk is not found for an expected index.
11+
*
12+
* @param integer $expectedIndex Expected index number
13+
* @return self
14+
*/
15+
public static function missingChunk($expectedIndex)
16+
{
17+
return new static(sprintf('Chunk not found for index "%d"', $expectedIndex));
18+
}
19+
20+
/**
21+
* Thrown when a chunk has an unexpected index number.
22+
*
23+
* @param integer $index Actual index number (i.e. "n" field)
24+
* @param integer $expectedIndex Expected index number
25+
* @return self
26+
*/
27+
public static function unexpectedIndex($index, $expectedIndex)
28+
{
29+
return new static(sprintf('Expected chunk to have index "%d" but found "%d"', $expectedIndex, $index));
30+
}
31+
32+
/**
33+
* Thrown when a chunk has an unexpected data size.
34+
*
35+
* @param integer $size Actual size (i.e. "data" field length)
36+
* @param integer $expectedSize Expected size
37+
* @return self
38+
*/
39+
public static function unexpectedSize($size, $expectedSize)
40+
{
41+
return new static(sprintf('Expected chunk to have size "%d" but found "%d"', $expectedSize, $size));
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace MongoDB\GridFS\Exception;
4+
5+
use MongoDB\Exception\RuntimeException;
6+
7+
class FileNotFoundException extends RuntimeException
8+
{
9+
/**
10+
* Thrown when a file cannot be found by its filename and revision.
11+
*
12+
* @param string $filename Filename
13+
* @param integer $revision Revision
14+
* @param string $namespace Namespace for the files collection
15+
* @return self
16+
*/
17+
public static function byFilenameAndRevision($filename, $revision, $namespace)
18+
{
19+
return new static(sprintf('File with name "%s" and revision "%d" not found in "%s"', $filename, $revision, $namespace));
20+
}
21+
22+
/**
23+
* Thrown when a file cannot be found by its ID.
24+
*
25+
* @param mixed $id File ID
26+
* @param string $namespace Namespace for the files collection
27+
* @return self
28+
*/
29+
public static function byId($id, $namespace)
30+
{
31+
$json = \MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP(['_id' => $id]));
32+
33+
return new static(sprintf('File "%s" not found in "%s"', $json, $namespace));
34+
}
35+
}

src/GridFS/GridFSDownload.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace MongoDB\GridFS;
44

55
use MongoDB\Driver\Exception\Exception;
6-
use MongoDB\Exception\GridFSCorruptFileException;
6+
use MongoDB\GridFS\Exception\CorruptFileException;
77
use stdClass;
88

99
/**
@@ -30,7 +30,7 @@ class GridFSDownload
3030
*
3131
* @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper
3232
* @param stdClass $file GridFS file document
33-
* @throws GridFSCorruptFileException
33+
* @throws CorruptFileException
3434
*/
3535
public function __construct(GridFSCollectionsWrapper $collectionsWrapper, stdClass $file)
3636
{
@@ -43,8 +43,8 @@ public function __construct(GridFSCollectionsWrapper $collectionsWrapper, stdCla
4343
['sort' => ['n' => 1]]
4444
);
4545
} catch (Exception $e) {
46-
// TODO: Why do we replace a driver exception with GridFSCorruptFileException here?
47-
throw new GridFSCorruptFileException();
46+
// TODO: Why do we replace a driver exception with CorruptFileException here?
47+
throw new CorruptFileException();
4848
}
4949

5050
$this->chunksIterator = new \IteratorIterator($cursor);
@@ -138,11 +138,11 @@ private function advanceChunks()
138138
}
139139

140140
if ( ! $this->chunksIterator->valid()) {
141-
throw new GridFSCorruptFileException();
141+
throw CorruptFileException::missingChunk($this->chunkOffset);
142142
}
143143

144144
if ($this->chunksIterator->current()->n != $this->chunkOffset) {
145-
throw new GridFSCorruptFileException();
145+
throw CorruptFileException::unexpectedIndex($this->chunksIterator->current()->n, $this->chunkOffset);
146146
}
147147

148148
$actualChunkSize = strlen($this->chunksIterator->current()->data->getData());
@@ -152,7 +152,7 @@ private function advanceChunks()
152152
: $this->file->chunkSize;
153153

154154
if ($actualChunkSize != $expectedChunkSize) {
155-
throw new GridFSCorruptFileException();
155+
throw CorruptFileException::unexpectedSize($actualChunkSize, $expectedChunkSize);
156156
}
157157

158158
$this->bytesSeen += $actualChunkSize;

tests/GridFS/BucketFunctionalTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testBasicOperations()
7373
} catch(\MongoDB\Exception\Exception $e) {
7474
$error = $e;
7575
}
76-
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
76+
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
7777
$this->assertTrue($error instanceof $fileNotFound);
7878
$this->assertEquals(0, $this->bucket->getCollectionsWrapper()->getFilesCollection()->count());
7979
$this->assertEquals(0, $this->bucket->getCollectionsWrapper()->getChunksCollection()->count());
@@ -116,7 +116,7 @@ public function testCorruptChunk()
116116
} catch(\MongoDB\Exception\Exception $e) {
117117
$error = $e;
118118
}
119-
$corruptFileError = '\MongoDB\Exception\GridFSCOrruptFileException';
119+
$corruptFileError = '\MongoDB\GridFS\Exception\CorruptFileException';
120120
$this->assertTrue($error instanceof $corruptFileError);
121121
}
122122
public function testErrorsOnMissingChunk()
@@ -131,7 +131,7 @@ public function testErrorsOnMissingChunk()
131131
} catch(\MongoDB\Exception\Exception $e) {
132132
$error = $e;
133133
}
134-
$corruptFileError = '\MongoDB\Exception\GridFSCOrruptFileException';
134+
$corruptFileError = '\MongoDB\GridFS\Exception\CorruptFileException';
135135
$this->assertTrue($error instanceof $corruptFileError);
136136
}
137137
public function testUploadEnsureIndexes()
@@ -177,7 +177,7 @@ public function testGetLastVersion()
177177
} catch(\MongoDB\Exception\Exception $e) {
178178
$error = $e;
179179
}
180-
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
180+
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
181181
$this->assertTrue($error instanceof $fileNotFound);
182182
}
183183
public function testGetVersion()
@@ -194,7 +194,7 @@ public function testGetVersion()
194194
$this->assertEquals("bar", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -2])));
195195
$this->assertEquals("foo", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -3])));
196196

197-
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
197+
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
198198
$error = null;
199199
try{
200200
$this->bucket->openDownloadStreamByName("test", ['revision' => 3]);
@@ -278,7 +278,7 @@ public function testRename()
278278
} catch(\MongoDB\Exception\Exception $e) {
279279
$error = $e;
280280
}
281-
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
281+
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
282282
$this->assertTrue($error instanceof $fileNotFound);
283283

284284
$this->assertEquals("testing", stream_get_contents($this->bucket->openDownloadStreamByName("second_name")));

tests/GridFS/SpecificationTests.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public function testSpecificationTests($testJson)
5252
} catch(\MongoDB\Exception\Exception $e) {
5353
$error = $e;
5454
}
55-
$errors = ['FileNotFound' => '\MongoDB\Exception\GridFSFileNotFoundException',
56-
'ChunkIsMissing' => '\MongoDB\Exception\GridFSCorruptFileException',
57-
'ExtraChunk' => '\MongoDB\Exception\GridFSCorruptFileException',
58-
'ChunkIsWrongSize' => '\MongoDB\Exception\GridFSCorruptFileException',
59-
'RevisionNotFound' => '\MongoDB\Exception\GridFSFileNotFoundException'
55+
$errors = ['FileNotFound' => '\MongoDB\GridFS\Exception\FileNotFoundException',
56+
'ChunkIsMissing' => '\MongoDB\GridFS\Exception\CorruptFileException',
57+
'ExtraChunk' => '\MongoDB\GridFS\Exception\CorruptFileException',
58+
'ChunkIsWrongSize' => '\MongoDB\GridFS\Exception\CorruptFileException',
59+
'RevisionNotFound' => '\MongoDB\GridFS\Exception\FileNotFoundException'
6060
];
6161
if (!isset($test['assert']['error'])) {
6262
$this->assertNull($error);

0 commit comments

Comments
 (0)