Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Add support for PHP 7's error exceptions #42

Closed
msheakoski opened this issue Jan 14, 2016 · 3 comments
Closed

Add support for PHP 7's error exceptions #42

msheakoski opened this issue Jan 14, 2016 · 3 comments
Assignees

Comments

@msheakoski
Copy link

Since PHP 7 now converts errors into exceptions, it would be useful to add a catch (Error $e) to https://github.com/zendframework/zend-stratigility/blob/master/src/Dispatch.php#L83-L85 so that non-thrown errors like 1/0 are also caught and sent to error middleware.

This is very useful for using middleware to send errors to Sentry or display them in Whoops or BooBoo.

@msheakoski
Copy link
Author

Just in case anybody needs a quick hack to work around this issue, I moved the initialization of Whoops into the middleware's constructor, which causes set_error_handler() to be called when the middleware is added via $app->pipe(new WhoopsMiddleware). Because Whoops's internal error handler converts errors into exceptions, the "ErrorException" gets handled the same way as thrown exceptions.

<?php // WhoopsMiddleware.php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Stratigility\ErrorMiddlewareInterface;

class WhoopsErrorHandler implements ErrorMiddlewareInterface
{
    protected $whoops;

    public function __construct()
    {
        $this->whoops = new Run;
        $this->whoops->pushHandler(new PrettyPageHandler);
        $this->whoops->register();
    }

    public function __invoke($error, Request $request, Response $response, callable $next = null)
    {
        $method = Run::EXCEPTION_HANDLER;
        ob_start();
        $this->whoops->$method($error);
        return new HtmlResponse(ob_get_clean(), 500);
    }
}

@nesl247
Copy link

nesl247 commented Mar 15, 2016

Looks like there is a PR for this #49.

@Ocramius
Copy link
Member

Marking as duplicate

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants