8000 [ErrorHandler] Allow override of the default non-debug template · symfony/symfony@6e1d16b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e1d16b

Browse files
PhilETaylorfabpot
authored andcommitted
[ErrorHandler] Allow override of the default non-debug template
1 parent e6e1ca3 commit 6e1d16b

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/Symfony/Component/ErrorHandler/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* added the ability to set `HtmlErrorRenderer::$template` to a custom template to render when not in debug mode.
8+
49
5.1.0
510
-----
611

src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class HtmlErrorRenderer implements ErrorRendererInterface
3939
private $outputBuffer;
4040
private $logger;
4141

42+
private static $template = 'views/error.html.php';
43+
4244
/**
4345
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
4446
* @param bool|callable $outputBuffer The output buffer as a string or a callable that should return it
@@ -134,7 +136,7 @@ private function renderException(FlattenException $exception, string $debugTempl
134136
$statusCode = $this->escape($exception->getStatusCode());
135137

136138
if (!$debug) {
137-
return $this->include('views/error.html.php', [
139+
return $this->include(self::$template, [
138140
'statusText' => $statusText,
139141
'statusCode' => $statusCode,
140142
]);
@@ -347,8 +349,19 @@ private function include(string $name, array $context = []): string
347349
{
348350
extract($context, EXTR_SKIP);
349351
ob_start();
350-
include __DIR__.'/../Resources/'.$name;
352+
353+
include file_exists($name) ? $name : __DIR__.'/../Resources/'.$name;
351354

352355
return trim(ob_get_clean());
353356
}
357+
358+
/**
359+
* Allows overriding the default non-debug template.
360+
*
361+
* @param string $template path to the custom template file to render
362+
*/
363+
public static function setTemplate(string $template): void
364+
{
365+
self::$template = $template;
366+
}
354367
}

src/Symfony/Component/ErrorHandler/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Debug::enable();
2121
//ErrorHandler::register();
2222
//DebugClassLoader::enable();
2323

24+
// If you want a custom generic template when debug is not enabled
25+
// HtmlErrorRenderer::setTemplate('/path/to/custom/error.html.php');
26+
2427
$data = ErrorHandler::call(static function () use ($filename, $datetimeFormat) {
2528
// if any code executed inside this anonymous function fails, a PHP exception
2629
// will be thrown, even if the code uses the '@' PHP silence operator

0 commit comments

Comments
 (0)
0