Skip to content

Commit 98e8681

Browse files
committed
feature #33155 [ErrorHandler] Added call() method utility to turns any PHP error into \ErrorException (yceruto)
This PR was merged into the 4.4 branch. Discussion ---------- [ErrorHandler] Added call() method utility to turns any PHP error into \ErrorException | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32936 | License | MIT | Doc PR | symfony/symfony-docs#... **Issue** There is no easy way to catch PHP warnings, though some progress has been made in this area for PHP 8.0 (https://wiki.php.net/rfc/consistent_type_errors). **Before** ```php $file = file_get_contents('unknown.txt'); // PHP Warning: file_get_contents(unknown.txt): failed to open stream: No such file or directory // workaround: $file = @file_get_contents('unknown.txt'); if (false === $file) { $e = error_get_last(); throw new \ErrorException($e['message'], 0, $e['type'], $e['file'], $e['line']); } ``` **After** ```php $file = ErrorHandler::call('file_get_contents', 'unknown.txt'); // or $file = ErrorHandler::call(static function () { return file_get_contents('unknown.txt'); }); // or (PHP 7.4) $file = ErrorHandler::call(fn () => file_get_contents('unknown.txt')); ``` All credits to @nicolas-grekas #32936 (comment) and @vudaltsov for the idea. Commits ------- 0faa855 Added ErrorHandler::call() method utility to turns any PHP warnings into `\ErrorException`
2 parents 6ee1f0b + 0faa855 commit 98e8681

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/Symfony/Component/ErrorHandler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ CHANGELOG
55
-----
66

77
* added the component
8+
* added `ErrorHandler::call()` method utility to turn any PHP error into `\ErrorException`

src/Symfony/Component/ErrorHandler/ErrorHandler.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,32 @@ public static function register(self $handler = null, bool $replace = true): sel
153153
return $handler;
154154
}
155155

156+
/**
157+
* Calls a function and turns any PHP error into \ErrorException.
158+
*
159+
* @return mixed What $function(...$arguments) returns
160+
*
161+
* @throws \ErrorException When $function(...$arguments) triggers a PHP error
162+
*/
163+
public static function call(callable $function, ...$arguments)
164+
{
165+
set_error_handler(static function (int $type, string $message, string $file, int $line) {
166+
if (__FILE__ === $file) {
167+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
168+
$file = $trace[2]['file'] ?? $file;
169+
$line = $trace[2]['line'] ?? $line;
170+
}
171+
172+
throw new \ErrorException($message, 0, $type, $file, $line);
173+
});
174+
175+
try {
176+
return $function(...$arguments);
177+
} finally {
178+
restore_error_handler();
179+
}
180+
}
181+
156182
public function __construct(BufferingLogger $bootstrappingLogger = null)
157183
{
158184
if ($bootstrappingLogger) {

src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,62 @@ public function testNotice()
123123
}
124124

125125
// dummy function to test trace in error handler.
126-
private static function triggerNotice($that)
126+
public static function triggerNotice($that)
127127
{
128128
$that->assertSame('', $foo.$foo.$bar);
129129
}
130130

131+
public function testFailureCall()
132+
{
133+
$this->expectException(\ErrorException::class);
134+
$this->expectExceptionMessage('fopen(unknown.txt): failed to open stream: No such file or directory');
135+
136+
ErrorHandler::call('fopen', 'unknown.txt', 'r');
137+
}
138+
139+
public function testCallRestoreErrorHandler()
140+
{
141+
$prev = set_error_handler('var_dump');
142+
try {
143+
ErrorHandler::call('fopen', 'unknown.txt', 'r');
144+
$this->fail('An \ErrorException should have been raised');
145+
} catch (\ErrorException $e) {
146+
$prev = set_error_handler($prev);
147+
restore_error_handler();
148+
} finally {
149+
restore_error_handler();
150+
}
151+
152+
$this->assertSame('var_dump', $prev);
153+
}
154+
155+
public function testCallErrorExceptionInfo()
156+
{
157+
try {
158+
ErrorHandler::call([self::class, 'triggerNotice'], $this);
159+
$this->fail('An \ErrorException should have been raised');
160+
} catch (\ErrorException $e) {
161+
$trace = $e->getTrace();
162+
$this->assertSame(E_NOTICE, $e->getSeverity());
163+
$this->assertSame(__FILE__, $e->getFile());
164+
$this->assertSame('Undefined variable: foo', $e->getMessage());
165+
$this->assertSame(0, $e->getCode());
166+
$this->assertSame('Symfony\Component\ErrorHandler\{closure}', $trace[0]['function']);
167+
$this->assertSame(ErrorHandler::class, $trace[0]['class']);
168+
$this->assertSame('triggerNotice', $trace[1]['function']);
169+
$this->assertSame(__CLASS__, $trace[1]['class']);
170+
}
171+
}
172+
173+
public function testSuccessCall()
174+
{
175+
touch($filename = tempnam(sys_get_temp_dir(), 'sf_error_handler_'));
176+
177+
self::assertIsResource(ErrorHandler::call('fopen', $filename, 'r'));
178+
179+
unlink($filename);
180+
}
181+
131182
public function testConstruct()
132183
{
133184
try {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy