cakephp: Custom ErrorHandlers are not used by ErrorHandlerMiddleware

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.5.3

  • Platform and Target: PHP7

What you did

Declare the following controller:

class TestsController extends AppController
{
    public function exception() {
        throw new \Exception();
    }

    public function error() {
        throw new \Error();
    }
}

Access /tests/exception and /tests/error.

What happened

While Exceptions are handled by ErrorHandlerMiddleware::handleException(), PHP7 Errors are handled by ErrorHandler::handleException().

What you expected to happen

Both Exceptions and PHP7 Errors are handled by ErrorHandlerMiddleware::handleException() if ErrorHandlerMiddleware is enabled.

Related code

https://github.com/cakephp/cakephp/blob/35f70ad2c9843b1c42afba072615ce9141d3bf45/src/Error/Middleware/ErrorHandlerMiddleware.php#L92-L99

Related issues

#8997, #9500, #9931

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 22 (22 by maintainers)

Most upvoted comments

Thank you for the clarification. I will try to overload ErrorHandlerMiddleware::__construct(). But if it is difficult, I will give up and create ExceptionLogger instead.

All PHP errors go to the existing ErrorHandler and not the middleware. Using the ErrorHandler inside the middleware presents a couple challenges.

  1. The current handleException() method directly outputs whereas the middleware needs to return a response.
  2. We don’t yet have a way to pass an ErrorHandler into the middleware.

These are solvable issues, but they should be figured out.

Yes. And if there are no problems, I would like to add the same catch blocks to Connection::transactional() too. Otherwise, if people use DatabaseLog engine, the logs are not logged if PHP7 errors happen in transactions.