diff options
author | Andrew Dolgov <[email protected]> | 2021-02-23 22:26:07 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2021-02-23 22:26:07 +0300 |
commit | 8d2e3c2528e67f8650c122f014364a34bf690d2a (patch) | |
tree | fd44203c8a5919848f689cb6caa8c0c8d0784d54 /classes | |
parent | 37d46411c77bda2b1823f7d230b06e36b7125a8d (diff) |
drop errors.php and simplify error handling
Diffstat (limited to 'classes')
-rwxr-xr-x | classes/api.php | 39 | ||||
-rwxr-xr-x | classes/feeds.php | 10 | ||||
-rwxr-xr-x | classes/handler/public.php | 14 | ||||
-rw-r--r-- | classes/pluginhandler.php | 6 | ||||
-rwxr-xr-x | classes/pref/feeds.php | 2 | ||||
-rw-r--r-- | classes/pref/labels.php | 2 | ||||
-rw-r--r-- | classes/pref/prefs.php | 2 | ||||
-rw-r--r-- | classes/pref/users.php | 2 | ||||
-rwxr-xr-x | classes/rpc.php | 19 |
9 files changed, 47 insertions, 49 deletions
diff --git a/classes/api.php b/classes/api.php index 6f3ee77db..1b3ee7d92 100755 --- a/classes/api.php +++ b/classes/api.php @@ -6,6 +6,13 @@ class API extends Handler { const STATUS_OK = 0; const STATUS_ERR = 1; + const E_API_DISABLED = "API_DISABLED"; + const E_NOT_LOGGED_IN = "NOT_LOGGED_IN"; + const E_LOGIN_ERROR = "LOGIN_ERROR"; + const E_INCORRECT_USAGE = "INCORRECT_USAGE"; + const E_UNKNOWN_METHOD = "UNKNOWN_METHOD"; + const E_OPERATION_FAILED = "E_OPERATION_FAILED"; + private $seq; private static function _param_to_bool($p) { @@ -13,9 +20,11 @@ class API extends Handler { } private function _wrap($status, $reply) { - print json_encode(array("seq" => $this->seq, - "status" => $status, - "content" => $reply)); + print json_encode([ + "seq" => $this->seq, + "status" => $status, + "content" => $reply + ]); } function before($method) { @@ -23,12 +32,12 @@ class API extends Handler { header("Content-Type: text/json"); if (empty($_SESSION["uid"]) && $method != "login" && $method != "isloggedin") { - $this->_wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN')); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_NOT_LOGGED_IN)); return false; } if (!empty($_SESSION["uid"]) && $method != "logout" && !get_pref('ENABLE_API_ACCESS')) { - $this->_wrap(self::STATUS_ERR, array("error" => 'API_DISABLED')); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_API_DISABLED)); return false; } @@ -69,13 +78,13 @@ class API extends Handler { "api_level" => self::API_LEVEL)); } else { // else we are not logged in user_error("Failed login attempt for $login from " . UserHelper::get_user_ip(), E_USER_WARNING); - $this->_wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_LOGIN_ERROR)); } } else { - $this->_wrap(self::STATUS_ERR, array("error" => "API_DISABLED")); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_API_DISABLED)); } } else { - $this->_wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_LOGIN_ERROR)); return; } } @@ -221,7 +230,7 @@ class API extends Handler { $this->_wrap(self::STATUS_OK, $headlines); } } else { - $this->_wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_INCORRECT_USAGE)); } } @@ -281,7 +290,7 @@ class API extends Handler { "updated" => $num_updated)); } else { - $this->_wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_INCORRECT_USAGE)); } } @@ -356,7 +365,7 @@ class API extends Handler { $this->_wrap(self::STATUS_OK, $articles); } else { - $this->_wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_INCORRECT_USAGE)); } } @@ -481,7 +490,7 @@ class API extends Handler { $this->_wrap($reply[0], $reply[1]); } else { - $this->_wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method)); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_UNKNOWN_METHOD, "method" => $method)); } } @@ -493,7 +502,7 @@ class API extends Handler { if (Article::_create_published_article($title, $url, $content, "", $_SESSION["uid"])) { $this->_wrap(self::STATUS_OK, array("status" => 'OK')); } else { - $this->_wrap(self::STATUS_ERR, array("error" => 'Publishing failed')); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_OPERATION_FAILED)); } } @@ -816,7 +825,7 @@ class API extends Handler { Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]); $this->_wrap(self::STATUS_OK, array("status" => "OK")); } else { - $this->_wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND")); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_OPERATION_FAILED)); } } @@ -831,7 +840,7 @@ class API extends Handler { $this->_wrap(self::STATUS_OK, array("status" => $rc)); } else { - $this->_wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); + $this->_wrap(self::STATUS_ERR, array("error" => self::E_INCORRECT_USAGE)); } } diff --git a/classes/feeds.php b/classes/feeds.php index eaedc1aee..a38cbae97 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -499,15 +499,7 @@ class Feeds extends Handler_Protected { // this is parsed by handleRpcJson() on first viewfeed() to set cdm expanded, etc $reply['runtime-info'] = RPC::make_runtime_info(); - $reply_json = json_encode($reply); - - if (!$reply_json) { - $reply_json = json_encode(["error" => ["code" => 15, - "message" => json_last_error_msg()]]); - } - - print $reply_json; - + print json_encode($reply); } private function _generate_dashboard_feed() { diff --git a/classes/handler/public.php b/classes/handler/public.php index 79dff37b5..42be6f713 100755 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -240,7 +240,7 @@ class Handler_Public extends Handler { } else { header("Content-Type: text/plain; charset=utf-8"); - print json_encode(array("error" => array("message" => "Unknown format"))); + print "Unknown format: $format."; } } @@ -290,7 +290,7 @@ class Handler_Public extends Handler { header("Location: index.php"); } else { header("Content-Type: text/json"); - print error_json(6); + print Errors::to_json(Errors::E_UNAUTHORIZED); } } @@ -408,7 +408,7 @@ class Handler_Public extends Handler { function index() { header("Content-Type: text/plain"); - print error_json(13); + print Errors::to_json(Errors::E_UNKNOWN_METHOD); } function forgotpass() { @@ -659,7 +659,7 @@ class Handler_Public extends Handler { <div class="content"> <?php - @$op = clean($_REQUEST["subop"]); + @$op = clean($_REQUEST["subop"] ?? ""); $updater = new DbUpdater(Db::pdo(), Config::get(Config::DB_TYPE), SCHEMA_VERSION); if ($op == "performupdate") { @@ -802,17 +802,17 @@ class Handler_Public extends Handler { } else { user_error("PluginHandler[PUBLIC]: Requested private method '$method' of plugin '$plugin_name'.", E_USER_WARNING); header("Content-Type: text/json"); - print error_json(6); + print Errors::to_json(Errors::E_UNAUTHORIZED); } } else { user_error("PluginHandler[PUBLIC]: Requested unknown method '$method' of plugin '$plugin_name'.", E_USER_WARNING); header("Content-Type: text/json"); - print error_json(13); + print Errors::to_json(Errors::E_UNKNOWN_METHOD); } } else { user_error("PluginHandler[PUBLIC]: Requested method '$method' of unknown plugin '$plugin_name'.", E_USER_WARNING); header("Content-Type: text/json"); - print error_json(14); + print Errors::to_json(Errors::E_UNKNOWN_PLUGIN); } } diff --git a/classes/pluginhandler.php b/classes/pluginhandler.php index 608f80dcb..75b823822 100644 --- a/classes/pluginhandler.php +++ b/classes/pluginhandler.php @@ -15,15 +15,15 @@ class PluginHandler extends Handler_Protected { $plugin->$method(); } else { user_error("Rejected ${plugin_name}->${method}(): invalid CSRF token.", E_USER_WARNING); - print error_json(6); + print Errors::to_json(Errors::E_UNAUTHORIZED); } } else { user_error("Rejected ${plugin_name}->${method}(): unknown method.", E_USER_WARNING); - print error_json(13); + print Errors::to_json(Errors::E_UNKNOWN_METHOD); } } else { user_error("Rejected ${plugin_name}->${method}(): unknown plugin.", E_USER_WARNING); - print error_json(14); + print Errors::to_json(Errors::E_UNKNOWN_PLUGIN); } } } diff --git a/classes/pref/feeds.php b/classes/pref/feeds.php index 7c3a40647..086c52697 100755 --- a/classes/pref/feeds.php +++ b/classes/pref/feeds.php @@ -561,8 +561,6 @@ class Pref_Feeds extends Handler_Protected { "all" => $this::get_ts_languages(), ] ]); - } else { - print json_encode(["error" => "FEED_NOT_FOUND"]); } } diff --git a/classes/pref/labels.php b/classes/pref/labels.php index 0b826e13f..5bc094d55 100644 --- a/classes/pref/labels.php +++ b/classes/pref/labels.php @@ -16,8 +16,6 @@ class Pref_Labels extends Handler_Protected { if ($line = $sth->fetch(PDO::FETCH_ASSOC)) { print json_encode($line); - } else { - print json_encode(["error" => "LABEL_NOT_FOUND"]); } } diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index 7ee03c21f..0d0dcadbc 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -1063,7 +1063,7 @@ class Pref_Prefs extends Handler_Protected { } } else { header("Content-Type: text/json"); - print error_json(6); + print Errors::to_json(Errors::E_UNAUTHORIZED); } } diff --git a/classes/pref/users.php b/classes/pref/users.php index f30abe001..13f808cb3 100644 --- a/classes/pref/users.php +++ b/classes/pref/users.php @@ -19,8 +19,6 @@ class Pref_Users extends Handler_Administrative { "user" => $row, "access_level_names" => $access_level_names ]); - } else { - print json_encode(["error" => "USER_NOT_FOUND"]); } } diff --git a/classes/rpc.php b/classes/rpc.php index 52d514aae..d0388a066 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -118,16 +118,22 @@ class RPC extends Handler_Protected { $_SESSION["hasSandbox"] = clean($_REQUEST["hasSandbox"]) === "true"; $_SESSION["clientTzOffset"] = clean($_REQUEST["clientTzOffset"]); - $reply = array(); + $error = Errors::E_SUCCESS; - $reply['error'] = sanity_check(); + if (get_schema_version(true) != SCHEMA_VERSION) { + $error = Errors::E_SCHEMA_MISMATCH; + } + + if ($error == Errors::E_SUCCESS) { + $reply = []; - if ($reply['error']['code'] == 0) { $reply['init-params'] = $this->make_init_params(); $reply['runtime-info'] = $this->make_runtime_info(); - } - print json_encode($reply); + print json_encode($reply); + } else { + print Errors::to_json($error); + } } /*function completeLabels() { @@ -315,10 +321,7 @@ class RPC extends Handler_Protected { $msg, 'client-js:' . $file, $line, $context); echo json_encode(array("message" => "HOST_ERROR_LOGGED")); - } else { - echo json_encode(array("error" => "MESSAGE_NOT_FOUND")); } - } function checkforupdates() { |