juliangut / slim-exception
Slim HTTP exceptions and exception handling
Installs: 5 529
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^7.1
- psr/log: ^1.0
- slim/slim: ^4.2
- willdurand/negotiation: ^2.0
Requires (Dev)
- brainmaestro/composer-git-hooks: ^2.8
- filp/whoops: ^2.4
- friendsofphp/php-cs-fixer: ^2.16
- infection/infection: ^0.13|^0.15|^0.16
- laminas/laminas-diactoros: ^2.1
- overtrue/phplint: ^1.2
- phpmd/phpmd: ^2.8
- phpstan/extension-installer: ^1.0.3
- phpstan/phpstan: ^0.12
- phpstan/phpstan-deprecation-rules: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^7.5|^8.0
- povils/phpmnd: ^2.1
- roave/security-advisories: dev-master
- sebastian/phpcpd: ^4.0|^5.0
- squizlabs/php_codesniffer: ^3.5
- thecodingmachine/phpstan-strict-rules: ^0.12
Suggests
- ext-json: In order to render json
- ext-simplexml: In order to render xml
- filp/whoops: Enhance development error reporting and production stack-traces
README
slim-exception
Alternative Slim error handling with better response format negotiation, better exception logging and better development support
Installation
Composer
composer require juliangut/slim-exception
Usage
Require composer autoload file
require './vendor/autoload.php'; use Jgut\Slim\Exception\Handler\ErrorHandler; use Jgut\Slim\Exception\Whoops\Handler\ErrorHandler as WhoopsErrorHandler; use Negotiation\Negotiator; use Slim\Factory\AppFactory; use Whoops\Run as Whoops; // Instantiate the app $app = AppFactory::create(); // ... $callableResolver = $app->getCallableResolver(); $responseFactory = $app->getResponseFactory(); $errorHandler = $inDevelopment ? new WhoopsErrorHandler($callableResolver, $responseFactory, new Negotiator(), new Whoops()) : new ErrorHandler($callableResolver, $responseFactory, new Negotiator()); // Add Error Middleware $errorMiddleware = $app->addErrorMiddleware($inDevelopment, true, true); $errorMiddleware->setDefaultErrorHandler($errorHandler); // ... $app->run();
Renderers
Custom error renderers are configured when using slim-exception error handlers. Fear not, out of the box ErrorHandler is a direct drop-in to change default Slim's ErrorHandler
You can register your error renderers or completely change them
$errorHandler = new ErrorHandler($callableResolver, $responseFactory, new Negotiator()); // Set single error renderer $errorHandler->setErrorRenderer('application/xhtml+xml', MyCustomHtmlRenderer::class); // Completely replace error renderers $errorHandler->setErrorRenderers(['text/html' => MyCustomHtmlRenderer::class]);
Whoops
Developers deserve a better and more informative error handling while in development environment
Whoops is a great tool for this purpose and its usage is integrated in this package. There is an special Whoops error handler which can be used as default exception handler for development
Given Whoops renderers are meant for development displayErrorDetails argument on Slim\Interfaces\ErrorRendererInterface::__invoke
won't be considered and stacktrace will always be displayed
The example of how to include Whoops error handler is in the code above
For you to use this handler you'll need to require whoops first. Additionally symfony's var-dumper plays nice with whoops so require it too
composer require filp/whoops
composer require symfony/var-dumper
To benefit from better and richer stack traces inside logs ErrorHandler uses Whoops if present so consider requiring Whoops in production as well, but do not use Whoops's ErrorHandler in production
Handle all errors/exceptions
In order to fully integrate error handling with the environment you can register ExceptionHandler globally. In this way any triggered and unhandled error will be captured and treated by the error handler
use Jgut\Slim\Exception\ExceptionHandler; use Slim\Factory\AppFactory; // Instantiate the app $app = AppFactory::create(); // ... // Create and register $errorHandler in error middleware $request = Psr17ServerRequestFactoryInterface::createServerRequest(); $exceptionHandler = new ExceptionHandler($request, $errorHandler, $inDevelopment, true, true); $exceptionHandler->registerHandling(); // ... $app->run($request); // This error will be captured and gracefully handled trigger_error('This is embarrasing', \E_USER_ERROR);
Upgrade from 1.x
Overall usage has been drastically simplified due to Slim 4 migration to exception based error handling, basically what slim-exception was doing in 1.x.
- Minimum Slim version is now 4.2
- ExceptionManager has been removed as its functionality is now integrated into Slim
- Exceptions no longer uses juliangut/http-exception and thus they have no identifier
- Global error/exception handling has been moved from a trait (meant for App) to its own class ExceptionHandler
Contributing
Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.
See file CONTRIBUTING.md
License
See file LICENSE included with the source code for a copy of the license terms.