Skip to content

Commit

Permalink
Added samuelgfeller/slim-error-renderer [SLE-192]
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgfeller committed Apr 21, 2024
1 parent 4bae86b commit c3eeac8
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 392 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"odan/session": "^6",
"nyholm/psr7": "^1",
"nyholm/psr7-server": "^1",
"samuelgfeller/slim-error-renderer": "^1.0",
"fig/http-message-util": "^1",
"php": "^8.2",
"ext-pdo": "*",
Expand Down
44 changes: 16 additions & 28 deletions config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Documentation: https://github.com/samuelgfeller/slim-example-project/wiki/Dependency-Injection.
*/

use App\Application\Middleware\NonFatalErrorHandlerMiddleware;
use App\Infrastructure\Utility\Settings;
use Cake\Database\Connection;
use Monolog\Formatter\LineFormatter;
Expand All @@ -23,8 +22,9 @@
use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Interfaces\RouteParserInterface;
use Slim\Middleware\ErrorMiddleware;
use Slim\Views\PhpRenderer;
use SlimErrorRenderer\Middleware\ExceptionHandlingMiddleware;
use SlimErrorRenderer\Middleware\NonFatalErrorHandlingMiddleware;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\EventListener\EnvelopeListener;
Expand Down Expand Up @@ -91,38 +91,26 @@
return $logger->pushHandler($rotatingFileHandler);
},

// Error handling: https://github.com/samuelgfeller/slim-example-project/wiki/Error-Handling
// Set error handler to custom DefaultErrorHandler
ErrorMiddleware::class => function (ContainerInterface $container) {
$config = $container->get('settings')['error'];
$app = $container->get(App::class);

$logger = $container->get(LoggerInterface::class);

$errorMiddleware = new ErrorMiddleware(
$app->getCallableResolver(),
$app->getResponseFactory(),
(bool)$config['display_error_details'],
(bool)$config['log_errors'],
(bool)$config['log_error_details'],
$logger
);
// Add custom exception handling middleware
ExceptionHandlingMiddleware::class => function (ContainerInterface $container) {
$settings = $container->get('settings');

$errorMiddleware->setDefaultErrorHandler(
$container->get(\App\Application\ErrorHandler\DefaultErrorHandler::class)
return new ExceptionHandlingMiddleware(
$container->get(ResponseFactoryInterface::class),
$settings['error']['log_errors'] ? $container->get(LoggerInterface::class) : null,
$settings['error']['display_error_details'],
$settings['public']['email']['main_contact_email'] ?? null,
// Get autowired prod error page renderer with layout and translations
$container->get(\App\Application\ErrorRenderer\ProdErrorPageRenderer::class)
);

return $errorMiddleware;
},
// Add error middleware for notices and warnings
NonFatalErrorHandlerMiddleware::class => function (ContainerInterface $container) {
NonFatalErrorHandlingMiddleware::class => function (ContainerInterface $container) {
$settings = $container->get('settings')['error'];
$logger = $container->get(LoggerInterface::class);

return new NonFatalErrorHandlerMiddleware(
(bool)$settings['display_error_details'],
(bool)$settings['log_errors'],
$logger,
return new NonFatalErrorHandlingMiddleware(
$settings['display_error_details'],
$settings['log_errors'] ? $container->get(LoggerInterface::class) : null,
);
},

Expand Down
3 changes: 1 addition & 2 deletions config/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
// When set to true, it shows error details and throws an ErrorException for notices and warnings.
'display_error_details' => false,
'log_errors' => true,
'log_error_details' => true,
];

// Deployment settings
Expand All @@ -49,7 +48,7 @@
$settings['public'] = [
'app_name' => 'Slim Example Project',
'email' => [
'main_contact_address' => '[email protected]',
'main_contact_email' => '[email protected]',
'main_sender_address' => '[email protected]',
'main_sender_name' => 'Slim Example Project',
],
Expand Down
12 changes: 6 additions & 6 deletions config/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use App\Application\Middleware\ForbiddenExceptionMiddleware;
use App\Application\Middleware\InvalidOperationExceptionMiddleware;
use App\Application\Middleware\NonFatalErrorHandlerMiddleware;
use App\Application\Middleware\PhpViewMiddleware;
use App\Application\Middleware\ValidationExceptionMiddleware;
use Odan\Session\Middleware\SessionStartMiddleware;
use Selective\BasePath\BasePathMiddleware;
use Slim\App;
use Slim\Middleware\ErrorMiddleware;
use SlimErrorRenderer\Middleware\ExceptionHandlingMiddleware;
use SlimErrorRenderer\Middleware\NonFatalErrorHandlingMiddleware;

// Slim middlewares are LIFO (last in, first out) so when responding, the order is backwards
// https://github.com/samuelgfeller/slim-example-project/wiki/Middleware#order-of-execution
Expand Down Expand Up @@ -42,8 +42,8 @@
$app->add(ValidationExceptionMiddleware::class);
$app->add(ForbiddenExceptionMiddleware::class);
$app->add(InvalidOperationExceptionMiddleware::class);
// Handle and log notices and warnings (throws ErrorException if displayErrorDetails is true)
$app->add(NonFatalErrorHandlerMiddleware::class);
// Set error handler to custom DefaultErrorHandler (defined in container.php)
$app->add(ErrorMiddleware::class);
// Handle and log notices and warnings (throw ErrorException if displayErrorDetails is true)
$app->add(NonFatalErrorHandlingMiddleware::class);
// Handle exceptions and display error page
$app->add(ExceptionHandlingMiddleware::class);
};
Loading

0 comments on commit c3eeac8

Please sign in to comment.