diff --git a/Framework/App.php b/Framework/App.php index bf3cb46..514fae4 100644 --- a/Framework/App.php +++ b/Framework/App.php @@ -5,8 +5,9 @@ use DI\ContainerBuilder; use DI\NotFoundException; use Exception; -use Framework\Exceptions\SystemException; +use Framework\Errors\Exceptions\SystemException; use Framework\Factories\ContainerFactory; +use Framework\Factories\ErrorMiddlewareFactory; use Framework\Factories\StaticInstancierFactory; use Framework\Middlewares\Internals\FileUploadErrorDetectionMiddleware; use Framework\Middlewares\Internals\RequestParametersCustomsMiddleware; @@ -28,10 +29,15 @@ final class App implements RequestHandlerInterface { public const VERSION = '1.1'; - private const INTERNAL_MIDDLEWARES = [ - FileUploadErrorDetectionMiddleware::class, - RequestParametersCustomsMiddleware::class, - ]; + //List of internal middlewares + private function getInternalMiddlewares(): array + { + return [ + FileUploadErrorDetectionMiddleware::class, + RequestParametersCustomsMiddleware::class, + ErrorMiddlewareFactory::make($this->getContainer()) + ]; + } /** * Router de l'application @@ -65,7 +71,7 @@ public function run(ServerRequestInterface $request, Router $router): ResponseIn $this->index = 0; $this->hasHandledGenericMiddlewares = false; - $this->middlewares = array_merge(self::INTERNAL_MIDDLEWARES, $router->getMiddlewares()); + $this->middlewares = array_merge($this->getInternalMiddlewares(), $router->getMiddlewares()); $this->runTimeRoute = $router; try { @@ -193,4 +199,4 @@ private function error(...$exceptions) } die; } -} \ No newline at end of file +} diff --git a/Framework/Exceptions/SystemException.php b/Framework/Errors/Exceptions/SystemException.php similarity index 95% rename from Framework/Exceptions/SystemException.php rename to Framework/Errors/Exceptions/SystemException.php index 6807689..bac319d 100644 --- a/Framework/Exceptions/SystemException.php +++ b/Framework/Errors/Exceptions/SystemException.php @@ -1,5 +1,5 @@ SENTRY_DSN, + 'capture_silenced_errors' => true, + 'environment' => ENV, + 'release' => RELEASE, + ]); +} diff --git a/Framework/Factories/ErrorMiddlewareFactory.php b/Framework/Factories/ErrorMiddlewareFactory.php new file mode 100644 index 0000000..b465b57 --- /dev/null +++ b/Framework/Factories/ErrorMiddlewareFactory.php @@ -0,0 +1,98 @@ + SENTRY_DSN, + 'capture_silenced_errors' => true, + 'environment' => ENV, + 'release' => RELEASE, + ]); + } + + if ($container->has(AuthenticationInterface::class)) { + $authentification = $container->get(AuthenticationInterface::class); + if ($authentification->isLogged()) { + $user = $authentification->getUser(); + configureScope(function (Scope $scope) use ($user): void { + $scope->setUser([ + 'id' => $user->getId(), + 'username' => $user->getUsername(), + 'email' => $user->email ?? null, + ]); + }); + } + } + + $whoops->appendHandler(new CallbackHandler(function (\Throwable $error) { + captureException($error); + })); + } + + if (!PRODUCTION) { + $whoops->appendHandler(new PrettyPageHandler); + } else { + $whoops->appendHandler(new CallbackHandler(function () use ($container) { + self::productionErrorHandler($container); + })); + } + + $whoops->register(); + + return new Whoops($whoops); + } + + public static function productionErrorHandler(ContainerInterface $container): void + { + ob_end_flush(); + http_response_code(500); + try { + require 'templates/errors/500.php'; + } catch (Exception $e) { + echo << + +
+ + + +Une erreur est survenue lors du chargement de cette page.
+
+ De plus, une erreur est survenue lors de l'affichage de la page d'erreur:
+
+ {$e->__toString()}
+