Sfoglia il codice sorgente

PHP7 compat for exception classes, fixes #718

Jordi Boggiano 10 anni fa
parent
commit
481fc4cfa1

+ 7 - 2
src/Monolog/Formatter/JsonFormatter.php

@@ -163,12 +163,17 @@ class JsonFormatter extends NormalizerFormatter
      * Normalizes given exception with or without its own stack trace based on
      * `includeStacktraces` property.
      *
-     * @param Exception $e
+     * @param Exception|Throwable $e
      *
      * @return array
      */
-    protected function normalizeException(Exception $e)
+    protected function normalizeException($e)
     {
+        // TODO 2.0 only check for Throwable
+        if (!$e instanceof Exception && !$e instanceof \Throwable) {
+            throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
+        }
+
         $data = array(
             'class' => get_class($e),
             'message' => $e->getMessage(),

+ 6 - 3
src/Monolog/Formatter/LineFormatter.php

@@ -11,8 +11,6 @@
 
 namespace Monolog\Formatter;
 
-use Exception;
-
 /**
  * Formats incoming records into a one-line string
  *
@@ -114,8 +112,13 @@ class LineFormatter extends NormalizerFormatter
         return $this->replaceNewlines($this->convertToString($value));
     }
 
-    protected function normalizeException(Exception $e)
+    protected function normalizeException($e)
     {
+        // TODO 2.0 only check for Throwable
+        if (!$e instanceof \Exception && !$e instanceof \Throwable) {
+            throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
+        }
+
         $previousText = '';
         if ($previous = $e->getPrevious()) {
             do {

+ 8 - 2
src/Monolog/Formatter/NormalizerFormatter.php

@@ -90,7 +90,8 @@ class NormalizerFormatter implements FormatterInterface
         }
 
         if (is_object($data)) {
-            if ($data instanceof Exception) {
+            // TODO 2.0 only check for Throwable
+            if ($data instanceof Exception || (PHP_VERSION_ID > 70000 && $data instanceof \Throwable)) {
                 return $this->normalizeException($data);
             }
 
@@ -112,8 +113,13 @@ class NormalizerFormatter implements FormatterInterface
         return '[unknown('.gettype($data).')]';
     }
 
-    protected function normalizeException(Exception $e)
+    protected function normalizeException($e)
     {
+        // TODO 2.0 only check for Throwable
+        if (!$e instanceof Exception && !$e instanceof \Throwable) {
+            throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
+        }
+
         $data = array(
             'class' => get_class($e),
             'message' => $e->getMessage(),