Skip to content

Commit

Permalink
Added logError and logErrorDetails variables on the Shutdown handler
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoninoM90 committed Apr 7, 2024
1 parent 0ef0154 commit abf659a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
36 changes: 30 additions & 6 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Application\ResponseEmitter\ResponseEmitter;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Psr\Log\LoggerInterface;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;

Expand All @@ -16,7 +17,7 @@
$containerBuilder = new ContainerBuilder();

if (false) { // Should be set to true in production
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
}

// Set up settings
Expand Down Expand Up @@ -50,8 +51,16 @@
/** @var SettingsInterface $settings */
$settings = $container->get(SettingsInterface::class);

/** @var LoggerInterface $logger */
$logger = $container->get(LoggerInterface::class);

/** @var bool $displayErrorDetails */
$displayErrorDetails = $settings->get('displayErrorDetails');

/** @var bool $logError */
$logError = $settings->get('logError');

/** @var bool $logErrorDetails */
$logErrorDetails = $settings->get('logErrorDetails');

// Create Request object from globals
Expand All @@ -60,10 +69,20 @@

// Create Error Handler
$responseFactory = $app->getResponseFactory();
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);

// Create Shutdown Handler
$shutdownHandler = new ShutdownHandler($request, $errorHandler, $displayErrorDetails);
$errorHandler = new HttpErrorHandler(
$callableResolver,
$responseFactory,
$logger
);

// Create and register Shutdown Handler
$shutdownHandler = new ShutdownHandler(
$request,
$errorHandler,
$displayErrorDetails,
$logError,
$logErrorDetails
);
register_shutdown_function($shutdownHandler);

// Add Routing Middleware
Expand All @@ -73,7 +92,12 @@
$app->addBodyParsingMiddleware();

// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logError, $logErrorDetails);
$errorMiddleware = $app->addErrorMiddleware(
$displayErrorDetails,
$logError,
$logErrorDetails,
$logger
);
$errorMiddleware->setDefaultErrorHandler($errorHandler);

// Run App & Emit Response
Expand Down
30 changes: 20 additions & 10 deletions src/Application/Handlers/ShutdownHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,41 @@ class ShutdownHandler

private bool $displayErrorDetails;

private bool $logError;

private bool $logErrorDetails;

public function __construct(
Request $request,
Request $request,
HttpErrorHandler $errorHandler,
bool $displayErrorDetails
bool $displayErrorDetails,
bool $logError,
bool $logErrorDetails
) {
$this->request = $request;
$this->errorHandler = $errorHandler;
$this->request = $request;
$this->errorHandler = $errorHandler;
$this->displayErrorDetails = $displayErrorDetails;
$this->logError = $logError;
$this->logErrorDetails = $logErrorDetails;
}

public function __invoke()
{
$error = error_get_last();

if (!$error) {
return;
}

$message = $this->getErrorMessage($error);
$message = $this->getErrorMessage($error);
$exception = new HttpInternalServerErrorException($this->request, $message);

$response = $this->errorHandler->__invoke(
$this->request,
$exception,
$this->displayErrorDetails,
false,
false,
$this->logError,
$this->logErrorDetails,
);

$responseEmitter = new ResponseEmitter();
Expand All @@ -53,10 +63,10 @@ private function getErrorMessage(array $error): string
return 'An error while processing your request. Please try again later.';
}

$errorFile = $error['file'];
$errorLine = $error['line'];
$errorFile = $error['file'];
$errorLine = $error['line'];
$errorMessage = $error['message'];
$errorType = $error['type'];
$errorType = $error['type'];

if ($errorType === E_USER_ERROR) {
return "FATAL ERROR: {$errorMessage}. on line {$errorLine} in file {$errorFile}.";
Expand Down

0 comments on commit abf659a

Please sign in to comment.