ソースを参照

Only log exception attributes when debug is on.

Elide the exception attributes when debug is false.

Refs #6957
Mark Story 10 年 前
コミット
91a18c2e17
2 ファイル変更43 行追加1 行削除
  1. 3 1
      src/Error/BaseErrorHandler.php
  2. 40 0
      tests/TestCase/Error/ErrorHandlerTest.php

+ 3 - 1
src/Error/BaseErrorHandler.php

@@ -259,7 +259,9 @@ abstract class BaseErrorHandler
             get_class($exception),
             get_class($exception),
             $exception->getMessage()
             $exception->getMessage()
         );
         );
-        if (method_exists($exception, 'getAttributes')) {
+        $debug = Configure::read('debug');
+
+        if ($debug && method_exists($exception, 'getAttributes')) {
             $attributes = $exception->getAttributes();
             $attributes = $exception->getAttributes();
             if ($attributes) {
             if ($attributes) {
                 $message .= "\nException Attributes: " . var_export($exception->getAttributes(), true);
                 $message .= "\nException Attributes: " . var_export($exception->getAttributes(), true);

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

@@ -26,6 +26,7 @@ use Cake\Network\Exception\ForbiddenException;
 use Cake\Network\Exception\NotFoundException;
 use Cake\Network\Exception\NotFoundException;
 use Cake\Network\Request;
 use Cake\Network\Request;
 use Cake\Network\Response;
 use Cake\Network\Response;
+use Cake\Routing\Exception\MissingControllerException;
 use Cake\Routing\Router;
 use Cake\Routing\Router;
 use Cake\TestSuite\TestCase;
 use Cake\TestSuite\TestCase;
 
 
@@ -281,6 +282,45 @@ class ErrorHandlerTest extends TestCase
     }
     }
 
 
     /**
     /**
+     * test logging attributes with/without debug
+     *
+     * @return void
+     */
+    public function testHandleExceptionLogAttributes()
+    {
+        $errorHandler = new TestErrorHandler([
+            'log' => true,
+            'trace' => true,
+        ]);
+
+        $error = new MissingControllerException(['class' => 'Derp']);
+
+        $this->_logger->expects($this->at(0))
+            ->method('log')
+            ->with('error', $this->logicalAnd(
+                $this->stringContains(
+                    '[Cake\Routing\Exception\MissingControllerException] ' .
+                    'Controller class Derp could not be found.'
+                ),
+                $this->stringContains('Exception Attributes:')
+            ));
+
+        $this->_logger->expects($this->at(1))
+            ->method('log')
+            ->with('error', $this->logicalAnd(
+                $this->stringContains(
+                    '[Cake\Routing\Exception\MissingControllerException] ' .
+                    'Controller class Derp could not be found.'
+                ),
+                $this->logicalNot($this->stringContains('Exception Attributes:'))
+            ));
+        $errorHandler->handleException($error);
+
+        Configure::write('debug', false);
+        $errorHandler->handleException($error);
+    }
+
+    /**
      * test handleException generating log.
      * test handleException generating log.
      *
      *
      * @return void
      * @return void