diff --git a/src/GridFS/StreamWrapper.php b/src/GridFS/StreamWrapper.php
index 24b0f0156d125566b624a6b77519c578b7376bf9..30ab6057c595b570f6ba4aa65c44e9f2c3694e78 100644
--- a/src/GridFS/StreamWrapper.php
+++ b/src/GridFS/StreamWrapper.php
@@ -2,6 +2,8 @@
namespace MongoDB\GridFS;
+use Exception;
+
/**
* Stream wrapper for reading and writing a GridFS file.
*
@@ -105,7 +107,12 @@ class StreamWrapper
return '';
}
- return $this->stream->downloadNumBytes($count);
+ try {
+ return $this->stream->downloadNumBytes($count);
+ } catch (Exception $e) {
+ trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
+ return false;
+ }
}
/**
@@ -137,8 +144,12 @@ class StreamWrapper
return 0;
}
- $this->stream->insertChunks($data);
- return strlen($data);
+ try {
+ return $this->stream->insertChunks($data);
+ } catch (Exception $e) {
+ trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
+ return false;
+ }
}
/**
diff --git a/tests/GridFS/BucketFunctionalTest.php b/tests/GridFS/BucketFunctionalTest.php
index f3ca9efbe1b9f12f78f0d301c93179a9328fad56..e21a2fdccb62675d530d1871e434a603350e8010 100644
--- a/tests/GridFS/BucketFunctionalTest.php
+++ b/tests/GridFS/BucketFunctionalTest.php
@@ -125,7 +125,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
/**
- * @expectedException MongoDB\GridFS\Exception\CorruptFileException
+ * @expectedException PHPUnit_Framework_Error_Warning
*/
public function testDownloadingFileWithMissingChunk()
{
@@ -137,7 +137,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
/**
- * @expectedException MongoDB\GridFS\Exception\CorruptFileException
+ * @expectedException PHPUnit_Framework_Error_Warning
*/
public function testDownloadingFileWithUnexpectedChunkIndex()
{
@@ -152,7 +152,7 @@ class BucketFunctionalTest extends FunctionalTestCase
}
/**
- * @expectedException MongoDB\GridFS\Exception\CorruptFileException
+ * @expectedException PHPUnit_Framework_Error_Warning
*/
public function testDownloadingFileWithUnexpectedChunkSize()
{
diff --git a/tests/GridFS/SpecFunctionalTest.php b/tests/GridFS/SpecFunctionalTest.php
index d45adf0fa934e473c9ebcae8a8bc86b7c3dbd2fa..5ecf02c499cb2b93a897a3996b37005fd627d15c 100644
--- a/tests/GridFS/SpecFunctionalTest.php
+++ b/tests/GridFS/SpecFunctionalTest.php
@@ -6,9 +6,9 @@ use MongoDB\Collection;
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
-use MongoDB\Exception\RuntimeException;
use MongoDB\Operation\BulkWrite;
use DateTime;
+use Exception;
use IteratorIterator;
use LogicException;
use MultipleIterator;
@@ -50,7 +50,7 @@ class SpecFunctionalTest extends FunctionalTestCase
try {
$result = $this->executeAct($test['act']);
- } catch (RuntimeException $e) {
+ } catch (Exception $e) {
$result = $e;
}
@@ -333,7 +333,10 @@ class SpecFunctionalTest extends FunctionalTestCase
case 'ChunkIsMissing':
case 'ChunkIsWrongSize':
- return 'MongoDB\GridFS\Exception\CorruptFileException';
+ /* Although ReadableStream throws a CorruptFileException, the
+ * stream wrapper will convert it to a PHP error of type
+ * E_USER_WARNING. */
+ return 'PHPUnit_Framework_Error_Warning';
default:
throw new LogicException('Unsupported error: ' . $error);