Browse Source

Implement the `log` option for ExceptionTrap.

Remove the config mutation as it doesn't actually do anything. The fatal
error that is triggered is caught by the global error handler not the
exception handler.
Mark Story 3 years ago
parent
commit
4be31afaad
2 changed files with 14 additions and 3 deletions
  1. 1 3
      src/Error/ExceptionTrap.php
  2. 13 0
      tests/TestCase/Error/ExceptionTrapTest.php

+ 1 - 3
src/Error/ExceptionTrap.php

@@ -348,7 +348,7 @@ class ExceptionTrap
                 break;
             }
         }
-        if ($shouldLog) {
+        if ($shouldLog && $this->_config['log']) {
             $this->logger()->log($exception, $request);
         }
         $this->dispatchEvent('Exception.beforeRender', ['exception' => $exception]);
@@ -366,8 +366,6 @@ class ExceptionTrap
      */
     public function logInternalError(Throwable $exception): void
     {
-        // Disable trace for internal errors.
-        $this->_config['trace'] = false;
         $message = sprintf(
             "[%s] %s (%s:%s)\n%s", // Keeping same message format
             get_class($exception),

+ 13 - 0
tests/TestCase/Error/ExceptionTrapTest.php

@@ -230,6 +230,19 @@ class ExceptionTrapTest extends TestCase
         $this->assertStringContainsString('nope', $logs[0]);
     }
 
+    public function testLogExceptionConfigOff()
+    {
+        Log::setConfig('test_error', [
+            'className' => 'Array',
+        ]);
+        $trap = new ExceptionTrap(['log' => false]);
+        $error = new InvalidArgumentException('nope');
+        $trap->logException($error);
+
+        $logs = Log::engine('test_error')->read();
+        $this->assertEmpty($logs);
+    }
+
     /**
      * @preserveGlobalState disabled
      * @runInSeparateProcess