Skip to content

Commit

Permalink
Seldaek#673 Fix fatal error while normalizing with __toString method …
Browse files Browse the repository at this point in the history
…which may throw an exception
  • Loading branch information
Anton Sergeyev committed Oct 25, 2015
1 parent 3d779cc commit f19d9ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Monolog/Formatter/NormalizerFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function normalize($data)

// non-serializable objects that implement __toString stringified
if (method_exists($data, '__toString') && !$data instanceof \JsonSerializable) {
$value = (string) $data;
$value = $data->__toString();
} else {
// the rest is json-serialized in some way
$value = $this->toJson($data, true);
Expand Down
17 changes: 17 additions & 0 deletions tests/Monolog/Formatter/NormalizerFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ public function testFormatExceptions()
), $formatted);
}

public function testFormatToStringExceptionHandle()
{
$formatter = new NormalizerFormatter('Y-m-d');
$this->setExpectedException('RuntimeException', 'Could not convert to string');
$formatter->format(array(
'myObject' => new TestToStringError(),
));
}

public function testBatchFormat()
{
$formatter = new NormalizerFormatter('Y-m-d');
Expand Down Expand Up @@ -268,3 +277,11 @@ public function __toString()
return $this->foo . ' - ' . (string) stream_get_contents($this->resource);
}
}

class TestToStringError
{
public function __toString()
{
throw new \RuntimeException('Could not convert to string');
}
}

0 comments on commit f19d9ce

Please sign in to comment.