@@ -89,6 +89,11 @@ final class ErrorHandler
8989 \E_WARNING => 'E_WARNING ' ,
9090 ];
9191
92+ /**
93+ * @var array<int, class-string<Throwable>>
94+ */
95+ private $ exceptionsTypesFor404 = [];
96+
9297 public function __construct (callable $ emailCallback )
9398 {
9499 $ this ->emailCallback = $ emailCallback ;
@@ -296,7 +301,11 @@ public function exceptionHandler(Throwable $exception): void
296301
297302 // @codeCoverageIgnoreStart
298303 if (! \headers_sent ()) {
299- \header ('HTTP/1.1 500 Internal Server Error ' );
304+ $ header = 'HTTP/1.1 500 Internal Server Error ' ;
305+ if (\in_array (\get_class ($ exception ), $ this ->exceptionsTypesFor404 , true )) {
306+ $ header = 'HTTP/1.1 404 Not Found ' ;
307+ }
308+ \header ($ header );
300309 }
301310 // @codeCoverageIgnoreEnd
302311
@@ -305,12 +314,16 @@ public function exceptionHandler(Throwable $exception): void
305314
306315 public function renderHtmlException (Throwable $ exception ): string
307316 {
308- $ ajax = (isset ($ _SERVER ) && isset ($ _SERVER ['X_REQUESTED_WITH ' ]) && 'XMLHttpRequest ' === $ _SERVER ['X_REQUESTED_WITH ' ]);
309- $ output = '' ;
317+ $ ajax = (isset ($ _SERVER ) && isset ($ _SERVER ['X_REQUESTED_WITH ' ]) && 'XMLHttpRequest ' === $ _SERVER ['X_REQUESTED_WITH ' ]);
318+ $ output = '' ;
319+ $ errorType = '500: Internal Server Error ' ;
320+ if (\in_array (\get_class ($ exception ), $ this ->exceptionsTypesFor404 , true )) {
321+ $ errorType = '404: Not Found ' ;
322+ }
310323 if (! $ ajax ) {
311- $ output .= '<!DOCTYPE html><html><head><title>500: Internal Server Error </title></head><body> ' ;
324+ $ output .= \sprintf ( '<!DOCTYPE html><html><head><title>%s </title></head><body> ' , $ errorType ) ;
312325 }
313- $ output .= '<h1>500: Internal Server Error </h1> ' ;
326+ $ output .= \sprintf ( '<h1>%s </h1> ' , $ errorType ) ;
314327 $ output .= \PHP_EOL ;
315328 if ($ this ->displayErrors ()) {
316329 $ currentEx = $ exception ;
@@ -457,4 +470,20 @@ private function purgeTrace(string $trace): string
457470 {
458471 return \defined ('ROOT_PATH ' ) ? \str_replace (ROOT_PATH , '. ' , $ trace ) : $ trace ;
459472 }
473+
474+ /**
475+ * @param array<int, class-string<Throwable>> $exceptionsTypesFor404
476+ */
477+ public function set404ExceptionTypes (array $ exceptionsTypesFor404 ): void
478+ {
479+ $ this ->exceptionsTypesFor404 = $ exceptionsTypesFor404 ;
480+ }
481+
482+ /**
483+ * @return array<int, class-string<Throwable>>
484+ */
485+ public function get404ExceptionTypes (): array
486+ {
487+ return $ this ->exceptionsTypesFor404 ;
488+ }
460489}
0 commit comments