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

This commit is contained in:
Phil E. Taylor 2020-07-04 17:22:56 +01:00 committed by Fabien Potencier
parent e6e1ca38c0
commit 6e1d16b44d
3 changed files with 23 additions and 2 deletions

View File

@ -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
-----

View File

@ -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
@ -134,7 +136,7 @@ class HtmlErrorRenderer implements ErrorRendererInterface
$statusCode = $this->escape($exception->getStatusCode());
if (!$debug) {
return $this->include('views/error.html.php', [
return $this->include(self::$template, [
'statusText' => $statusText,
'statusCode' => $statusCode,
]);
@ -347,8 +349,19 @@ class HtmlErrorRenderer implements ErrorRendererInterface
{
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;
}
}

View File

@ -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