Browse Source

Add previous exception trace in BaseErrorHandler logging

Marc Ypes 7 years ago
parent
commit
98a46df904
2 changed files with 54 additions and 6 deletions
  1. 27 6
      src/Error/BaseErrorHandler.php
  2. 27 0
      tests/TestCase/Error/ErrorHandlerTest.php

+ 27 - 6
src/Error/BaseErrorHandler.php

@@ -355,12 +355,33 @@ abstract class BaseErrorHandler
      */
     protected function _getMessage(Exception $exception)
     {
+        $message = $this->getMessageForException($exception);
+
+        $request = Router::getRequest();
+        if ($request) {
+            $message .= $this->_requestContext($request);
+        }
+
+        return $message;
+    }
+
+    /**
+     * Generate the message for the exception
+     *
+     * @param \Exception $exception The exception to log a message for.
+     * @param bool $isPrevious False for original exception, true for previous
+     * @return string Error message
+     */
+    protected function getMessageForException($exception, $isPrevious = false)
+    {
         $exception = $exception instanceof PHP7ErrorException ?
             $exception->getError() :
             $exception;
         $config = $this->_options;
+
         $message = sprintf(
-            '[%s] %s in %s on line %s',
+            '%s[%s] %s in %s on line %s',
+            $isPrevious ? "\nCaused by: " : '',
             get_class($exception),
             $exception->getMessage(),
             $exception->getFile(),
@@ -375,15 +396,15 @@ abstract class BaseErrorHandler
             }
         }
 
-        $request = Router::getRequest();
-        if ($request) {
-            $message .= $this->_requestContext($request);
-        }
-
         if (!empty($config['trace'])) {
             $message .= "\nStack Trace:\n" . $exception->getTraceAsString() . "\n\n";
         }
 
+        $previous = $exception->getPrevious();
+        if ($previous) {
+            $message .= $this->getMessageForException($previous, true);
+        }
+
         return $message;
     }
 

+ 27 - 0
tests/TestCase/Error/ErrorHandlerTest.php

@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\Error;
 
 use Cake\Core\Configure;
 use Cake\Core\Plugin;
+use Cake\Datasource\Exception\RecordNotFoundException;
 use Cake\Error\ErrorHandler;
 use Cake\Error\PHP7ErrorException;
 use Cake\Http\Exception\ForbiddenException;
@@ -346,6 +347,32 @@ class ErrorHandlerTest extends TestCase
     }
 
     /**
+     * test logging attributes with previous exception
+     *
+     * @return void
+     */
+    public function testHandleExceptionLogPrevious()
+    {
+        $errorHandler = new TestErrorHandler([
+            'log' => true,
+            'trace' => true,
+        ]);
+
+        $previous = new \Cake\Datasource\Exception\RecordNotFoundException('Previous logged');
+        $error = new \Cake\Http\Exception\NotFoundException('Kaboom!', null, $previous);
+
+        $this->_logger->expects($this->at(0))
+            ->method('log')
+            ->with('error', $this->logicalAnd(
+                $this->stringContains('[Cake\Http\Exception\NotFoundException] Kaboom!'),
+                $this->stringContains('Caused by: [Cake\Datasource\Exception\RecordNotFoundException] Previous logged'),
+                $this->stringContains('ErrorHandlerTest->testHandleExceptionLogPrevious')
+            ));
+
+        $errorHandler->handleException($error);
+    }
+
+    /**
      * test handleException generating log.
      *
      * @return void