Skip to content

Commit a36b079

Browse files
committed
PHPLIB-199: Convert Exceptions to E_USER_WARNING in StreamWrapper
Unfortunately, PHP's StreamWrapper API requires that we trigger errors instead of throwing exceptions. This will affect driver exceptions thrown during IO and CorruptFileException thrown by ReadableStream.
1 parent 63c2313 commit a36b079

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/GridFS/StreamWrapper.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MongoDB\GridFS;
44

5+
use Exception;
6+
57
/**
68
* Stream wrapper for reading and writing a GridFS file.
79
*
@@ -105,7 +107,12 @@ public function stream_read($count)
105107
return '';
106108
}
107109

108-
return $this->stream->downloadNumBytes($count);
110+
try {
111+
return $this->stream->downloadNumBytes($count);
112+
} catch (Exception $e) {
113+
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
114+
return false;
115+
}
109116
}
110117

111118
/**
@@ -137,8 +144,12 @@ public function stream_write($data)
137144
return 0;
138145
}
139146

140-
$this->stream->insertChunks($data);
141-
return strlen($data);
147+
try {
148+
return $this->stream->insertChunks($data);
149+
} catch (Exception $e) {
150+
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
151+
return false;
152+
}
142153
}
143154

144155
/**

tests/GridFS/BucketFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testDeleteStillRemovesChunksIfFileDoesNotExist($input, $expected
125125
}
126126

127127
/**
128-
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
128+
* @expectedException PHPUnit_Framework_Error_Warning
129129
*/
130130
public function testDownloadingFileWithMissingChunk()
131131
{
@@ -137,7 +137,7 @@ public function testDownloadingFileWithMissingChunk()
137137
}
138138

139139
/**
140-
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
140+
* @expectedException PHPUnit_Framework_Error_Warning
141141
*/
142142
public function testDownloadingFileWithUnexpectedChunkIndex()
143143
{
@@ -152,7 +152,7 @@ public function testDownloadingFileWithUnexpectedChunkIndex()
152152
}
153153

154154
/**
155-
* @expectedException MongoDB\GridFS\Exception\CorruptFileException
155+
* @expectedException PHPUnit_Framework_Error_Warning
156156
*/
157157
public function testDownloadingFileWithUnexpectedChunkSize()
158158
{

tests/GridFS/SpecFunctionalTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use MongoDB\BSON\Binary;
77
use MongoDB\BSON\ObjectId;
88
use MongoDB\BSON\UTCDateTime;
9-
use MongoDB\Exception\RuntimeException;
109
use MongoDB\Operation\BulkWrite;
1110
use DateTime;
11+
use Exception;
1212
use IteratorIterator;
1313
use LogicException;
1414
use MultipleIterator;
@@ -50,7 +50,7 @@ public function testSpecification(array $initialData, array $test)
5050

5151
try {
5252
$result = $this->executeAct($test['act']);
53-
} catch (RuntimeException $e) {
53+
} catch (Exception $e) {
5454
$result = $e;
5555
}
5656

@@ -333,7 +333,10 @@ private function getExceptionClassForError($error)
333333

334334
case 'ChunkIsMissing':
335335
case 'ChunkIsWrongSize':
336-
return 'MongoDB\GridFS\Exception\CorruptFileException';
336+
/* Although ReadableStream throws a CorruptFileException, the
337+
* stream wrapper will convert it to a PHP error of type
338+
* E_USER_WARNING. */
339+
return 'PHPUnit_Framework_Error_Warning';
337340

338341
default:
339342
throw new LogicException('Unsupported error: ' . $error);

0 commit comments

Comments
 (0)