Skip to content

Commit ffab734

Browse files
committed
registering basic exception handler for late failures
1 parent 959eb56 commit ffab734

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/Symfony/Component/ErrorHandler/ErrorHandler.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,11 @@ public function handleException($exception, array $error = null)
592592
}
593593
}
594594
$exceptionHandler = $this->exceptionHandler;
595-
$this->exceptionHandler = null;
595+
if ((!\is_array($exceptionHandler) || !$exceptionHandler[0] instanceof self || 'sendPhpResponse' !== $exceptionHandler[1]) && !\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
596+
$this->exceptionHandler = [$this, 'sendPhpResponse'];
597+
} else {
598+
$this->exceptionHandler = null;
599+
}
596600
try {
597601
if (null !== $exceptionHandler) {
598602
return $exceptionHandler($exception);
@@ -696,18 +700,28 @@ public static function handleFatalError(array $error = null): void
696700
private function sendPhpResponse(\Throwable $exception)
697701
{
698702
$charset = ini_get('default_charset') ?: 'UTF-8';
699-
700-
if (!headers_sent()) {
701-
header('HTTP/1.0 500');
702-
header(sprintf('Content-Type: text/html; charset=%s', $charset));
703-
}
703+
$statusCode = 500;
704+
$headers = [];
704705

705706
if (class_exists(HtmlErrorRenderer::class)) {
706-
echo (new HtmlErrorRenderer(true))->render(FlattenException::createFromThrowable($exception));
707+
$exception = FlattenException::createFromThrowable($exception);
708+
$statusCode = $exception->getStatusCode();
709+
$headers = $exception->getHeaders();
710+
$response = (new HtmlErrorRenderer(true))->render($exception);
707711
} else {
708712
$message = htmlspecialchars($exception->getMessage(), ENT_COMPAT | ENT_SUBSTITUTE, $charset);
709-
echo sprintf('<!DOCTYPE html><html><head><meta charset="%s" /><meta name="robots" content="noindex,nofollow" /></head><body>%s</body></html>', $charset, $message);
713+
$response = sprintf('<!DOCTYPE html><html><head><meta charset="%s" /><meta name="robots" content="noindex,nofollow" /></head><body>%s</body></html>', $charset, $message);
710714
}
715+
716+
if (!headers_sent()) {
717+
header(sprintf('HTTP/1.0 %s', $statusCode));
718+
foreach ($headers as $name => $value) {
719+
header($name.': '.$value, false);
720+
}
721+
header('Content-Type: text/html; charset='.$charset);
722+
}
723+
724+
echo $response;
711725
}
712726

713727
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,18 @@ public function testCustomExceptionHandler()
557557
$handler->handleException(new \Exception());
558558
}
559559

560+
public function testSendPhpResponse()
561+
{
562+
$handler = new ErrorHandler();
563+
$handler->setExceptionHandler([$handler, 'sendPhpResponse']);
564+
565+
ob_start();
566+
$handler->handleException(new \RuntimeException('Class Foo not found'));
567+
$response = ob_get_clean();
568+
569+
self::assertStringContainsString('Class Foo not found', $response);
570+
}
571+
560572
/**
561573
* @dataProvider errorHandlerWhenLoggingProvider
562574
*/

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