PHP Class Zend\Stratigility\Middleware\ErrorHandler

Use this middleware as the outermost (or close to outermost) middleware layer, and use it to intercept PHP errors and exceptions. The class offers two extension points: - Error response generators. - Listeners. Error response generators are callables with the following signature: function ( Throwable|Exception $e, ServerRequestInterface $request, ResponseInterface $response ) : ResponseInterface These are provided the error, and the request responsible; the response provided is the response prototype provided to the ErrorHandler instance itself, and can be used as the basis for returning an error response. An error response generator must be provided as a constructor argument; if not provided, an instance of Zend\Stratigility\Middleware\ErrorResponseGenerator will be used. Listeners use the following signature: function ( Throwable|Exception $e, ServerRequestInterface $request, ResponseInterface $response ) : void Listeners are given the error, the request responsible, and the generated error response, and can then react to them. They are best suited for logging and monitoring purposes. Listeners are attached using the attachListener() method, and triggered in the order attached.
Inheritance: implements Interop\Http\Middleware\ServerMiddlewareInterface
Show file Open project: zendframework/zend-stratigility Class Usage Examples

Public Methods

Method Description
__construct ( Psr\Http\Message\ResponseInterface $responsePrototype, callable $responseGenerator = null )
__invoke ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next ) : Psr\Http\Message\ResponseInterface Proxy to process()
attachListener ( callable $listener ) Attach an error listener.
process ( Psr\Http\Message\ServerRequestInterface $request, Interop\Http\Middleware\DelegateInterface $delegate ) : Psr\Http\Message\ResponseInterface Middleware to handle errors and exceptions in layers it wraps.

Private Methods

Method Description
createErrorHandler ( ) : callable Creates and returns a callable error handler that raises exceptions.
handleThrowable ( Throwabl\Throwable | Exceptio\Exception $e, Psr\Http\Message\ServerRequestInterface $request ) : Psr\Http\Message\ResponseInterface Handles all throwables/exceptions, generating and returning a response.
triggerListeners ( Throwabl\Throwable | Exceptio\Exception $error, Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response ) : void Trigger all error listeners.

Method Details

__construct() public method

public __construct ( Psr\Http\Message\ResponseInterface $responsePrototype, callable $responseGenerator = null )
$responsePrototype Psr\Http\Message\ResponseInterface Empty/prototype response to update and return when returning an error response.
$responseGenerator callable Callback that will generate the final error response; if none is provided, ErrorResponseGenerator is used.

__invoke() public method

Proxies to process, after first wrapping the $next argument using the CallableDelegateDecorator.
public __invoke ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next ) : Psr\Http\Message\ResponseInterface
$request Psr\Http\Message\ServerRequestInterface
$response Psr\Http\Message\ResponseInterface
$next callable
return Psr\Http\Message\ResponseInterface

attachListener() public method

Each listener receives the following three arguments: - Throwable|Exception $error - ServerRequestInterface $request - ResponseInterface $response These instances are all immutable, and the return values of listeners are ignored; use listeners for reporting purposes only.
public attachListener ( callable $listener )
$listener callable

process() public method

Adds an error handler that will convert PHP errors to ErrorException instances. Internally, wraps the call to $next() in a try/catch block, catching all PHP Throwables (PHP 7) and Exceptions (PHP 5.6 and earlier). When an exception is caught, an appropriate error response is created and returned instead; otherwise, the response returned by $next is used.
public process ( Psr\Http\Message\ServerRequestInterface $request, Interop\Http\Middleware\DelegateInterface $delegate ) : Psr\Http\Message\ResponseInterface
$request Psr\Http\Message\ServerRequestInterface
$delegate Interop\Http\Middleware\DelegateInterface
return Psr\Http\Message\ResponseInterface