From 1329d01280bd23c29d3547b4926b73ac66a9e18f Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 1 Mar 2016 04:25:14 +0100 Subject: [PATCH] ActionController: helper method for JSON errors --- .../Web/Controller/ActionController.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/library/Director/Web/Controller/ActionController.php b/library/Director/Web/Controller/ActionController.php index f726532d..bec63532 100644 --- a/library/Director/Web/Controller/ActionController.php +++ b/library/Director/Web/Controller/ActionController.php @@ -166,6 +166,27 @@ abstract class ActionController extends Controller $this->prepareTable($name)->render('list/table', null, true); } + // TODO: just return json_last_error_msg() for PHP >= 5.5.0 + protected function getLastJsonError() + { + switch (json_last_error()) { + 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'; + } + + return $this; + } + protected function api($endpointName = null) { if ($this->api === null) {