Skip to content

Commit 1619843

Browse files
committed
Added ErrorHandler::call() method utility to turns any PHP warnings into \ErrorException
1 parent 588890a commit 1619843

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-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 warning
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: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,59 @@ 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+
} catch (\ErrorException $e) {
145+
$prev = set_error_handler($prev);
146+
restore_error_handler();
147+
}
148+
restore_error_handler();
149+
150+
$this->assertSame('var_dump', $prev);
151+
}
152+
153+
public function testCallErrorExceptionInfo()
154+
{
155+
try {
156+
ErrorHandler::call([self::class, 'triggerNotice'], $this);
157+
} catch (\ErrorException $e) {
158+
$trace = $e->getTrace();
159+
$this->assertSame(E_NOTICE, $e->getSeverity());
160+
$this->assertSame(__FILE__, $e->getFile());
161+
$this->assertSame('Undefined variable: foo', $e->getMessage());
162+
$this->assertSame(0, $e->getCode());
163+
$this->assertSame('Symfony\Component\ErrorHandler\{closure}', $trace[0]['function']);
164+
$this->assertSame(ErrorHandler::class, $trace[0]['class']);
165+
$this->assertSame('triggerNotice', $trace[1]['function']);
166+
$this->assertSame(__CLASS__, $trace[1]['class']);
167+
}
168+
}
169+
170+
public function testSuccessCall()
171+
{
172+
touch($filename = tempnam(sys_get_temp_dir(), 'sf_error_handler_'));
173+
174+
self::assertIsResource(ErrorHandler::call('fopen', $filename, 'r'));
175+
176+
unlink($filename);
177+
}
178+
131179
public function testConstruct()
132180
{
133181
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