From 05035cf305efb45ff634567a749f7d9c531fcb2d Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Tue, 12 Feb 2019 16:34:07 -0500 Subject: [PATCH] move getJSONErrorString to global included file --- admin.php | 10 ++++-- include/getJsonErrorString.php | 63 ++++++++++++++++++++++++++++++++++ include/tmdb_interface.php | 48 +++----------------------- 3 files changed, 74 insertions(+), 47 deletions(-) create mode 100644 include/getJsonErrorString.php diff --git a/admin.php b/admin.php index 2af1219..b856268 100644 --- a/admin.php +++ b/admin.php @@ -18,6 +18,8 @@ declare(strict_types=1); +require_once('include/getJsonErrorString.php'); + const FOLDER_PATH = '../video/unwatched'; const MAX_RECURSE_DEPTH = 5; @@ -133,10 +135,12 @@ function getUnsavedMetadata() : void $out->path_info = $path_info; $json = json_encode($out); - if ($json) + if ($json) { echo $json; - else - echo '{"errors":"error enconding json in ' . __FUNCTION__ . '"}'; + } else { + echo '{"errors":"' . __FUNCTION__ . ', error enconding json: ' + . getJsonErrorString(json_last_error()) . '"}'; + } } else { $out->errors = $g_errors; echo json_encode($out); diff --git a/include/getJsonErrorString.php b/include/getJsonErrorString.php new file mode 100644 index 0000000..39daad4 --- /dev/null +++ b/include/getJsonErrorString.php @@ -0,0 +1,63 @@ +. + */ + + +function getJsonErrorString($error_enum) : string +{ + $err = ''; + + switch($error_enum) { + case JSON_ERROR_NONE: + $err = 'No error has occurred'; + break; + case JSON_ERROR_DEPTH: + $err = 'The maximum stack depth has been exceeded'; + break; + case JSON_ERROR_STATE_MISMATCH: + $err ='Invalid or malformed JSON'; + break; + case JSON_ERROR_CTRL_CHAR: + $err = 'Control character error, possibly incorrectly encoded'; + break; + case JSON_ERROR_SYNTAX: + $err = 'Syntax error'; + break; + case JSON_ERROR_UTF8: + $err = 'Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + case JSON_ERROR_RECURSION: + $err = 'One or more recursive references in the value to be encoded'; + break; + case JSON_ERROR_INF_OR_NAN: + $err = 'One or more NAN or INF values in the value to be encoded'; + break; + case JSON_ERROR_UNSUPPORTED_TYPE: + $err = 'A value of a type that cannot be encoded was given'; + break; + case JSON_ERROR_INVALID_PROPERTY_NAME: + $err = 'A property name that cannot be encoded was given'; + break; + case JSON_ERROR_UTF16: + $err = 'Malformed UTF-16 characters, possibly incorrectly encoded'; + break; + } + + return $err; +} + +?> diff --git a/include/tmdb_interface.php b/include/tmdb_interface.php index 0726bf3..ca7da1a 100644 --- a/include/tmdb_interface.php +++ b/include/tmdb_interface.php @@ -17,6 +17,9 @@ */ +require_once('getJsonErrorString.php'); + + class tmdb_interface { const BASE_API_URL = 'https://api.themoviedb.org/3'; @@ -43,7 +46,7 @@ class tmdb_interface if ($j_err !== JSON_ERROR_NONE) { $this->errors[] = $str; - $this->errors[] = __METHOD__ . ", JSON error: " . get_json_error(json_last_error()); + $this->errors[] = __METHOD__ . ", JSON error: " . getJsonErrorString(json_last_error()); } return strlen($str); @@ -94,49 +97,6 @@ class tmdb_interface public $errors = []; - private function get_json_error($error_enum) - { - $err = ''; - - switch($error_enum) { - case JSON_ERROR_NONE: - $err = 'No error has occurred'; - break; - case JSON_ERROR_DEPTH: - $err = 'The maximum stack depth has been exceeded'; - break; - case JSON_ERROR_STATE_MISMATCH: - $err ='Invalid or malformed JSON'; - break; - case JSON_ERROR_CTRL_CHAR: - $err = 'Control character error, possibly incorrectly encoded'; - break; - case JSON_ERROR_SYNTAX: - $err = 'Syntax error'; - break; - case JSON_ERROR_UTF8: - $err = 'Malformed UTF-8 characters, possibly incorrectly encoded'; - break; - case JSON_ERROR_RECURSION: - $err = 'One or more recursive references in the value to be encoded'; - break; - case JSON_ERROR_INF_OR_NAN: - $err = 'One or more NAN or INF values in the value to be encoded'; - break; - case JSON_ERROR_UNSUPPORTED_TYPE: - $err = 'A value of a type that cannot be encoded was given'; - break; - case JSON_ERROR_INVALID_PROPERTY_NAME: - $err = 'A property name that cannot be encoded was given'; - break; - case JSON_ERROR_UTF16: - $err = 'Malformed UTF-16 characters, possibly incorrectly encoded'; - break; - } - - return $err; - } - private $start_time = 0; }