From 96ef0dccf8fbb19e3def1668ffca990cbd895622 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 9 Feb 2017 19:50:04 +0100 Subject: [PATCH] Make JSON error handling logic reusable refs #2728 --- .../Exception/Json/JsonDecodeException.php | 11 +++ .../Exception/Json/JsonEncodeException.php | 11 +++ .../Icinga/Exception/Json/JsonException.php | 13 +++ library/Icinga/Util/Json.php | 80 +++++++++++++++++++ .../Command/Transport/ApiCommandTransport.php | 2 +- .../Exception/JsonDecodeException.php | 55 ------------- .../Monitoring/Web/Rest/RestRequest.php | 10 +-- 7 files changed, 118 insertions(+), 64 deletions(-) create mode 100644 library/Icinga/Exception/Json/JsonDecodeException.php create mode 100644 library/Icinga/Exception/Json/JsonEncodeException.php create mode 100644 library/Icinga/Exception/Json/JsonException.php create mode 100644 library/Icinga/Util/Json.php delete mode 100644 modules/monitoring/library/Monitoring/Exception/JsonDecodeException.php diff --git a/library/Icinga/Exception/Json/JsonDecodeException.php b/library/Icinga/Exception/Json/JsonDecodeException.php new file mode 100644 index 000000000..978eb308b --- /dev/null +++ b/library/Icinga/Exception/Json/JsonDecodeException.php @@ -0,0 +1,11 @@ +=')) { + return json_last_error_msg(); + } + + // All possible error codes before PHP 5.5.0 (except JSON_ERROR_NONE) + switch (json_last_error()) { + case JSON_ERROR_DEPTH: + return 'Maximum stack depth exceeded'; + case JSON_ERROR_STATE_MISMATCH: + return 'State mismatch (invalid or malformed JSON)'; + case JSON_ERROR_CTRL_CHAR: + return 'Control character error, possibly incorrectly encoded'; + case JSON_ERROR_SYNTAX: + return 'Syntax error'; + case JSON_ERROR_UTF8: + return 'Malformed UTF-8 characters, possibly incorrectly encoded'; + default: + return 'Unknown error'; + } + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php b/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php index c3d4cd4c1..1f88bebd9 100644 --- a/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php +++ b/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php @@ -4,11 +4,11 @@ namespace Icinga\Module\Monitoring\Command\Transport; use Icinga\Application\Logger; +use Icinga\Exception\Json\JsonDecodeException; use Icinga\Module\Monitoring\Command\IcingaApiCommand; use Icinga\Module\Monitoring\Command\IcingaCommand; use Icinga\Module\Monitoring\Command\Renderer\IcingaApiCommandRenderer; use Icinga\Module\Monitoring\Exception\CommandTransportException; -use Icinga\Module\Monitoring\Exception\JsonDecodeException; use Icinga\Module\Monitoring\Web\Rest\RestRequest; /** diff --git a/modules/monitoring/library/Monitoring/Exception/JsonDecodeException.php b/modules/monitoring/library/Monitoring/Exception/JsonDecodeException.php deleted file mode 100644 index a39370ad1..000000000 --- a/modules/monitoring/library/Monitoring/Exception/JsonDecodeException.php +++ /dev/null @@ -1,55 +0,0 @@ -=') - ? json_last_error_msg() - : $this->errorCodeToMessage(json_last_error()); - } else { - $msg = $this->errorCodeToMessage($jsonError); - } - - parent::__construct('%s: %s', $msg, $invalidJson); - } - - /** - * Convert the given error code (from {@link json_last_error()}) to a human readable error message - * - * @param int $jsonError - * @return string - */ - protected function errorCodeToMessage($jsonError) - { - switch ($jsonError) { - 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'; - } - } -} diff --git a/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php b/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php index b975b68f3..adeb28567 100644 --- a/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php +++ b/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php @@ -5,7 +5,7 @@ namespace Icinga\Module\Monitoring\Web\Rest; use Exception; use Icinga\Application\Logger; -use Icinga\Module\Monitoring\Exception\JsonDecodeException; +use Icinga\Util\Json; /** * REST Request @@ -262,12 +262,6 @@ class RestRequest fclose($stream); } - $response = @json_decode($result, true); - - if ($response === null) { - throw new JsonDecodeException($result); - } - - return $response; + return Json::decode($result, true); } }