Skip to content

[ErrorHandler] Allow override of the default non-debug template #37492

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
Jul 7, 2020
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/ErrorHandler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.2.0
-----

* added the ability to set `HtmlErrorRenderer::$template` to a custom template to render when not in debug mode.

5.1.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class HtmlErrorRenderer implements ErrorRendererInterface
private $outputBuffer;
private $logger;

private static $template = 'views/error.html.php';

/**
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
* @param bool|callable $outputBuffer The output buffer as a string or a callable that should return it
Expand Down Expand Up @@ -134,7 +136,7 @@ private function renderException(FlattenException $exception, string $debugTempl
$statusCode = $this->escape($exception->getStatusCode());

if (!$debug) {
return $this->include('views/error.html.php', [
return $this->include(self::$template, [
'statusText' => $statusText,
'statusCode' => $statusCode,
]);
Expand Down Expand Up @@ -347,8 +349,19 @@ private function include(string $name, array $context = []): string
{
extract($context, EXTR_SKIP);
ob_start();
include __DIR__.'/../Resources/'.$name;

include file_exists($name) ? $name : __DIR__.'/../Resources/'.$name;

return trim(ob_get_clean());
}

/**
* Allows overriding the default non-debug template.
*
* @param string $template path to the custom template file to render
*/
public static function setTemplate(string $template): void
{
self::$template = $template;
}
}
3 changes: 3 additions & 0 deletions src/Symfony/Component/ErrorHandler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Debug::enable();
//ErrorHandler::register();
//DebugClassLoader::enable();

// If you want a custom generic template when debug is not enabled
// HtmlErrorRenderer::setTemplate('/path/to/custom/error.html.php');

$data = ErrorHandler::call(static function () use ($filename, $datetimeFormat) {
// if any code executed inside this anonymous function fails, a PHP exception
// will be thrown, even if the code uses the '@' PHP silence operator
Expand Down