Skip to content

[ErrorHandler] Registering basic exception handler to handle early failures #33260

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
Aug 20, 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
31 changes: 29 additions & 2 deletions src/Symfony/Component/ErrorHandler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Symfony\Component\ErrorHandler\FatalErrorHandler\FatalErrorHandlerInterface;
use Symfony\Component\ErrorHandler\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
use Symfony\Component\ErrorHandler\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
use Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorRenderer\Exception\FlattenException;

/**
* A generic ErrorHandler for the PHP engine.
Expand Down Expand Up @@ -144,6 +146,8 @@ public static function register(self $handler = null, bool $replace = true): sel
$handler->setExceptionHandler($p);
$prev[0]->setExceptionHandler($p);
}
} elseif (null === $prev && !\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
$handler->setExceptionHandler([$handler, 'sendPhpResponse']);
} else {
$handler->setExceptionHandler($prev);
}
Expand Down Expand Up @@ -320,7 +324,7 @@ public function throwAt(int $levels, bool $replace = false): int
public function scopeAt(int $levels, bool $replace = false): int
{
$prev = $this->scopedErrors;
$this->scopedErrors = (int) $levels;
$this->scopedErrors = $levels;
if (!$replace) {
$this->scopedErrors |= $prev;
}
Expand Down Expand Up @@ -358,7 +362,7 @@ public function traceAt(int $levels, bool $replace = false): int
public function screamAt(int $levels, bool $replace = false): int
{
$prev = $this->screamedErrors;
$this->screamedErrors = (int) $levels;
$this->screamedErrors = $levels;
if (!$replace) {
$this->screamedErrors |= $prev;
}
Expand Down Expand Up @@ -683,6 +687,29 @@ public static function handleFatalError(array $error = null): void
}
}

/**
* Sends the error associated with the given Exception as a plain PHP response.
*
* As this method is mainly called during Kernel boot, where nothing is yet
* available, the Response content is always HTML.
*/
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));
}

if (class_exists(HtmlErrorRenderer::class)) {
echo (new HtmlErrorRenderer(true))->render(FlattenException::createFromThrowable($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);
}
}

/**
* Gets the fatal error handlers.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/ErrorHandler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"require-dev": {
"symfony/http-kernel": "^3.4|^4.0|^5.0"
},
"suggest": {
"symfony/error-renderer": "For better error rendering"
},
"conflict": {
"symfony/http-kernel": "<3.4"
},
Expand Down