From c964633278cdc5c70e4be3adfb6cbe906d1340b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Wed, 9 Mar 2016 11:42:26 +0100 Subject: [PATCH] Provide diagnostics for JSON encoding errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #11705 Signed-off-by: Michal Čihař --- libraries/Response.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/libraries/Response.php b/libraries/Response.php index f66982da3a..f7f1f85c54 100644 --- a/libraries/Response.php +++ b/libraries/Response.php @@ -364,7 +364,40 @@ class Response // response correctly. PMA_headerJSON(); - echo json_encode($this->_JSON); + $result = json_encode($this->_JSON); + if ($result === false) { + switch (json_last_error()) { + case JSON_ERROR_NONE: + $error = 'No errors'; + break; + case JSON_ERROR_DEPTH: + $error = 'Maximum stack depth exceeded'; + break; + case JSON_ERROR_STATE_MISMATCH: + $error = 'Underflow or the modes mismatch'; + break; + case JSON_ERROR_CTRL_CHAR: + $error = 'Unexpected control character found'; + break; + case JSON_ERROR_SYNTAX: + $error = 'Syntax error, malformed JSON'; + break; + case JSON_ERROR_UTF8: + $error = 'Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + default: + $error = 'Unknown error'; + break; + } + echo json_encode( + array( + 'success' => false, + 'error' => 'JSON encoding failed: ' . $error, + ) + ); + } else { + echo $result; + } } /**