14
14
use Psr \Log \LoggerInterface ;
15
15
use Psr \Log \LogLevel ;
16
16
use Symfony \Component \ErrorHandler \Exception \FatalErrorException ;
17
- use Symfony \Component \ErrorHandler \Exception \FatalThrowableError ;
18
17
use Symfony \Component \ErrorHandler \Exception \OutOfMemoryException ;
19
18
use Symfony \Component \ErrorHandler \Exception \SilencedErrorContext ;
20
19
use Symfony \Component \ErrorHandler \FatalErrorHandler \ClassNotFoundFatalErrorHandler ;
@@ -266,7 +265,7 @@ public function setLoggers(array $loggers): array
266
265
267
266
if ($ flush ) {
268
267
foreach ($ this ->bootstrappingLogger ->cleanLogs () as $ log ) {
269
- $ type = $ log [2 ]['exception ' ] instanceof \ErrorException ? $ log [ 2 ][ ' exception ' ]-> getSeverity () : E_ERROR ;
268
+ $ type = ThrowableUtils:: getSeverity ( $ log [2 ]['exception ' ]) ;
270
269
if (!isset ($ flush [$ type ])) {
271
270
$ this ->bootstrappingLogger ->log ($ log [0 ], $ log [1 ], $ log [2 ]);
272
271
} elseif ($ this ->loggers [$ type ][0 ]) {
@@ -281,7 +280,7 @@ public function setLoggers(array $loggers): array
281
280
/**
282
281
* Sets a user exception handler.
283
282
*
284
- * @param callable|null $handler A handler that will be called on Exception
283
+ * @param callable|null $handler A handler that must support \Throwable instances that will be called on Exception
285
284
*
286
285
* @return callable|null The previous exception handler
287
286
*/
@@ -540,57 +539,64 @@ public function handleError(int $type, string $message, string $file, int $line)
540
539
/**
541
540
* Handles an exception by logging then forwarding it to another handler.
542
541
*
543
- * @param \Exception|\Throwable $exception An exception to handle
544
- * @param array $error An array as returned by error_get_last()
542
+ * @param array $error An array as returned by error_get_last()
545
543
*
546
544
* @internal
547
545
*/
548
- public function handleException ($ exception , array $ error = null )
546
+ public function handleException (\ Throwable $ exception , array $ error = null )
549
547
{
550
548
if (null === $ error ) {
551
549
self ::$ exitCode = 255 ;
552
550
}
553
- if (!$ exception instanceof \Exception) {
554
- $ exception = new FatalThrowableError ($ exception );
555
- }
556
- $ type = $ exception instanceof FatalErrorException ? $ exception ->getSeverity () : E_ERROR ;
551
+
552
+ $ type = ThrowableUtils::getSeverity ($ exception );
557
553
$ handlerException = null ;
558
554
559
- if (($ this ->loggedErrors & $ type ) || $ exception instanceof FatalThrowableError ) {
555
+ if (($ this ->loggedErrors & $ type ) || $ exception instanceof \Error ) {
560
556
if (false !== strpos ($ message = $ exception ->getMessage (), "class@anonymous \0" )) {
561
557
$ message = $ this ->parseAnonymousClass ($ message );
562
558
}
559
+
563
560
if ($ exception instanceof FatalErrorException) {
564
- if ($ exception instanceof FatalThrowableError) {
565
- $ error = [
566
- 'type ' => $ type ,
567
- 'message ' => $ message ,
568
- 'file ' => $ exception ->getFile (),
569
- 'line ' => $ exception ->getLine (),
570
- ];
571
- } else {
572
- $ message = 'Fatal ' .$ message ;
573
- }
561
+ $ message = 'Fatal ' .$ message ;
574
562
} elseif ($ exception instanceof \ErrorException) {
575
563
$ message = 'Uncaught ' .$ message ;
564
+ } elseif ($ exception instanceof \Error) {
565
+ $ error = [
566
+ 'type ' => $ type ,
567
+ 'message ' => $ message ,
568
+ 'file ' => $ exception ->getFile (),
569
+ 'line ' => $ exception ->getLine (),
570
+ ];
571
+ $ message = 'Uncaught Error: ' .$ message ;
576
572
} else {
577
573
$ message = 'Uncaught Exception: ' .$ message ;
578
574
}
579
575
}
576
+
580
577
if ($ this ->loggedErrors & $ type ) {
581
578
try {
582
579
$ this ->loggers [$ type ][0 ]->log ($ this ->loggers [$ type ][1 ], $ message , ['exception ' => $ exception ]);
583
580
} catch (\Throwable $ handlerException ) {
584
581
}
585
582
}
583
+
584
+ // temporary until fatal error handlers rework
585
+ $ originalException = $ exception ;
586
+ if (!$ exception instanceof \Exception) {
587
+ $ exception = new FatalErrorException ($ exception ->getMessage (), $ exception ->getCode (), $ type , $ exception ->getFile (), $ exception ->getLine (), null , true , $ exception ->getTrace ());
588
+ }
589
+
586
590
if ($ exception instanceof FatalErrorException && !$ exception instanceof OutOfMemoryException && $ error ) {
587
591
foreach ($ this ->getFatalErrorHandlers () as $ handler ) {
588
592
if ($ e = $ handler ->handleError ($ error , $ exception )) {
589
- $ exception = $ e ;
593
+ $ convertedException = $ e ;
590
594
break ;
591
595
}
592
596
}
593
597
}
598
+
599
+ $ exception = $ convertedException ?? $ originalException ;
594
600
$ exceptionHandler = $ this ->exceptionHandler ;
595
601
if ((!\is_array ($ exceptionHandler ) || !$ exceptionHandler [0 ] instanceof self || 'sendPhpResponse ' !== $ exceptionHandler [1 ]) && !\in_array (\PHP_SAPI , ['cli ' , 'phpdbg ' ], true )) {
596
602
$ this ->exceptionHandler = [$ this , 'sendPhpResponse ' ];
0 commit comments