PropertyModifierJsonDecode: fail on error...

...and give some details

fixes #13733
This commit is contained in:
Thomas Gelf 2016-12-22 12:21:49 +01:00
parent 8fa9bafd6e
commit 2ef36899e9
1 changed files with 22 additions and 2 deletions

View File

@ -36,7 +36,7 @@ class PropertyModifierJsonDecode extends PropertyModifierHook
}
$decoded = @json_decode($value);
if ($decoded === null && JSON_ERROR_NONE === json_last_error()) {
if ($decoded === null && JSON_ERROR_NONE !== json_last_error()) {
switch ($this->getSetting('on_failure')) {
case 'null':
return null;
@ -45,7 +45,8 @@ class PropertyModifierJsonDecode extends PropertyModifierHook
case 'fail':
default:
throw new InvalidPropertyException(
'JSON decoding failed for %s',
'JSON decoding failed with "%s" for %s',
$this->getLastJsonError(),
$value
);
}
@ -53,4 +54,23 @@ class PropertyModifierJsonDecode extends PropertyModifierHook
return $decoded;
}
// TODO: just return json_last_error_msg() for PHP >= 5.5.0
protected function getLastJsonError()
{
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
return 'The maximum stack depth has been exceeded';
case JSON_ERROR_CTRL_CHAR:
return 'Control character error, possibly incorrectly encoded';
case JSON_ERROR_STATE_MISMATCH:
return 'Invalid or malformed JSON';
case JSON_ERROR_SYNTAX:
return 'Syntax error';
case JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
default:
return 'An error occured when parsing a JSON string';
}
}
}