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
 public function __invoke(ContainerInterface $container)
 {
     $generator = $container->has(ErrorResponseGenerator::class) ? $container->get(ErrorResponseGenerator::class) : null;
     $errorHandler = new ErrorHandler(new Response(), $generator);
     if ($container->has(LoggerInterface::class)) {
         $logger = $container->get(LoggerInterface::class);
         $errorHandler->attachListener(function (Throwable $throwable, RequestInterface $request, ResponseInterface $response) use($logger) {
             $logger->error('"{method} {uri}": {message} in {file}:{line}', ['date' => date('Y-m-d H:i:s'), 'method' => $request->getMethod(), 'uri' => (string) $request->getUri(), 'message' => $throwable->getMessage(), 'file' => $throwable->getFile(), 'line' => $throwable->getLine()]);
         });
     }
     return $errorHandler;
 }
 public function testCanProvideAlternateErrorResponseGenerator()
 {
     $generator = function ($e, $request, $response) {
         $response = $response->withStatus(400);
         $response->getBody()->write('The client messed up');
         return $response;
     };
     $this->delegate->process(Argument::type(ServerRequestInterface::class))->willThrow(new RuntimeException('Exception raised', 503));
     $this->response->withStatus(400)->will([$this->response, 'reveal']);
     $this->response->getBody()->will([$this->body, 'reveal']);
     $this->body->write('The client messed up')->shouldBeCalled();
     $middleware = new ErrorHandler($this->response->reveal(), $generator);
     $result = $middleware->process($this->request->reveal(), $this->delegate->reveal());
     $this->assertSame($this->response->reveal(), $result);
 }