Repository navigation
Todo
Labels
Description
Description
The following code:
class InvalidFilter extends php_user_filter
{
public const FILTERNAME = 'test.fails.later';
private int $calls = 0;
public static function register(): void
{
if (in_array(self::FILTERNAME, stream_get_filters(), true)) {
return;
}
stream_filter_register(self::FILTERNAME, self::class) || throw new RuntimeException('The stream filter "'.self::FILTERNAME.'" could not be registered.');
}
public function onCreate(): bool
{
$this->calls = 0;
return true;
}
public function onClose(): void
{
$this->calls = 0;
}
public function filter($in, $out, &$consumed, bool $closing): int
{
$inputBuffer = '';
while (null !== ($bucket = stream_bucket_make_writeable($in))) {
$inputBuffer .= $bucket->data;
$consumed += $bucket->datalen;
}
if (0 === $this->calls) {
stream_bucket_append($out, stream_bucket_new($this->stream, $inputBuffer));
++$this->calls;
return PSFS_PASS_ON;
}
trigger_error('This is an error', E_USER_WARNING);
return PSFS_ERR_FATAL;
}
}
InvalidFilter::register();
$context = stream_context_create(['stream' => [
'error_mode' => StreamErrorMode::Exception,
'error_store' => StreamErrorStore::All,
]]);
$path = tempnam(sys_get_temp_dir(), 'csv');
file_put_contents($path, str_repeat("a,b,c\n", 1366));
$stream = fopen(filename: $path, mode: 'r', context: $context);
$filter = stream_filter_append($stream,'test.fails.later', STREAM_FILTER_READ);
try {
$row = 1;
while (($fields = fgetcsv($stream, escape: "")) !== false) {
$num = count($fields);
echo "\n<p>$num champs à la ligne $row :</p>\n<ol>";
for ($c = 0; $c < $num; $c++) {
echo "\n\t<li>", $fields[$c], "</li>";
}
echo "\n</ol>";
$row++;
}
fclose($stream);
} catch (StreamException $e) {
echo "Error: " . $e->getMessage() . "\n";
echo "Code: " . $e->getCode() . "\n";
$errors = $e->getErrors();
$first = array_first($errors);
if ($first) {
echo "Wrapper: " . $first->wrapperName . "\n";
echo "Error Code: " . $first->code->name . "\n";
if ($first->param) {
echo "File: " . $first->param . "\n";
}
}
}Resulted in this output:
with the first bucket being read.
Warning: This is an error in filepath.php on line 46
But I expected this output instead:
I would have expected a StreamException to be caught
PHP Version
PHP 8.6.0
Operating System
No response
Activity
Girgias commentedon Nov 23, 2026
The issue is that the stream layer doesn't hook or override the error handler, so emitting a user error would not get overridden. The solution is probably to check the error_mode/error_store context options via
stream_context_get_options(or possiblystream_context_get_params()) and determine if you should emit a warning, throw an exception, or do nothing.nyamsprod commentedon Nov 24, 2026
@Girgias the E_USER_WARNING is the "hack" I use currently. I indeed catch the warning and convert it into an ErrorException. I was hoping that with the new StreamException I would no longer need to do that. In my example I do use
Hence why I would I thought I would found the offending error when examinating the emitted Stream Exception which is not emitted at all.