public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
try {
return $next($request, $response);
} catch (Exception $e) {
if ($this->logger) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
$type = $this->type($request);
$response = $response->withHeader('Content-Type', $type);
try {
if (method_exists($e, 'getHttpStatus')) {
$code = $e->getHttpStatus();
} else {
$code = $e->getCode();
}
$response = $response->withStatus($code);
} catch (InvalidArgumentException $_) {
// Exception did not contain a valid code
$response = $response->withStatus(500);
}
if ($e instanceof HttpException) {
$response = $e->withResponse($response);
}
$handler = $this->handler($type);
$this->whoops->pushHandler($handler);
$body = $this->whoops->handleException($e);
$response->getBody()->write($body);
$this->whoops->popHandler();
return $response;
}
}