Skip to content

[ErrorHandler] Registering basic exception handler for late failures #33327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/Symfony/Component/ErrorHandler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,11 @@ public function handleException($exception, array $error = null)
}
}
$exceptionHandler = $this->exceptionHandler;
$this->exceptionHandler = null;
if ((!\is_array($exceptionHandler) || !$exceptionHandler[0] instanceof self || 'sendPhpResponse' !== $exceptionHandler[1]) && !\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
$this->exceptionHandler = [$this, 'sendPhpResponse'];
} else {
$this->exceptionHandler = null;
}
try {
if (null !== $exceptionHandler) {
return $exceptionHandler($exception);
Expand Down Expand Up @@ -696,18 +700,28 @@ public static function handleFatalError(array $error = null): void
private function sendPhpResponse(\Throwable $exception)
{
$charset = ini_get('default_charset') ?: 'UTF-8';

if (!headers_sent()) {
header('HTTP/1.0 500');
header(sprintf('Content-Type: text/html; charset=%s', $charset));
}
$statusCode = 500;
$headers = [];

if (class_exists(HtmlErrorRenderer::class)) {
echo (new HtmlErrorRenderer(true))->render(FlattenException::createFromThrowable($exception));
$exception = FlattenException::createFromThrowable($exception);
$statusCode = $exception->getStatusCode();
$headers = $exception->getHeaders();
$response = (new HtmlErrorRenderer(true))->render($exception);
} else {
$message = htmlspecialchars($exception->getMessage(), ENT_COMPAT | ENT_SUBSTITUTE, $charset);
echo sprintf('<!DOCTYPE html><html><head><meta charset="%s" /><meta name="robots" content="noindex,nofollow" /></head><body>%s</body></html>', $charset, $message);
$response = sprintf('<!DOCTYPE html><html><head><meta charset="%s" /><meta name="robots" content="noindex,nofollow" /></head><body>%s</body></html>', $charset, $message);
}

if (!headers_sent()) {
header(sprintf('HTTP/1.0 %s', $statusCode));
foreach ($headers as $name => $value) {
header($name.': '.$value, false);
}
header('Content-Type: text/html; charset='.$charset);
}

echo $response;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,18 @@ public function testCustomExceptionHandler()
$handler->handleException(new \Exception());
}

public function testSendPhpResponse()
{
$handler = new ErrorHandler();
$handler->setExceptionHandler([$handler, 'sendPhpResponse']);

ob_start();
$handler->handleException(new \RuntimeException('Class Foo not found'));
$response = ob_get_clean();

self::assertStringContainsString('Class Foo not found', $response);
}

/**
* @dataProvider errorHandlerWhenLoggingProvider
*/
Expand Down