Skip to content

Commit e93947f

Browse files
committed
added 404 for not-found exceptions
1 parent cfb98bc commit e93947f

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/Mvc/Application.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ protected function onRun() : void
262262
* @param string $requestName
263263
* @return mixed
264264
* @throws MissingMethod
265-
* @throws NotFound
266265
* @throws Exception
267266
*/
268267
public function executeController( array $parameters, string $requestName = '' ): mixed
@@ -299,7 +298,26 @@ public function executeController( array $parameters, string $requestName = '' )
299298

300299
$request->setRouteParameters( $parameters );
301300

302-
return $controller->$method( $request );
301+
// Catch NotFound exceptions and redirect to 404
302+
try
303+
{
304+
return $controller->$method( $request );
305+
}
306+
catch( NotFound $e )
307+
{
308+
Log::warning( "Resource not found: " . $e->getMessage() );
309+
310+
Event::emit( new Http404( $parameters['route'] ?? 'unknown' ) );
311+
312+
return $this->executeController(
313+
array_merge(
314+
$parameters,
315+
[
316+
"Controller" => "Neuron\Mvc\Controllers\HttpCodes@code404",
317+
]
318+
)
319+
);
320+
}
303321
}
304322

305323
/**

0 commit comments

Comments
 (0)