Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix normalizing an exception that wraps a throwable
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Dec 2, 2020
commit 8d8da79932a768b1e87a37b1a1c86ffb48806bd6
8 changes: 5 additions & 3 deletions src/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace InterfaSys\LogNormalizer;

use Throwable;

/**
* Converts any variable to a String
*
Expand Down Expand Up @@ -252,7 +254,7 @@ private function normalizeDate($data) {
*/
private function normalizeObject($data, $depth) {
if (is_object($data)) {
if ($data instanceof \Exception) {
if ($data instanceof Throwable) {
return $this->normalizeException($data);
}
// We don't need to go too deep in the recursion
Expand All @@ -275,11 +277,11 @@ private function normalizeObject($data, $depth) {
/**
* Converts an Exception to String
*
* @param \Exception $exception
* @param Throwable $exception
*
* @return string[]
*/
private function normalizeException(\Exception $exception) {
private function normalizeException(Throwable $exception) {
$data = [
'class' => get_class($exception),
'message' => $exception->getMessage(),
Expand Down
31 changes: 31 additions & 0 deletions tests/NormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

namespace InterfaSys\LogNormalizer;

use Exception;
use PHPUnit\Framework\TestCase;
use TypeError;
use function get_class;

/**
Expand Down Expand Up @@ -309,6 +311,35 @@ public function testFormatExceptions() {
);
}

public function testFormatExceptionWithPreviousThrowable() {
$t = new TypeError("not a type error");
$e = new Exception("an exception", 13, $t);
Comment on lines +315 to +316
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$t = new TypeError("not a type error");
$e = new Exception("an exception", 13, $t);
$t = new TypeError('not a type error');
$e = new Exception('an exception', 13, $t);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to add the cs fixer later on 😉


$normalized = $this->normalizer->normalize([
'exception' => $e,
]);

self::assertEquals(
[
'exception' => [
'class' => get_class($e),
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile() . ':' . $e->getLine(),
'trace' => $e->getTraceAsString(),
'previous' => [
'class' => 'TypeError',
'message' => 'not a type error',
'code' => 0,
'file' => $t->getFile() . ':' . $t->getLine(),
'trace' => $t->getTraceAsString(),
]
]
], $normalized
);
self::assertTrue(isset($normalized['exception']['previous']));
}

public function testUnknown() {
$data = fopen('php://memory', 'rb');
fclose($data);
Expand Down