2017 © Pedro Peláez
 

library error-handler

Middleware to handle http errors

image

middlewares/error-handler

Middleware to handle http errors

  • Monday, June 25, 2018
  • by oscarotero
  • Repository
  • 2 Watchers
  • 1 Stars
  • 1,868 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 73 % Grown

The README.md

middlewares/error-handler

Latest Version on Packagist ![Software License][ico-license] Testing ![Total Downloads][ico-downloads], (*1)

Middleware to catch and format errors encountered while handling the request., (*2)

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/error-handler., (*3)

composer require middlewares/error-handler

Example

use Middlewares\ErrorFormatter;
use Middlewares\ErrorHandler;
use Middlewares\Utils\Dispatcher;

// Create a new ErrorHandler instance
// Any number of formatters can be added. One will be picked based on the Accept
// header of the request. If no formatter matches, the first formatter in the array
// will be used.
$errorHandler = new ErrorHandler([
    new ErrorFormatter\HtmlFormatter(),
    new ErrorFormatter\ImageFormatter(),
    new ErrorFormatter\JsonFormatter(),
    new ErrorFormatter\PlainFormatter(),
    new ErrorFormatter\SvgFormatter(),
    new ErrorFormatter\XmlFormatter(),
]);

// ErrorHandler should always be the first middleware in the stack!
$dispatcher = new Dispatcher([
    $errorHandler,
    // ...
    function ($request) {
        throw HttpErrorException::create(404);
    }
]);

$request = $serverRequestFactory->createServerRequest('GET', '/');
$response = $dispatcher->dispatch($request);

Usage

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

$errorHandler = new ErrorHandler([
    new ErrorFormatter\HtmlFormatter(),
    new ErrorFormatter\JsonFormatter()
]);

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


Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details., (*6)

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

The Versions