Commit 32600bb0 authored by Jeremy Mikola's avatar Jeremy Mikola

Relocate GridFS exception classes and use static constructors

parent c3e4774e
<?php
namespace MongoDB\Exception;
class GridFSCorruptFileException extends \MongoDB\Driver\Exception\RuntimeException implements Exception
{
}
<?php
namespace MongoDB\Exception;
class GridFSFileNotFoundException extends \MongoDB\Driver\Exception\RuntimeException implements Exception
{
public function __construct($filename, $namespace)
{
parent::__construct(sprintf('Unable to find file "%s" in namespace "%s"', $filename, $namespace));
}
}
...@@ -7,8 +7,8 @@ use MongoDB\Driver\Cursor; ...@@ -7,8 +7,8 @@ use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\GridFSFileNotFoundException;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\GridFS\Exception\FileNotFoundException;
use MongoDB\Operation\Find; use MongoDB\Operation\Find;
/** /**
...@@ -84,7 +84,7 @@ class Bucket ...@@ -84,7 +84,7 @@ class Bucket
* attempt to delete orphaned chunks. * attempt to delete orphaned chunks.
* *
* @param ObjectId $id ObjectId of the file * @param ObjectId $id ObjectId of the file
* @throws GridFSFileNotFoundException * @throws FileNotFoundException
*/ */
public function delete(ObjectId $id) public function delete(ObjectId $id)
{ {
...@@ -93,7 +93,7 @@ class Bucket ...@@ -93,7 +93,7 @@ class Bucket
$this->collectionsWrapper->getChunksCollection()->deleteMany(['files_id' => $id]); $this->collectionsWrapper->getChunksCollection()->deleteMany(['files_id' => $id]);
if ($file === null) { if ($file === null) {
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace()); throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
} }
} }
...@@ -103,7 +103,7 @@ class Bucket ...@@ -103,7 +103,7 @@ class Bucket
* *
* @param ObjectId $id ObjectId of the file * @param ObjectId $id ObjectId of the file
* @param resource $destination Writable Stream * @param resource $destination Writable Stream
* @throws GridFSFileNotFoundException * @throws FileNotFoundException
*/ */
public function downloadToStream(ObjectId $id, $destination) public function downloadToStream(ObjectId $id, $destination)
{ {
...@@ -113,7 +113,7 @@ class Bucket ...@@ -113,7 +113,7 @@ class Bucket
); );
if ($file === null) { if ($file === null) {
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace()); throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
} }
$gridFsStream = new GridFSDownload($this->collectionsWrapper, $file); $gridFsStream = new GridFSDownload($this->collectionsWrapper, $file);
...@@ -142,7 +142,7 @@ class Bucket ...@@ -142,7 +142,7 @@ class Bucket
* @param string $filename File name * @param string $filename File name
* @param resource $destination Writable Stream * @param resource $destination Writable Stream
* @param array $options Download options * @param array $options Download options
* @throws GridFSFileNotFoundException * @throws FileNotFoundException
*/ */
public function downloadToStreamByName($filename, $destination, array $options = []) public function downloadToStreamByName($filename, $destination, array $options = [])
{ {
...@@ -207,7 +207,7 @@ class Bucket ...@@ -207,7 +207,7 @@ class Bucket
* *
* @param ObjectId $id ObjectId of the file * @param ObjectId $id ObjectId of the file
* @return resource * @return resource
* @throws GridFSFileNotFoundException * @throws FileNotFoundException
*/ */
public function openDownloadStream(ObjectId $id) public function openDownloadStream(ObjectId $id)
{ {
...@@ -217,7 +217,7 @@ class Bucket ...@@ -217,7 +217,7 @@ class Bucket
); );
if ($file === null) { if ($file === null) {
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace()); throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
} }
return $this->openDownloadStreamByFile($file); return $this->openDownloadStreamByFile($file);
...@@ -245,7 +245,7 @@ class Bucket ...@@ -245,7 +245,7 @@ class Bucket
* @param string $filename File name * @param string $filename File name
* @param array $options Download options * @param array $options Download options
* @return resource * @return resource
* @throws GridFSFileNotFoundException * @throws FileNotFoundException
*/ */
public function openDownloadStreamByName($filename, array $options = []) public function openDownloadStreamByName($filename, array $options = [])
{ {
...@@ -293,7 +293,7 @@ class Bucket ...@@ -293,7 +293,7 @@ class Bucket
$filesCollection = $this->collectionsWrapper->getFilesCollection(); $filesCollection = $this->collectionsWrapper->getFilesCollection();
$result = $filesCollection->updateOne(['_id' => $id], ['$set' => ['filename' => $newFilename]]); $result = $filesCollection->updateOne(['_id' => $id], ['$set' => ['filename' => $newFilename]]);
if($result->getModifiedCount() == 0) { if($result->getModifiedCount() == 0) {
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace()); throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
} }
} }
...@@ -339,7 +339,7 @@ class Bucket ...@@ -339,7 +339,7 @@ class Bucket
); );
if ($file === null) { if ($file === null) {
throw new GridFSFileNotFoundException($filename, $filesCollection->getNameSpace()); throw FileNotFoundException::byFilenameAndRevision($filename, $revision, $filesCollection->getNameSpace());
} }
return $file; return $file;
......
<?php
namespace MongoDB\GridFS\Exception;
use MongoDB\Exception\RuntimeException;
class CorruptFileException extends RuntimeException
{
/**
* Thrown when a chunk is not found for an expected index.
*
* @param integer $expectedIndex Expected index number
* @return self
*/
public static function missingChunk($expectedIndex)
{
return new static(sprintf('Chunk not found for index "%d"', $expectedIndex));
}
/**
* Thrown when a chunk has an unexpected index number.
*
* @param integer $index Actual index number (i.e. "n" field)
* @param integer $expectedIndex Expected index number
* @return self
*/
public static function unexpectedIndex($index, $expectedIndex)
{
return new static(sprintf('Expected chunk to have index "%d" but found "%d"', $expectedIndex, $index));
}
/**
* Thrown when a chunk has an unexpected data size.
*
* @param integer $size Actual size (i.e. "data" field length)
* @param integer $expectedSize Expected size
* @return self
*/
public static function unexpectedSize($size, $expectedSize)
{
return new static(sprintf('Expected chunk to have size "%d" but found "%d"', $expectedSize, $size));
}
}
<?php
namespace MongoDB\GridFS\Exception;
use MongoDB\Exception\RuntimeException;
class FileNotFoundException extends RuntimeException
{
/**
* Thrown when a file cannot be found by its filename and revision.
*
* @param string $filename Filename
* @param integer $revision Revision
* @param string $namespace Namespace for the files collection
* @return self
*/
public static function byFilenameAndRevision($filename, $revision, $namespace)
{
return new static(sprintf('File with name "%s" and revision "%d" not found in "%s"', $filename, $revision, $namespace));
}
/**
* Thrown when a file cannot be found by its ID.
*
* @param mixed $id File ID
* @param string $namespace Namespace for the files collection
* @return self
*/
public static function byId($id, $namespace)
{
$json = \MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP(['_id' => $id]));
return new static(sprintf('File "%s" not found in "%s"', $json, $namespace));
}
}
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
namespace MongoDB\GridFS; namespace MongoDB\GridFS;
use MongoDB\Driver\Exception\Exception; use MongoDB\Driver\Exception\Exception;
use MongoDB\Exception\GridFSCorruptFileException; use MongoDB\GridFS\Exception\CorruptFileException;
use stdClass; use stdClass;
/** /**
...@@ -30,7 +30,7 @@ class GridFSDownload ...@@ -30,7 +30,7 @@ class GridFSDownload
* *
* @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper * @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper
* @param stdClass $file GridFS file document * @param stdClass $file GridFS file document
* @throws GridFSCorruptFileException * @throws CorruptFileException
*/ */
public function __construct(GridFSCollectionsWrapper $collectionsWrapper, stdClass $file) public function __construct(GridFSCollectionsWrapper $collectionsWrapper, stdClass $file)
{ {
...@@ -43,8 +43,8 @@ class GridFSDownload ...@@ -43,8 +43,8 @@ class GridFSDownload
['sort' => ['n' => 1]] ['sort' => ['n' => 1]]
); );
} catch (Exception $e) { } catch (Exception $e) {
// TODO: Why do we replace a driver exception with GridFSCorruptFileException here? // TODO: Why do we replace a driver exception with CorruptFileException here?
throw new GridFSCorruptFileException(); throw new CorruptFileException();
} }
$this->chunksIterator = new \IteratorIterator($cursor); $this->chunksIterator = new \IteratorIterator($cursor);
...@@ -138,11 +138,11 @@ class GridFSDownload ...@@ -138,11 +138,11 @@ class GridFSDownload
} }
if ( ! $this->chunksIterator->valid()) { if ( ! $this->chunksIterator->valid()) {
throw new GridFSCorruptFileException(); throw CorruptFileException::missingChunk($this->chunkOffset);
} }
if ($this->chunksIterator->current()->n != $this->chunkOffset) { if ($this->chunksIterator->current()->n != $this->chunkOffset) {
throw new GridFSCorruptFileException(); throw CorruptFileException::unexpectedIndex($this->chunksIterator->current()->n, $this->chunkOffset);
} }
$actualChunkSize = strlen($this->chunksIterator->current()->data->getData()); $actualChunkSize = strlen($this->chunksIterator->current()->data->getData());
...@@ -152,7 +152,7 @@ class GridFSDownload ...@@ -152,7 +152,7 @@ class GridFSDownload
: $this->file->chunkSize; : $this->file->chunkSize;
if ($actualChunkSize != $expectedChunkSize) { if ($actualChunkSize != $expectedChunkSize) {
throw new GridFSCorruptFileException(); throw CorruptFileException::unexpectedSize($actualChunkSize, $expectedChunkSize);
} }
$this->bytesSeen += $actualChunkSize; $this->bytesSeen += $actualChunkSize;
......
...@@ -73,7 +73,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -73,7 +73,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) { } catch(\MongoDB\Exception\Exception $e) {
$error = $e; $error = $e;
} }
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException'; $fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound); $this->assertTrue($error instanceof $fileNotFound);
$this->assertEquals(0, $this->bucket->getCollectionsWrapper()->getFilesCollection()->count()); $this->assertEquals(0, $this->bucket->getCollectionsWrapper()->getFilesCollection()->count());
$this->assertEquals(0, $this->bucket->getCollectionsWrapper()->getChunksCollection()->count()); $this->assertEquals(0, $this->bucket->getCollectionsWrapper()->getChunksCollection()->count());
...@@ -116,7 +116,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -116,7 +116,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) { } catch(\MongoDB\Exception\Exception $e) {
$error = $e; $error = $e;
} }
$corruptFileError = '\MongoDB\Exception\GridFSCOrruptFileException'; $corruptFileError = '\MongoDB\GridFS\Exception\CorruptFileException';
$this->assertTrue($error instanceof $corruptFileError); $this->assertTrue($error instanceof $corruptFileError);
} }
public function testErrorsOnMissingChunk() public function testErrorsOnMissingChunk()
...@@ -131,7 +131,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -131,7 +131,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) { } catch(\MongoDB\Exception\Exception $e) {
$error = $e; $error = $e;
} }
$corruptFileError = '\MongoDB\Exception\GridFSCOrruptFileException'; $corruptFileError = '\MongoDB\GridFS\Exception\CorruptFileException';
$this->assertTrue($error instanceof $corruptFileError); $this->assertTrue($error instanceof $corruptFileError);
} }
public function testUploadEnsureIndexes() public function testUploadEnsureIndexes()
...@@ -177,7 +177,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -177,7 +177,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) { } catch(\MongoDB\Exception\Exception $e) {
$error = $e; $error = $e;
} }
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException'; $fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound); $this->assertTrue($error instanceof $fileNotFound);
} }
public function testGetVersion() public function testGetVersion()
...@@ -194,7 +194,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -194,7 +194,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$this->assertEquals("bar", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -2]))); $this->assertEquals("bar", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -2])));
$this->assertEquals("foo", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -3]))); $this->assertEquals("foo", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -3])));
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException'; $fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$error = null; $error = null;
try{ try{
$this->bucket->openDownloadStreamByName("test", ['revision' => 3]); $this->bucket->openDownloadStreamByName("test", ['revision' => 3]);
...@@ -278,7 +278,7 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -278,7 +278,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) { } catch(\MongoDB\Exception\Exception $e) {
$error = $e; $error = $e;
} }
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException'; $fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound); $this->assertTrue($error instanceof $fileNotFound);
$this->assertEquals("testing", stream_get_contents($this->bucket->openDownloadStreamByName("second_name"))); $this->assertEquals("testing", stream_get_contents($this->bucket->openDownloadStreamByName("second_name")));
......
...@@ -52,11 +52,11 @@ class SpecificationTests extends FunctionalTestCase ...@@ -52,11 +52,11 @@ class SpecificationTests extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) { } catch(\MongoDB\Exception\Exception $e) {
$error = $e; $error = $e;
} }
$errors = ['FileNotFound' => '\MongoDB\Exception\GridFSFileNotFoundException', $errors = ['FileNotFound' => '\MongoDB\GridFS\Exception\FileNotFoundException',
'ChunkIsMissing' => '\MongoDB\Exception\GridFSCorruptFileException', 'ChunkIsMissing' => '\MongoDB\GridFS\Exception\CorruptFileException',
'ExtraChunk' => '\MongoDB\Exception\GridFSCorruptFileException', 'ExtraChunk' => '\MongoDB\GridFS\Exception\CorruptFileException',
'ChunkIsWrongSize' => '\MongoDB\Exception\GridFSCorruptFileException', 'ChunkIsWrongSize' => '\MongoDB\GridFS\Exception\CorruptFileException',
'RevisionNotFound' => '\MongoDB\Exception\GridFSFileNotFoundException' 'RevisionNotFound' => '\MongoDB\GridFS\Exception\FileNotFoundException'
]; ];
if (!isset($test['assert']['error'])) { if (!isset($test['assert']['error'])) {
$this->assertNull($error); $this->assertNull($error);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment