@@ -592,7 +592,11 @@ public function handleException($exception, array $error = null)
592
592
}
593
593
}
594
594
$ exceptionHandler = $ this ->exceptionHandler ;
595
- $ this ->exceptionHandler = null ;
595
+ if ((!\is_array ($ exceptionHandler ) || !$ exceptionHandler [0 ] instanceof self || 'sendPhpResponse ' !== $ exceptionHandler [1 ]) && !\in_array (\PHP_SAPI , ['cli ' , 'phpdbg ' ], true )) {
596
+ $ this ->exceptionHandler = [$ this , 'sendPhpResponse ' ];
597
+ } else {
598
+ $ this ->exceptionHandler = null ;
599
+ }
596
600
try {
597
601
if (null !== $ exceptionHandler ) {
598
602
return $ exceptionHandler ($ exception );
@@ -696,18 +700,28 @@ public static function handleFatalError(array $error = null): void
696
700
private function sendPhpResponse (\Throwable $ exception )
697
701
{
698
702
$ charset = ini_get ('default_charset ' ) ?: 'UTF-8 ' ;
699
-
700
- if (!headers_sent ()) {
701
- header ('HTTP/1.0 500 ' );
702
- header (sprintf ('Content-Type: text/html; charset=%s ' , $ charset ));
703
- }
703
+ $ statusCode = 500 ;
704
+ $ headers = [];
704
705
705
706
if (class_exists (HtmlErrorRenderer::class)) {
706
- echo (new HtmlErrorRenderer (true ))->render (FlattenException::createFromThrowable ($ exception ));
707
+ $ exception = FlattenException::createFromThrowable ($ exception );
708
+ $ statusCode = $ exception ->getStatusCode ();
709
+ $ headers = $ exception ->getHeaders ();
710
+ $ response = (new HtmlErrorRenderer (true ))->render ($ exception );
707
711
} else {
708
712
$ message = htmlspecialchars ($ exception ->getMessage (), ENT_COMPAT | ENT_SUBSTITUTE , $ charset );
709
- echo sprintf ('<!DOCTYPE html><html><head><meta charset="%s" /><meta name="robots" content="noindex,nofollow" /></head><body>%s</body></html> ' , $ charset , $ message );
713
+ $ response = sprintf ('<!DOCTYPE html><html><head><meta charset="%s" /><meta name="robots" content="noindex,nofollow" /></head><body>%s</body></html> ' , $ charset , $ message );
710
714
}
715
+
716
+ if (!headers_sent ()) {
717
+ header (sprintf ('HTTP/1.0 %s ' , $ statusCode ));
718
+ foreach ($ headers as $ name => $ value ) {
719
+ header ($ name .': ' .$ value , false );
720
+ }
721
+ header ('Content-Type: text/html; charset= ' .$ charset );
722
+ }
723
+
724
+ echo $ response ;
711
725
}
712
726
713
727
/**
0 commit comments