项目作者: middlewares

项目描述 :
PSR-15 middleware to handle http errors
高级语言: PHP
项目地址: git://github.com/middlewares/error-handler.git
创建时间: 2016-10-03T12:02:54Z
项目社区:https://github.com/middlewares/error-handler

开源协议:MIT License

下载


middlewares/error-handler

Latest Version on Packagist
Software License
Testing
Total Downloads

Middleware to catch and format errors encountered while handling the request.

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/error-handler.

  1. composer require middlewares/error-handler

Example

  1. use Middlewares\ErrorFormatter;
  2. use Middlewares\ErrorHandler;
  3. use Middlewares\Utils\Dispatcher;
  4. // Create a new ErrorHandler instance
  5. // Any number of formatters can be added. One will be picked based on the Accept
  6. // header of the request. If no formatter matches, the first formatter in the array
  7. // will be used.
  8. $errorHandler = new ErrorHandler([
  9. new ErrorFormatter\HtmlFormatter(),
  10. new ErrorFormatter\ImageFormatter(),
  11. new ErrorFormatter\JsonFormatter(),
  12. new ErrorFormatter\PlainFormatter(),
  13. new ErrorFormatter\SvgFormatter(),
  14. new ErrorFormatter\XmlFormatter(),
  15. ]);
  16. // ErrorHandler should always be the first middleware in the stack!
  17. $dispatcher = new Dispatcher([
  18. $errorHandler,
  19. // ...
  20. function ($request) {
  21. throw HttpErrorException::create(404);
  22. }
  23. ]);
  24. $request = $serverRequestFactory->createServerRequest('GET', '/');
  25. $response = $dispatcher->dispatch($request);

Usage

Add the formatters to be used (instances of Middlewares\ErrorFormatter\FormatterInterface). If no formatters are provided, use all available.

  1. $errorHandler = new ErrorHandler([
  2. new ErrorFormatter\HtmlFormatter(),
  3. new ErrorFormatter\JsonFormatter()
  4. ]);

Note: If no formatter is found, the first value of the array will be used. In the example above, HtmlFormatter.

How to log the error and delegate the formatting to the middleware

Please note that the following snippet must go even before error-hander’s middleware, which usually goes first.

  1. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  2. {
  3. try {
  4. return $handler->handle($request);
  5. } catch (Throwable $exception) {
  6. $this->logger->critical('Uncaught {error}', [
  7. 'error' => $exception->getMessage(),
  8. 'exception' => $exception, // If you use Monolog, this is correct
  9. ]);
  10. // leave it for the middleware
  11. throw $exception;
  12. }
  13. }

How to use a custom response for Production

This snippet might come handy when you want to customize your response in production.

  1. class PrettyPage implements StreamFactoryInterface
  2. {
  3. public function createStream(string $content = ''): StreamInterface
  4. {
  5. return Factory::createStream('<strong>Pretty page</strong>');
  6. }
  7. public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
  8. {
  9. // This is safe as the Middleware only uses createStream()
  10. throw new Exception('Not implemented');
  11. }
  12. public function createStreamFromResource($resource): StreamInterface
  13. {
  14. // This is safe as the Middleware only uses createStream()
  15. throw new Exception('Not implemented');
  16. }
  17. }
  18. $errorHandler = new ErrorHandler([
  19. new HtmlFormatter(
  20. null,
  21. new PrettyPage,
  22. ),
  23. ]);

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.