From 33efe571e3e7ebd607e92345c2e94e7fd8ae27f0 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 2 Feb 2015 19:45:11 +0100 Subject: Rework api error handling Signed-off-by: Florian Pritz --- application/controllers/api.php | 62 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 29 deletions(-) (limited to 'application/controllers/api.php') diff --git a/application/controllers/api.php b/application/controllers/api.php index 7557c6c99..490f59c2c 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -18,35 +18,39 @@ class Api extends MY_Controller { } public function route() { - $requested_version = $this->uri->segment(2); - $controller = $this->uri->segment(3); - $function = $this->uri->segment(4); - $major = intval(explode(".", $requested_version)[0]); - - if (!preg_match("/^[a-zA-Z-_]+$/", $controller)) { - return send_json_error_reply("api/invalid-controller-value", "Invalid controller requested"); - } - - if (!preg_match("/^[a-zA-Z-_]+$/", $function)) { - return send_json_error_reply("api/invalid-function-value", "Invalid function requested"); - } - - $namespace = "controllers\\api\\v".$major; - $class = $namespace."\\".$controller; - $class_info = $namespace."\\api_info"; - - if (!class_exists($class_info) || version_compare($class_info::get_version(), $requested_version, "<")) { - return send_json_error_reply("api/version-not-supported", "Requested API version is not supported"); - } - - if (!class_exists($class)) { - return send_json_error_reply("api/unknown-controller", "Unknown controller requested"); - } - - $c= new $class; - if (!method_exists($c, $function)) { - return send_json_error_reply("api/unknown-function", "Unknown function requested"); + try { + $requested_version = $this->uri->segment(2); + $controller = $this->uri->segment(3); + $function = $this->uri->segment(4); + $major = intval(explode(".", $requested_version)[0]); + + if (!preg_match("/^[a-zA-Z-_]+$/", $controller)) { + throw new \exceptions\PublicApiException("api/invalid-controller-value", "Invalid controller requested"); + } + + if (!preg_match("/^[a-zA-Z-_]+$/", $function)) { + throw new \exceptions\PublicApiException("api/invalid-function-value", "Invalid function requested"); + } + + $namespace = "controllers\\api\\v".$major; + $class = $namespace."\\".$controller; + $class_info = $namespace."\\api_info"; + + if (!class_exists($class_info) || version_compare($class_info::get_version(), $requested_version, "<")) { + throw new \exceptions\PublicApiException("api/version-not-supported", "Requested API version is not supported"); + } + + if (!class_exists($class)) { + throw new \exceptions\PublicApiException("api/unknown-controller", "Unknown controller requested"); + } + + $c= new $class; + if (!method_exists($c, $function)) { + throw new \exceptions\PublicApiException("api/unknown-function", "Unknown function requested"); + } + return $c->$function(); + } catch (\exceptions\PublicApiException $e) { + return send_json_error_reply($e->get_error_id(), $e->getMessage(), $e->get_data()); } - return $c->$function(); } } -- cgit v1.2.3-24-g4f1b