Skip to content

Commit 66e2e9d

Browse files
authored
PHPLIB-592: Allow GridFS StreamWrapper to throw exceptions (#869)
Previously, exceptions were converted to E_USER_WARNING with a false return value for consistency with PHP's stream API (e.g. fread). This is not actually required and allowing the original exception to be propagated is likely more helpful for users and simplifies some handling in the spec test runner.
1 parent f7b9ba2 commit 66e2e9d

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

src/GridFS/StreamWrapper.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@
1919

2020
use MongoDB\BSON\UTCDateTime;
2121
use stdClass;
22-
use Throwable;
2322

2423
use function explode;
25-
use function get_class;
2624
use function in_array;
2725
use function is_integer;
28-
use function sprintf;
2926
use function stream_context_get_options;
3027
use function stream_get_wrappers;
3128
use function stream_wrapper_register;
3229
use function stream_wrapper_unregister;
33-
use function trigger_error;
3430

35-
use const E_USER_WARNING;
3631
use const SEEK_CUR;
3732
use const SEEK_END;
3833
use const SEEK_SET;
@@ -162,13 +157,7 @@ public function stream_read($length)
162157
return '';
163158
}
164159

165-
try {
166-
return $this->stream->readBytes($length);
167-
} catch (Throwable $e) {
168-
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), E_USER_WARNING);
169-
170-
return false;
171-
}
160+
return $this->stream->readBytes($length);
172161
}
173162

174163
/**
@@ -259,13 +248,7 @@ public function stream_write($data)
259248
return 0;
260249
}
261250

262-
try {
263-
return $this->stream->writeBytes($data);
264-
} catch (Throwable $e) {
265-
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), E_USER_WARNING);
266-
267-
return false;
268-
}
251+
return $this->stream->writeBytes($data);
269252
}
270253

271254
/**

tests/GridFS/BucketFunctionalTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MongoDB\Driver\WriteConcern;
1010
use MongoDB\Exception\InvalidArgumentException;
1111
use MongoDB\GridFS\Bucket;
12+
use MongoDB\GridFS\Exception\CorruptFileException;
1213
use MongoDB\GridFS\Exception\FileNotFoundException;
1314
use MongoDB\GridFS\Exception\StreamException;
1415
use MongoDB\Model\BSONDocument;
@@ -173,7 +174,8 @@ public function testDownloadingFileWithMissingChunk(): void
173174

174175
$this->chunksCollection->deleteOne(['files_id' => $id, 'n' => 0]);
175176

176-
$this->expectWarning();
177+
$this->expectException(CorruptFileException::class);
178+
$this->expectExceptionMessage('Chunk not found for index "0"');
177179
stream_get_contents($this->bucket->openDownloadStream($id));
178180
}
179181

@@ -186,7 +188,8 @@ public function testDownloadingFileWithUnexpectedChunkIndex(): void
186188
['$set' => ['n' => 1]]
187189
);
188190

189-
$this->expectWarning();
191+
$this->expectException(CorruptFileException::class);
192+
$this->expectExceptionMessage('Expected chunk to have index "0" but found "1"');
190193
stream_get_contents($this->bucket->openDownloadStream($id));
191194
}
192195

@@ -199,7 +202,8 @@ public function testDownloadingFileWithUnexpectedChunkSize(): void
199202
['$set' => ['data' => new Binary('fooba', Binary::TYPE_GENERIC)]]
200203
);
201204

202-
$this->expectWarning();
205+
$this->expectException(CorruptFileException::class);
206+
$this->expectExceptionMessage('Expected chunk to have size "6" but found "5"');
203207
stream_get_contents($this->bucket->openDownloadStream($id));
204208
}
205209

tests/UnifiedSpecTests/Operation.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use MongoDB\Operation\FindOneAndReplace;
1818
use MongoDB\Operation\FindOneAndUpdate;
1919
use PHPUnit\Framework\Assert;
20-
use PHPUnit\Framework\AssertionFailedError;
2120
use PHPUnit\Framework\Constraint\IsType;
21+
use PHPUnit\Framework\Exception as PHPUnitException;
2222
use stdClass;
2323
use Throwable;
2424

@@ -146,18 +146,14 @@ public function assert(bool $rethrowExceptions = false): void
146146
$result = $this->execute();
147147
$saveResultAsEntity = $this->saveResultAsEntity;
148148
} catch (Throwable $e) {
149-
/* Rethrow any internal PHP errors and PHPUnit assertion failures,
150-
* since those are never expected for "expectError".
151-
*
152-
* Note: we must be selective about what PHPUnit exceptions to pass
153-
* through, as PHPUnit's Warning exception must be considered for
154-
* expectError in GridFS tests (see: PHPLIB-592).
149+
/* Rethrow any internal PHP errors and PHPUnit exceptions, since
150+
* those are never expected for "expectError".
155151
*
156152
* TODO: Consider adding operation details (e.g. operations[] index)
157153
* to the exception message. Alternatively, throw a new exception
158154
* and include this as the previous, since PHPUnit will render the
159155
* chain when reporting a test failure. */
160-
if ($e instanceof Error || $e instanceof AssertionFailedError) {
156+
if ($e instanceof Error || $e instanceof PHPUnitException) {
161157
throw $e;
162158
}
163159

0 commit comments

Comments
 (0)