mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-09 06:36:46 +02:00
Throw exception when json encoding fails and errors are not ignored, fixes #545
This commit is contained in:
@@ -150,9 +150,44 @@ class NormalizerFormatter implements FormatterInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
|
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
|
||||||
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
$json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||||
|
} else {
|
||||||
|
$json = json_encode($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return json_encode($data);
|
if ($json === false) {
|
||||||
|
$this->throwEncodeError(json_last_error(), $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws an exception according to a given code with a customized message
|
||||||
|
*
|
||||||
|
* @param int $code return code of json_last_error function
|
||||||
|
* @param mixed $data data that was meant to be encoded
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
private function throwEncodeError($code, $data)
|
||||||
|
{
|
||||||
|
switch ($code) {
|
||||||
|
case JSON_ERROR_DEPTH:
|
||||||
|
$msg = 'Maximum stack depth exceeded';
|
||||||
|
break;
|
||||||
|
case JSON_ERROR_STATE_MISMATCH:
|
||||||
|
$msg = 'Underflow or the modes mismatch';
|
||||||
|
break;
|
||||||
|
case JSON_ERROR_CTRL_CHAR:
|
||||||
|
$msg = 'Unexpected control character found';
|
||||||
|
break;
|
||||||
|
case JSON_ERROR_UTF8:
|
||||||
|
$msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$msg = 'Unknown error';
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \RuntimeException('JSON encoding failed: '.$msg.'. Encoding: '.var_export($data, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -174,6 +174,17 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(@json_encode(array($resource)), $res);
|
$this->assertEquals(@json_encode(array($resource)), $res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException RuntimeException
|
||||||
|
*/
|
||||||
|
public function testThrowsOnInvalidEncoding()
|
||||||
|
{
|
||||||
|
$formatter = new NormalizerFormatter();
|
||||||
|
$reflMethod = new \ReflectionMethod($formatter, 'toJson');
|
||||||
|
$reflMethod->setAccessible(true);
|
||||||
|
$res = $reflMethod->invoke($formatter, array('message' => utf8_decode('öü')));
|
||||||
|
}
|
||||||
|
|
||||||
public function testExceptionTraceWithArgs()
|
public function testExceptionTraceWithArgs()
|
||||||
{
|
{
|
||||||
if (defined('HHVM_VERSION')) {
|
if (defined('HHVM_VERSION')) {
|
||||||
|
Reference in New Issue
Block a user