@@ -39,15 +39,17 @@ class HtmlErrorRenderer implements ErrorRendererInterface
39
39
private $ projectDir ;
40
40
private $ outputBuffer ;
41
41
private $ logger ;
42
+ private $ exceptionsMapping ;
42
43
43
44
private static $ template = 'views/error.html.php ' ;
44
45
45
46
/**
46
- * @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
47
+ * @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
47
48
* @param string|FileLinkFormatter|null $fileLinkFormat
48
- * @param bool|callable $outputBuffer The output buffer as a string or a callable that should return it
49
+ * @param bool|callable $outputBuffer The output buffer as a string or a callable that should return it
50
+ * @param array $exceptionsMapping An exceptions to log & status code mapping
49
51
*/
50
- public function __construct ($ debug = false , string $ charset = null , $ fileLinkFormat = null , string $ projectDir = null , $ outputBuffer = '' , LoggerInterface $ logger = null )
52
+ public function __construct ($ debug = false , string $ charset = null , $ fileLinkFormat = null , string $ projectDir = null , $ outputBuffer = '' , LoggerInterface $ logger = null , array $ exceptionsMapping = [] )
51
53
{
52
54
if (!\is_bool ($ debug ) && !\is_callable ($ debug )) {
53
55
throw new \TypeError (sprintf ('Argument 1 passed to "%s()" must be a boolean or a callable, "%s" given. ' , __METHOD__ , \gettype ($ debug )));
@@ -63,22 +65,30 @@ public function __construct($debug = false, string $charset = null, $fileLinkFor
63
65
$ this ->projectDir = $ projectDir ;
64
66
$ this ->outputBuffer = $ outputBuffer ;
65
67
$ this ->logger = $ logger ;
68
+ $ this ->exceptionsMapping = $ exceptionsMapping ;
66
69
}
67
70
68
71
/**
69
72
* {@inheritdoc}
70
73
*/
71
- public function render (\Throwable $ exception ): FlattenException
74
+ public function render (\Throwable $ throwable ): FlattenException
72
75
{
73
76
$ headers = ['Content-Type ' => 'text/html; charset= ' .$ this ->charset ];
74
- if (\is_bool ($ this ->debug ) ? $ this ->debug : ($ this ->debug )($ exception )) {
75
- $ headers ['X-Debug-Exception ' ] = rawurlencode ($ exception ->getMessage ());
76
- $ headers ['X-Debug-Exception-File ' ] = rawurlencode ($ exception ->getFile ()).': ' .$ exception ->getLine ();
77
+ if (\is_bool ($ this ->debug ) ? $ this ->debug : ($ this ->debug )($ throwable )) {
78
+ $ headers ['X-Debug-Exception ' ] = rawurlencode ($ throwable ->getMessage ());
79
+ $ headers ['X-Debug-Exception-File ' ] = rawurlencode ($ throwable ->getFile ()).': ' . $ throwable ->getLine ();
77
80
}
78
81
79
- $ exception = FlattenException::createFromThrowable ($ exception , null , $ headers );
82
+ $ statusCode = 500 ;
83
+ foreach ($ this ->exceptionsMapping as $ exception => $ config ) {
84
+ if ($ throwable instanceof $ exception && $ config ['status_code ' ]) {
85
+ $ statusCode = $ config ['status_code ' ];
86
+ break ;
87
+ }
88
+ }
89
+ $ throwable = FlattenException::createFromThrowable ($ throwable , $ statusCode , $ headers );
80
90
81
- return $ exception ->setAsString ($ this ->renderException ($ exception ));
91
+ return $ throwable ->setAsString ($ this ->renderException ($ throwable ));
82
92
}
83
93
84
94
/**
@@ -262,8 +272,6 @@ private function formatFile(string $file, int $line, string $text = null): strin
262
272
* @param string $file A file path
263
273
* @param int $line The selected line number
264
274
* @param int $srcContext The number of displayed lines around or -1 for the whole file
265
- *
266
- * @return string
267
275
*/
268
276
private function fileExcerpt (string $ file , int $ line , int $ srcContext = 3 ): string
269
277
{
0 commit comments