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 8b00bd1b6..1f88bebd9 100644 --- a/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php +++ b/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php @@ -4,6 +4,7 @@ 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; @@ -193,12 +194,18 @@ class ApiCommandTransport implements CommandTransportInterface $this->getHost(), $this->getPort() ); - $response = RestRequest::post($this->getUriFor($command->getEndpoint())) - ->authenticateWith($this->getUsername(), $this->getPassword()) - ->sendJson() - ->noStrictSsl() - ->setPayload($command->getData()) - ->send(); + + try { + $response = RestRequest::post($this->getUriFor($command->getEndpoint())) + ->authenticateWith($this->getUsername(), $this->getPassword()) + ->sendJson() + ->noStrictSsl() + ->setPayload($command->getData()) + ->send(); + } catch (JsonDecodeException $e) { + throw new CommandTransportException('Got invalid JSON response from the Icinga 2 API: %s', $e->getMessage()); + } + if (isset($response['error'])) { throw new CommandTransportException( 'Can\'t send external Icinga command: %u %s', diff --git a/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php b/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php index 2cf25ea0a..adeb28567 100644 --- a/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php +++ b/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Monitoring\Web\Rest; use Exception; use Icinga\Application\Logger; +use Icinga\Util\Json; /** * REST Request @@ -261,35 +262,6 @@ class RestRequest fclose($stream); } - $response = @json_decode($result, true); - - if ($response === null) { - if (version_compare(PHP_VERSION, '5.5.0', '>=')) { - throw new Exception(json_last_error_msg()); - } else { - switch (json_last_error()) { - case JSON_ERROR_DEPTH: - $msg = 'The maximum stack depth has been exceeded'; - break; - case JSON_ERROR_CTRL_CHAR: - $msg = 'Control character error, possibly incorrectly encoded'; - break; - case JSON_ERROR_STATE_MISMATCH: - $msg = 'Invalid or malformed JSON'; - break; - case JSON_ERROR_SYNTAX: - $msg = 'Syntax error'; - break; - case JSON_ERROR_UTF8: - $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded'; - break; - default: - $msg = 'An error occured when parsing a JSON string'; - } - throw new Exception($msg); - } - } - - return $response; + return Json::decode($result, true); } }