From 2ef36899e92dd3e14cedfac51de11eefe4717a5b Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 22 Dec 2016 12:21:49 +0100 Subject: [PATCH] PropertyModifierJsonDecode: fail on error... ...and give some details fixes #13733 --- .../PropertyModifierJsonDecode.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/library/Director/PropertyModifier/PropertyModifierJsonDecode.php b/library/Director/PropertyModifier/PropertyModifierJsonDecode.php index c495b414..c2fe9671 100644 --- a/library/Director/PropertyModifier/PropertyModifierJsonDecode.php +++ b/library/Director/PropertyModifier/PropertyModifierJsonDecode.php @@ -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'; + } + } }