8000 [Debug] Better error handling · symfony/symfony@c17f213 · GitHub
[go: up one dir, main page]

Skip to content

Commit c17f213

Browse files
committed
[Debug] Better error handling
1. Send the raw exception in the log context instead of custom formatting 2. Add config option to log in Symfony all PHP errors
1 parent 02f59fe commit c17f213

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public function getConfigTreeBuilder()
9494
->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()
9595
->prototype('scalar')->end()
9696
->end()
97+
->arrayNode('debug')
98+
->addDefaultsIfNotSet()
99+
->children()
100+
->booleanNode('log_php_errors')
101+
->info('Log PHP errors and disable native error logging')
102+
->defaultValue($this->debug)
103+
->end()
104+
->end()
105+
->end()
97106
->end()
98107
;
99108

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ public function load(array $configs, ContainerBuilder $container)
164164
$definition->setPublic(false);
165165
$container->setDefinition('debug.event_dispatcher.parent', $definition);
166166
$container->setAlias('event_dispatcher', 'debug.event_dispatcher');
167-
} else {
167+
}
168+
169+
if (!$config['debug']['log_php_errors']) {
168170
$definition->replaceArgument(1, null);
169171
}
170172

src/Symfony/Component/Debug/ErrorHandler.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -495,20 +495,13 @@ public function handleException($exception, array $error = null)
495495
$type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
496496

497497
if (($this->loggedErrors & $type) || $exception instanceof FatalThrowableError) {
498-
$e = array(
499-
'type' => $type,
500-
'file' => $exception->getFile(),
501-
'line' => $exception->getLine(),
502-
'level' => error_reporting(),
503-
'stack' => $exception->getTrace(),
504-
);
505498
if ($exception instanceof FatalErrorException) {
506499
if ($exception instanceof FatalThrowableError) {
507500
$error = array(
508501
'type' => $type,
509502
'message' => $message = $exception->getMessage(),
510-
'file' => $e['file'],
511-
'line' => $e['line'],
503+
'file' => $exception->getFile(),
504+
'line' => $exception->getLine(),
512505
);
513506
} else {
514507
$message = 'Fatal '.$exception->getMessage();
@@ -523,7 +516,7 @@ public function handleException($exception, array $error = null)
523516
}
524517
}
525518
if ($this->loggedErrors & $type) {
526-
$this->loggers[$type][0]->log($this->loggers[$type][1], $message, $e);
519+
$this->loggers[$type][0]->log($this->loggers[$type][1], $message, array('exception' => $exception));
527520
}
528521
if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
529522
foreach ($this->getFatalErrorHandlers() as $handler) {

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Component\Debug\Tests;
1313

1414
use Psr\Log\LogLevel;
15-
use Symfony\Component\Debug\ErrorHandler;
1615
use Symfony\Component\Debug\BufferingLogger;
16+
use Symfony\Component\Debug\ErrorHandler;
1717
use Symfony\Component\Debug\Exception\ContextErrorException;
1818

1919
/**
@@ -305,9 +305,9 @@ public function testHandleException()
305305
$logger = $this->getMock('Psr\Log\LoggerInterface');
306306

307307
$logArgCheck = function ($level, $message, $context) {
308-
$this->assertEquals('Uncaught Exception: foo', $message);
309-
$this->assertArrayHasKey('type', $context);
310-
$this->assertEquals($context['type'], E_ERROR);
308+
$this->assertSame('Uncaught Exception: foo', $message);
309+
$this->assertArrayHasKey('exception', $context);
310+
$this->assertInstanceOf(\Exception::class, $context['exception']);
311311
};
312312

313313
$logger
@@ -424,8 +424,8 @@ public function testHandleFatalError()
424424

425425
$logArgCheck = function ($level, $message, $context) {
426426
$this->assertEquals('Fatal Parse Error: foo', $message);
427-
$this->assertArrayHasKey('type', $context);
428-
$this->assertEquals($context['type'], E_PARSE);
427+
$this->assertArrayHasKey('exception', $context);
428+
$this->assertInstanceOf(\Exception::class, $context['exception']);
429429
};
430430

431431
$logger
@@ -477,14 +477,7 @@ public function testHandleFatalErrorOnHHVM()
477477
->method('log')
478478
->with(
479479
$this->equalTo(LogLevel::CRITICAL),
480-
$this->equalTo('Fatal Error: foo'),
481-
$this->equalTo(array(
482-
'type' => 1,
483-
'file' => 'bar',
484-
'line' => 123,
485-
'level' => -1,
486-
'stack' => array(456),
487-
))
480+
$this->equalTo('Fatal Error: foo')
488481
)
489482
;
490483

0 commit comments

Comments
 (0)
0