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;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\GridFSFileNotFoundException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\GridFS\Exception\FileNotFoundException;
use MongoDB\Operation\Find;
/**
......@@ -84,7 +84,7 @@ class Bucket
* attempt to delete orphaned chunks.
*
* @param ObjectId $id ObjectId of the file
* @throws GridFSFileNotFoundException
* @throws FileNotFoundException
*/
public function delete(ObjectId $id)
{
......@@ -93,7 +93,7 @@ class Bucket
$this->collectionsWrapper->getChunksCollection()->deleteMany(['files_id' => $id]);
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
*
* @param ObjectId $id ObjectId of the file
* @param resource $destination Writable Stream
* @throws GridFSFileNotFoundException
* @throws FileNotFoundException
*/
public function downloadToStream(ObjectId $id, $destination)
{
......@@ -113,7 +113,7 @@ class Bucket
);
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);
......@@ -142,7 +142,7 @@ class Bucket
* @param string $filename File name
* @param resource $destination Writable Stream
* @param array $options Download options
* @throws GridFSFileNotFoundException
* @throws FileNotFoundException
*/
public function downloadToStreamByName($filename, $destination, array $options = [])
{
......@@ -207,7 +207,7 @@ class Bucket
*
* @param ObjectId $id ObjectId of the file
* @return resource
* @throws GridFSFileNotFoundException
* @throws FileNotFoundException
*/
public function openDownloadStream(ObjectId $id)
{
......@@ -217,7 +217,7 @@ class Bucket
);
if ($file === null) {
throw new GridFSFileNotFoundException($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace());
}
return $this->openDownloadStreamByFile($file);
......@@ -245,7 +245,7 @@ class Bucket
* @param string $filename File name
* @param array $options Download options
* @return resource
* @throws GridFSFileNotFoundException
* @throws FileNotFoundException
*/
public function openDownloadStreamByName($filename, array $options = [])
{
......@@ -293,7 +293,7 @@ class Bucket
$filesCollection = $this->collectionsWrapper->getFilesCollection();
$result = $filesCollection->updateOne(['_id' => $id], ['$set' => ['filename' => $newFilename]]);
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
);
if ($file === null) {
throw new GridFSFileNotFoundException($filename, $filesCollection->getNameSpace());
throw FileNotFoundException::byFilenameAndRevision($filename, $revision, $filesCollection->getNameSpace());
}
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 @@
namespace MongoDB\GridFS;
use MongoDB\Driver\Exception\Exception;
use MongoDB\Exception\GridFSCorruptFileException;
use MongoDB\GridFS\Exception\CorruptFileException;
use stdClass;
/**
......@@ -30,7 +30,7 @@ class GridFSDownload
*
* @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper
* @param stdClass $file GridFS file document
* @throws GridFSCorruptFileException
* @throws CorruptFileException
*/
public function __construct(GridFSCollectionsWrapper $collectionsWrapper, stdClass $file)
{
......@@ -43,8 +43,8 @@ class GridFSDownload
['sort' => ['n' => 1]]
);
} catch (Exception $e) {
// TODO: Why do we replace a driver exception with GridFSCorruptFileException here?
throw new GridFSCorruptFileException();
// TODO: Why do we replace a driver exception with CorruptFileException here?
throw new CorruptFileException();
}
$this->chunksIterator = new \IteratorIterator($cursor);
......@@ -138,11 +138,11 @@ class GridFSDownload
}
if ( ! $this->chunksIterator->valid()) {
throw new GridFSCorruptFileException();
throw CorruptFileException::missingChunk($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());
......@@ -152,7 +152,7 @@ class GridFSDownload
: $this->file->chunkSize;
if ($actualChunkSize != $expectedChunkSize) {
throw new GridFSCorruptFileException();
throw CorruptFileException::unexpectedSize($actualChunkSize, $expectedChunkSize);
}
$this->bytesSeen += $actualChunkSize;
......
......@@ -73,7 +73,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound);
$this->assertEquals(0, $this->bucket->getCollectionsWrapper()->getFilesCollection()->count());
$this->assertEquals(0, $this->bucket->getCollectionsWrapper()->getChunksCollection()->count());
......@@ -116,7 +116,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$corruptFileError = '\MongoDB\Exception\GridFSCOrruptFileException';
$corruptFileError = '\MongoDB\GridFS\Exception\CorruptFileException';
$this->assertTrue($error instanceof $corruptFileError);
}
public function testErrorsOnMissingChunk()
......@@ -131,7 +131,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$corruptFileError = '\MongoDB\Exception\GridFSCOrruptFileException';
$corruptFileError = '\MongoDB\GridFS\Exception\CorruptFileException';
$this->assertTrue($error instanceof $corruptFileError);
}
public function testUploadEnsureIndexes()
......@@ -177,7 +177,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound);
}
public function testGetVersion()
......@@ -194,7 +194,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$this->assertEquals("bar", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -2])));
$this->assertEquals("foo", stream_get_contents($this->bucket->openDownloadStreamByName("test", ['revision' => -3])));
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$error = null;
try{
$this->bucket->openDownloadStreamByName("test", ['revision' => 3]);
......@@ -278,7 +278,7 @@ class BucketFunctionalTest extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$fileNotFound = '\MongoDB\Exception\GridFSFileNotFoundException';
$fileNotFound = '\MongoDB\GridFS\Exception\FileNotFoundException';
$this->assertTrue($error instanceof $fileNotFound);
$this->assertEquals("testing", stream_get_contents($this->bucket->openDownloadStreamByName("second_name")));
......
......@@ -52,11 +52,11 @@ class SpecificationTests extends FunctionalTestCase
} catch(\MongoDB\Exception\Exception $e) {
$error = $e;
}
$errors = ['FileNotFound' => '\MongoDB\Exception\GridFSFileNotFoundException',
'ChunkIsMissing' => '\MongoDB\Exception\GridFSCorruptFileException',
'ExtraChunk' => '\MongoDB\Exception\GridFSCorruptFileException',
'ChunkIsWrongSize' => '\MongoDB\Exception\GridFSCorruptFileException',
'RevisionNotFound' => '\MongoDB\Exception\GridFSFileNotFoundException'
$errors = ['FileNotFound' => '\MongoDB\GridFS\Exception\FileNotFoundException',
'ChunkIsMissing' => '\MongoDB\GridFS\Exception\CorruptFileException',
'ExtraChunk' => '\MongoDB\GridFS\Exception\CorruptFileException',
'ChunkIsWrongSize' => '\MongoDB\GridFS\Exception\CorruptFileException',
'RevisionNotFound' => '\MongoDB\GridFS\Exception\FileNotFoundException'
];
if (!isset($test['assert']['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