PHP8: better exception handling

Looks like PHP8 handles exceptions differently with Throwables used in cases where php-error occoured.
To fix this Throwable are now used to catch exception in routeRequest and any instance of Throwable will be converted to Exception.
This commit is contained in:
sessingo
2023-04-08 19:49:51 +02:00
parent 5ac747374b
commit fa05d64a76
+7 -3
View File
@@ -59,7 +59,7 @@ class Router
* when a route is being processed.
* @var array
*/
protected array$routeStack = [];
protected array $routeStack = [];
/**
* List of added bootmanagers
@@ -345,7 +345,7 @@ class Router
try {
/* Verify csrf token for request */
$this->csrfVerifier->handle($this->request);
} catch(Exception $e) {
} catch (Exception $e) {
return $this->handleException($e);
}
}
@@ -443,10 +443,14 @@ class Router
}
}
} catch (Exception $e) {
} catch (\Throwable $e) {
if ($e instanceof Exception) {
return $this->handleException($e);
}
return $this->handleException(new Exception($e->getMessage(), $e->getCode()));
}
if ($methodNotAllowed === true) {
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUrl()->getPath(), $this->request->getMethod());
return $this->handleException(new NotFoundHttpException($message, 403));