cakephp: Argument 1 passed to Cake\Error\ExceptionRenderer::__construct() must be an instance of Exception

This is a (multiple allowed):

  • bug

  • CakePHP Version: 3.5.7

  • Platform and Target: PHP 7.1.11; MariaDB 10.2.11

What you did

I write this code on the controller:

$categories = TableRegistry::get('Categories');
$category = $categories->find('all');

What happened

Gives me this error:

Argument 1 passed to Cake\Error\ExceptionRenderer::__construct() must be an instance of Exception, instance of Error given, called in /home/psantos/Workspace/my_app/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php on line 167

What you expected to happen

I was expecting to get a more descriptive error info. no matter what change I do, I stil getting the same error.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (15 by maintainers)

Most upvoted comments

Perhaps this patch might fix the reported issue.

diff --git a/src/Error/Middleware/ErrorHandlerMiddleware.php b/src/Error/Middleware/ErrorHandlerMiddleware.php
index 1529c3a..d848a0b 100644
--- a/src/Error/Middleware/ErrorHandlerMiddleware.php
+++ b/src/Error/Middleware/ErrorHandlerMiddleware.php
@@ -111,6 +111,10 @@ class ErrorHandlerMiddleware
      */
     public function handleException($exception, $request, $response)
     {
+        if ($exception instanceof \Error) {
+            $exception = new \Cake\Error\PHP7ErrorException($exception);
+        }
+
         $renderer = $this->getRenderer($exception);
         try {
             $res = $renderer->render();

BaseErrorHandler does similar.

IMO the bigger issue is some php7 errors to not being logged, the typehint issue is minor in comparison.

Custom error handler, see e.g. DatabaseLog plugin.