Merge pull request #2354 from fabrixxm/issues/api-errors

api: throw HTTPException instead of calling api_error directly
This commit is contained in:
Michael Vogel 2016-02-18 07:26:33 +01:00
commit f9e2543133

View File

@ -161,10 +161,7 @@
if (!isset($_SERVER['PHP_AUTH_USER'])) { if (!isset($_SERVER['PHP_AUTH_USER'])) {
logger('API_login: ' . print_r($_SERVER,true), LOGGER_DEBUG); logger('API_login: ' . print_r($_SERVER,true), LOGGER_DEBUG);
header('WWW-Authenticate: Basic realm="Friendica"'); header('WWW-Authenticate: Basic realm="Friendica"');
header('HTTP/1.0 401 Unauthorized'); throw new UnauthorizedException("This API requires login");
die((api_error($a, 'json', "This api requires login")));
//die('This api requires login');
} }
$user = $_SERVER['PHP_AUTH_USER']; $user = $_SERVER['PHP_AUTH_USER'];
@ -216,8 +213,9 @@
if((! $record) || (! count($record))) { if((! $record) || (! count($record))) {
logger('API_login failure: ' . print_r($_SERVER,true), LOGGER_DEBUG); logger('API_login failure: ' . print_r($_SERVER,true), LOGGER_DEBUG);
header('WWW-Authenticate: Basic realm="Friendica"'); header('WWW-Authenticate: Basic realm="Friendica"');
header('HTTP/1.0 401 Unauthorized'); #header('HTTP/1.0 401 Unauthorized');
die('This api requires login'); #die('This api requires login');
throw new UnauthorizedException("This API requires login");
} }
authenticate_success($record); $_SESSION["allow_api"] = true; authenticate_success($record); $_SESSION["allow_api"] = true;
@ -331,7 +329,8 @@
* *
* @param Api $a * @param Api $a
* @param string $type Return type (xml, json, rss, as) * @param string $type Return type (xml, json, rss, as)
* @param string $error Error message * @param HTTPException $error Error object
* @return strin error message formatted as $type
*/ */
function api_error(&$a, $type, $e) { function api_error(&$a, $type, $e) {
$error = ($e->getMessage()!==""?$e->getMessage():$e->httpdesc); $error = ($e->getMessage()!==""?$e->getMessage():$e->httpdesc);
@ -903,7 +902,8 @@
if ($posts_day > $throttle_day) { if ($posts_day > $throttle_day) {
logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG);
die(api_error($a, $type, sprintf(t("Daily posting limit of %d posts reached. The post was rejected."), $throttle_day))); #die(api_error($a, $type, sprintf(t("Daily posting limit of %d posts reached. The post was rejected."), $throttle_day)));
throw new TooManyRequestsException(sprintf(t("Daily posting limit of %d posts reached. The post was rejected."), $throttle_day));
} }
} }
@ -922,7 +922,9 @@
if ($posts_week > $throttle_week) { if ($posts_week > $throttle_week) {
logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG);
die(api_error($a, $type, sprintf(t("Weekly posting limit of %d posts reached. The post was rejected."), $throttle_week))); #die(api_error($a, $type, sprintf(t("Weekly posting limit of %d posts reached. The post was rejected."), $throttle_week)));
throw new TooManyRequestsException(sprintf(t("Weekly posting limit of %d posts reached. The post was rejected."), $throttle_week));
} }
} }
@ -941,7 +943,8 @@
if ($posts_month > $throttle_month) { if ($posts_month > $throttle_month) {
logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG);
die(api_error($a, $type, sprintf(t("Monthly posting limit of %d posts reached. The post was rejected."), $throttle_month))); #die(api_error($a, $type, sprintf(t("Monthly posting limit of %d posts reached. The post was rejected."), $throttle_month)));
throw new TooManyRequestsException(sprintf(t("Monthly posting limit of %d posts reached. The post was rejected."), $throttle_month));
} }
} }
@ -1809,7 +1812,7 @@
$action_argv_id=2; $action_argv_id=2;
if ($a->argv[1]=="1.1") $action_argv_id=3; if ($a->argv[1]=="1.1") $action_argv_id=3;
if ($a->argc<=$action_argv_id) die(api_error($a, $type, t("Invalid request."))); if ($a->argc<=$action_argv_id) throw new BadRequestException("Invalid request.");
$action = str_replace(".".$type,"",$a->argv[$action_argv_id]); $action = str_replace(".".$type,"",$a->argv[$action_argv_id]);
if ($a->argc==$action_argv_id+2) { if ($a->argc==$action_argv_id+2) {
$itemid = intval($a->argv[$action_argv_id+1]); $itemid = intval($a->argv[$action_argv_id+1]);