From 3e466028022966f0cedc0a8e4a8f443e101f3e76 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 28 Mar 2018 18:16:30 +0200 Subject: [PATCH] Json: wrap json_encode to emit errors refs #1445 --- library/Director/Core/Json.php | 19 +++++++ library/Director/Core/RestApiClient.php | 2 +- .../Exception/JsonEncodeException.php | 7 +++ library/Director/Exception/JsonException.php | 57 +++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 library/Director/Core/Json.php create mode 100644 library/Director/Exception/JsonEncodeException.php create mode 100644 library/Director/Exception/JsonException.php diff --git a/library/Director/Core/Json.php b/library/Director/Core/Json.php new file mode 100644 index 00000000..5315d978 --- /dev/null +++ b/library/Director/Core/Json.php @@ -0,0 +1,19 @@ + 'The maximum stack depth has been exceeded', + JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', + JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', + JSON_ERROR_SYNTAX => 'Syntax error', + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' + ]; + if (array_key_exists($code, $map)) { + return $map[$code]; + } + + if (PHP_VERSION_ID >= 50500) { + $map = [ + JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded', + JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded', + JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given', + ]; + if (array_key_exists($code, $map)) { + return $map[$code]; + } + } + + if (PHP_VERSION_ID >= 70000) { + $map = [ + JSON_ERROR_INVALID_PROPERTY_NAME => 'A property name that cannot be encoded was given', + JSON_ERROR_UTF16 => 'Malformed UTF-16 characters, possibly incorrectly encoded', + ]; + + if (array_key_exists($code, $map)) { + return $map[$code]; + } + } + + return 'An error occured when parsing a JSON string'; + } +}