From 0ebe4d5d3ef7210df2319e01817a497e2793b5ab Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 16 Sep 2014 14:42:25 +0200 Subject: [PATCH] Added checks for malformed JSON data --- src/Helpers/HttpHelper.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Helpers/HttpHelper.php b/src/Helpers/HttpHelper.php index 884cfa4e..b3da9448 100644 --- a/src/Helpers/HttpHelper.php +++ b/src/Helpers/HttpHelper.php @@ -20,7 +20,12 @@ class HttpHelper public function outputJSON($data) { - $this->output(json_encode((array) $data)); + $encodedJson = json_encode((array) $data); + $lastError = json_last_error(); + if ($lastError !== JSON_ERROR_NONE) + $this->output('Fatal error while encoding JSON: ' . $lastError . PHP_EOL . PHP_EOL . print_r($data, true)); + else + $this->output($encodedJson); } public function getRequestHeaders()