mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 04:37:38 +02:00
#673 Fix fatal error while normalizing with __toString method which may throw an exception
This commit is contained in:
@@ -96,7 +96,7 @@ class NormalizerFormatter implements FormatterInterface
|
|||||||
|
|
||||||
// non-serializable objects that implement __toString stringified
|
// non-serializable objects that implement __toString stringified
|
||||||
if (method_exists($data, '__toString') && !$data instanceof \JsonSerializable) {
|
if (method_exists($data, '__toString') && !$data instanceof \JsonSerializable) {
|
||||||
$value = (string) $data;
|
$value = $data->__toString();
|
||||||
} else {
|
} else {
|
||||||
// the rest is json-serialized in some way
|
// the rest is json-serialized in some way
|
||||||
$value = $this->toJson($data, true);
|
$value = $this->toJson($data, true);
|
||||||
|
@@ -78,6 +78,15 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
), $formatted);
|
), $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()
|
public function testBatchFormat()
|
||||||
{
|
{
|
||||||
$formatter = new NormalizerFormatter('Y-m-d');
|
$formatter = new NormalizerFormatter('Y-m-d');
|
||||||
@@ -268,3 +277,11 @@ class TestStreamFoo
|
|||||||
return $this->foo . ' - ' . (string) stream_get_contents($this->resource);
|
return $this->foo . ' - ' . (string) stream_get_contents($this->resource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TestToStringError
|
||||||
|
{
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
throw new \RuntimeException('Could not convert to string');
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user