Skip to content

Commit

Permalink
Met a jour la gestion des erreurs
Browse files Browse the repository at this point in the history
  • Loading branch information
GribouilleVert committed Apr 25, 2021
1 parent d3be94e commit ddb7143
Show file tree
Hide file tree
Showing 12 changed files with 1,498 additions and 63 deletions.
20 changes: 13 additions & 7 deletions Framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -193,4 +199,4 @@ private function error(...$exceptions)
}
die;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Framework\Exceptions;
namespace Framework\Errors\Exceptions;

class SystemException extends \Exception {

Expand Down Expand Up @@ -45,4 +45,4 @@ public function getSeverityText(): ?string
}
}

}
}
10 changes: 10 additions & 0 deletions Framework/Errors/sentry_global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

if (SENTRY_DSN AND SENTRY_ALL) {
Sentry\init([
'dsn' => SENTRY_DSN,
'capture_silenced_errors' => true,
'environment' => ENV,
'release' => RELEASE,
]);
}
98 changes: 98 additions & 0 deletions Framework/Factories/ErrorMiddlewareFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
namespace Framework\Factories;

use Exception;
use Framework\Guard\AuthenticationInterface;
use Middlewares\Whoops;
use Psr\Container\ContainerInterface;
use Sentry\State\Scope;
use Whoops\Handler\CallbackHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
use function Sentry\captureException;
use function Sentry\configureScope;
use function Sentry\init;
use const PRODUCTION;
use const SENTRY_ALL;
use const SENTRY_DSN;

class ErrorMiddlewareFactory {

public static function make(ContainerInterface $container)
{
$whoops = new Run();

if (is_string(SENTRY_DSN)) {
if (!SENTRY_ALL) {
init([
'dsn' => 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 <<<HTML
<!doctype html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Errur 500</title>
</head>
<body>
<h1>Erreur 500</h1>
<p>Une erreur est survenue lors du chargement de cette page.</p>
<p>
<b>De plus, une erreur est survenue lors de l'affichage de la page d'erreur:</b>
<br>
{$e->__toString()}
</p>
</body>
</html>
HTML;
}
die;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Framework\Middlewares\Internals;

use Framework\Exceptions\SystemException;
use Framework\Errors\Exceptions\SystemException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Framework\Middlewares\Internals;

use Framework\Exceptions\SystemException;
use Framework\Errors\Exceptions\SystemException;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down
4 changes: 2 additions & 2 deletions Framework/System/views/error.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Framework\Exceptions\SystemException;
use Framework\Errors\Exceptions\SystemException;

/**
* @var SystemException[] $exceptions
Expand Down Expand Up @@ -47,4 +47,4 @@
</div>
</main>
</body>
</html>
</html>
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"files": [
"config/constants.php",
"Framework/Errors/sentry_global.php",
"Framework/functions.php",
"App/functions.php"
]
Expand All @@ -30,7 +31,8 @@
"laminas/laminas-httphandlerrunner": "^1",
"robmorgan/phinx": "^0.12.4",
"pagerfanta/pagerfanta": "^2.7",
"altorouter/altorouter": "^2.0"
"altorouter/altorouter": "^2.0",
"sentry/sdk": "^3.1"
},
"require-dev": {
"middlewares/whoops": "^2",
Expand Down
Loading

0 comments on commit ddb7143

Please sign in to comment.