Skip to content

Commit

Permalink
Replaced error handler by samuelgfeller/slim-error-renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgfeller committed Apr 19, 2024
1 parent 27ca922 commit 6dacf3f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 284 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"fig/http-message-util": "^1.1",
"php": "^8.2",
"ext-pdo": "*",
"ext-json": "*"
"ext-json": "*",
"samuelgfeller/slim-error-renderer": "^1.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
Expand Down
37 changes: 13 additions & 24 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 @@ -19,7 +18,8 @@
use Selective\BasePath\BasePathMiddleware;
use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Middleware\ErrorMiddleware;
use SlimErrorRenderer\Middleware\ExceptionHandlingMiddleware;
use SlimErrorRenderer\Middleware\NonFatalErrorHandlingMiddleware;

return [
'settings' => function () {
Expand Down Expand Up @@ -74,34 +74,23 @@
},

// 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
);

$errorMiddleware->setDefaultErrorHandler(
$container->get(\App\Application\ErrorHandler\DefaultApiErrorHandler::class)
ExceptionHandlingMiddleware::class => function (ContainerInterface $container) {
$settings = $container->get('settings');

return new ExceptionHandlingMiddleware(
$container->get(ResponseFactoryInterface::class),
$settings['error']['log_errors'] ? $container->get(LoggerInterface::class) : null,
$settings['error']['display_error_details'],
$settings['public']['main_contact_email'] ?? null
);

return $errorMiddleware;
},

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

return new NonFatalErrorHandlerMiddleware(
return new NonFatalErrorHandlingMiddleware(
(bool)$config['display_error_details'],
(bool)$config['log_errors'],
$logger,
Expand Down
6 changes: 2 additions & 4 deletions config/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
// Project root dir (1 parent)
$settings['root_dir'] = dirname(__DIR__, 1);

// Error handling
// Documentation: https://github.com/samuelgfeller/slim-example-project/wiki/Error-Handling
// Enable error reporting for all errors
error_reporting(E_ALL);
// Error handling. Documentation: https://github.com/samuelgfeller/slim-example-project/wiki/Error-Handling
$settings['error'] = [
// MUST be set to false in production.
// MUST be set to false in production to prevent disclosing sensitive information.
// 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,
];

// API documentation: https://github.com/samuelgfeller/slim-example-project/wiki/API-Endpoint
Expand Down
6 changes: 3 additions & 3 deletions config/middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// Returns a response with validation errors
$app->add(App\Application\Middleware\ValidationExceptionMiddleware::class);
// Handle and log notices and warnings (throws ErrorException if displayErrorDetails is true)
$app->add(App\Application\Middleware\NonFatalErrorHandlerMiddleware::class);
// Set error handler to custom DefaultErrorHandler (defined in container.php)
$app->add(Slim\Middleware\ErrorMiddleware::class);
$app->add(\SlimErrorRenderer\Middleware\NonFatalErrorHandlingMiddleware::class);
// Add exception handling middleware
$app->add(\SlimErrorRenderer\Middleware\ExceptionHandlingMiddleware::class);

// Cross-Origin Resource Sharing (CORS) middleware - last middleware to be executed first
// and can return response early (without calling handle()) for pre-flight requests.
Expand Down
134 changes: 0 additions & 134 deletions src/Application/ErrorHandler/DefaultApiErrorHandler.php

This file was deleted.

5 changes: 4 additions & 1 deletion src/Application/Middleware/CorsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// Skips the rest of the middleware stack and returns the response
$response = $this->responseFactory->createResponse();
} else {
// Override the "Accept" header to JSON because the API only supports JSON requests
// (e.g. return a json error response even if the request is text/html)
$request = $request->withHeader('Accept', 'application/json');
// Continue with the middleware stack
$response = $handler->handle($request);
}
Expand All @@ -35,7 +38,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
->withHeader('Access-Control-Allow-Credentials', 'true')
->withHeader('Access-Control-Allow-Origin', $this->allowedOrigin ?? '')
->withHeader('Access-Control-Allow-Headers', '*')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, P ATCH, DELETE, OPTIONS')
->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->withHeader('Pragma', 'no-cache');

Expand Down
75 changes: 0 additions & 75 deletions src/Application/Middleware/NonFatalErrorHandlerMiddleware.php

This file was deleted.

This file was deleted.

0 comments on commit 6dacf3f

Please sign in to comment.