Move System::jsonError to BaseModule->jsonError
- This will ensure headers set in BaseModule->run will be carried in jsonError scenarios - Make BaseApi->checkThrottleLimit an object method to use BaseModule->jsonError - Deprecate jsonError() method in Core\System
This commit is contained in:
parent
81279dad9e
commit
46180d7d5b
|
@ -510,4 +510,23 @@ abstract class BaseModule implements ICanHandleRequests
|
||||||
{
|
{
|
||||||
$this->httpExit(json_encode($content, $options), ICanCreateResponses::TYPE_JSON, $content_type);
|
$this->httpExit(json_encode($content, $options), ICanCreateResponses::TYPE_JSON, $content_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display a non-200 HTTP code response using JSON to encode the content and exit
|
||||||
|
*
|
||||||
|
* @param int $httpCode
|
||||||
|
* @param mixed $content
|
||||||
|
* @param string $content_type
|
||||||
|
* @return void
|
||||||
|
* @throws HTTPException\InternalServerErrorException
|
||||||
|
*/
|
||||||
|
public function jsonError(int $httpCode, $content, string $content_type = 'application/json')
|
||||||
|
{
|
||||||
|
if ($httpCode >= 400) {
|
||||||
|
$this->logger->debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => $this->args->getMethod(), 'agent' => $this->server['HTTP_USER_AGENT'] ?? '']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->response->setStatus($httpCode);
|
||||||
|
$this->jsonExit($content, $content_type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -373,6 +373,9 @@ class System
|
||||||
self::exit();
|
self::exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated since 2023.09 Use BaseModule->jsonError instead
|
||||||
|
*/
|
||||||
public static function jsonError($httpCode, $content, $content_type = 'application/json')
|
public static function jsonError($httpCode, $content, $content_type = 'application/json')
|
||||||
{
|
{
|
||||||
if ($httpCode >= 400) {
|
if ($httpCode >= 400) {
|
||||||
|
|
|
@ -57,7 +57,7 @@ class Error extends BaseFactory
|
||||||
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
||||||
|
|
||||||
$this->logError(404, $error);
|
$this->logError(404, $error);
|
||||||
System::jsonError(404, $errorObj->toArray());
|
$this->jsonError(404, $errorObj->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function UnprocessableEntity(string $error = '')
|
public function UnprocessableEntity(string $error = '')
|
||||||
|
@ -67,7 +67,7 @@ class Error extends BaseFactory
|
||||||
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
||||||
|
|
||||||
$this->logError(422, $error);
|
$this->logError(422, $error);
|
||||||
System::jsonError(422, $errorObj->toArray());
|
$this->jsonError(422, $errorObj->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Unauthorized(string $error = '', string $error_description = '')
|
public function Unauthorized(string $error = '', string $error_description = '')
|
||||||
|
@ -76,7 +76,7 @@ class Error extends BaseFactory
|
||||||
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
||||||
|
|
||||||
$this->logError(401, $error);
|
$this->logError(401, $error);
|
||||||
System::jsonError(401, $errorObj->toArray());
|
$this->jsonError(401, $errorObj->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Forbidden(string $error = '')
|
public function Forbidden(string $error = '')
|
||||||
|
@ -86,7 +86,7 @@ class Error extends BaseFactory
|
||||||
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
||||||
|
|
||||||
$this->logError(403, $error);
|
$this->logError(403, $error);
|
||||||
System::jsonError(403, $errorObj->toArray());
|
$this->jsonError(403, $errorObj->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function InternalError(string $error = '')
|
public function InternalError(string $error = '')
|
||||||
|
@ -96,6 +96,6 @@ class Error extends BaseFactory
|
||||||
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
||||||
|
|
||||||
$this->logError(500, $error);
|
$this->logError(500, $error);
|
||||||
System::jsonError(500, $errorObj->toArray());
|
$this->jsonError(500, $errorObj->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,6 @@ class Proofs extends BaseApi
|
||||||
*/
|
*/
|
||||||
protected function rawContent(array $request = [])
|
protected function rawContent(array $request = [])
|
||||||
{
|
{
|
||||||
System::jsonError(404, ['error' => 'Record not found']);
|
$this->jsonError(404, ['error' => 'Record not found']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -263,7 +263,7 @@ class Statuses extends BaseApi
|
||||||
$item['gravity'] = Item::GRAVITY_COMMENT;
|
$item['gravity'] = Item::GRAVITY_COMMENT;
|
||||||
$item['object-type'] = Activity\ObjectType::COMMENT;
|
$item['object-type'] = Activity\ObjectType::COMMENT;
|
||||||
} else {
|
} else {
|
||||||
self::checkThrottleLimit();
|
$this->checkThrottleLimit();
|
||||||
|
|
||||||
$item['gravity'] = Item::GRAVITY_PARENT;
|
$item['gravity'] = Item::GRAVITY_PARENT;
|
||||||
$item['object-type'] = Activity\ObjectType::NOTE;
|
$item['object-type'] = Activity\ObjectType::NOTE;
|
||||||
|
|
|
@ -121,7 +121,7 @@ class Update extends BaseApi
|
||||||
$item['gravity'] = Item::GRAVITY_COMMENT;
|
$item['gravity'] = Item::GRAVITY_COMMENT;
|
||||||
$item['object-type'] = Activity\ObjectType::COMMENT;
|
$item['object-type'] = Activity\ObjectType::COMMENT;
|
||||||
} else {
|
} else {
|
||||||
self::checkThrottleLimit();
|
$this->checkThrottleLimit();
|
||||||
|
|
||||||
$item['gravity'] = Item::GRAVITY_PARENT;
|
$item['gravity'] = Item::GRAVITY_PARENT;
|
||||||
$item['object-type'] = Activity\ObjectType::NOTE;
|
$item['object-type'] = Activity\ObjectType::NOTE;
|
||||||
|
|
|
@ -434,7 +434,7 @@ class BaseApi extends BaseModule
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function checkThrottleLimit()
|
public function checkThrottleLimit()
|
||||||
{
|
{
|
||||||
$uid = self::getCurrentUserID();
|
$uid = self::getCurrentUserID();
|
||||||
|
|
||||||
|
@ -447,11 +447,11 @@ class BaseApi extends BaseModule
|
||||||
$posts_day = Post::countThread($condition);
|
$posts_day = Post::countThread($condition);
|
||||||
|
|
||||||
if ($posts_day > $throttle_day) {
|
if ($posts_day > $throttle_day) {
|
||||||
Logger::notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
|
$this->logger->notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
|
||||||
$error = DI::l10n()->t('Too Many Requests');
|
$error = $this->t('Too Many Requests');
|
||||||
$error_description = DI::l10n()->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day);
|
$error_description = $this->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day);
|
||||||
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
||||||
System::jsonError(429, $errorobj->toArray());
|
$this->jsonError(429, $errorobj->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -464,10 +464,10 @@ class BaseApi extends BaseModule
|
||||||
|
|
||||||
if ($posts_week > $throttle_week) {
|
if ($posts_week > $throttle_week) {
|
||||||
Logger::notice('Weekly posting limit reached', ['uid' => $uid, 'posts' => $posts_week, 'limit' => $throttle_week]);
|
Logger::notice('Weekly posting limit reached', ['uid' => $uid, 'posts' => $posts_week, 'limit' => $throttle_week]);
|
||||||
$error = DI::l10n()->t('Too Many Requests');
|
$error = $this->t('Too Many Requests');
|
||||||
$error_description = DI::l10n()->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week);
|
$error_description = $this->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week);
|
||||||
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
||||||
System::jsonError(429, $errorobj->toArray());
|
$this->jsonError(429, $errorobj->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -480,10 +480,10 @@ class BaseApi extends BaseModule
|
||||||
|
|
||||||
if ($posts_month > $throttle_month) {
|
if ($posts_month > $throttle_month) {
|
||||||
Logger::notice('Monthly posting limit reached', ['uid' => $uid, 'posts' => $posts_month, 'limit' => $throttle_month]);
|
Logger::notice('Monthly posting limit reached', ['uid' => $uid, 'posts' => $posts_month, 'limit' => $throttle_month]);
|
||||||
$error = DI::l10n()->t('Too Many Requests');
|
$error = $this->t('Too Many Requests');
|
||||||
$error_description = DI::l10n()->tt('Monthly posting limit of %d post reached. The post was rejected.', 'Monthly posting limit of %d posts reached. The post was rejected.', $throttle_month);
|
$error_description = $this->tt('Monthly posting limit of %d post reached. The post was rejected.', 'Monthly posting limit of %d posts reached. The post was rejected.', $throttle_month);
|
||||||
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
|
||||||
System::jsonError(429, $errorobj->toArray());
|
$this->jsonError(429, $errorobj->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -135,7 +135,7 @@ class Circle extends BaseModule
|
||||||
$this->jsonExit(['status' => 'OK', 'message' => $message]);
|
$this->jsonExit(['status' => 'OK', 'message' => $message]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
DI::sysmsg()->addNotice($e->getMessage());
|
DI::sysmsg()->addNotice($e->getMessage());
|
||||||
System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
|
$this->jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,7 @@ class Friendica extends BaseModule
|
||||||
header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
|
header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
|
||||||
$this->jsonExit($data, 'application/activity+json');
|
$this->jsonExit($data, 'application/activity+json');
|
||||||
} catch (HTTPException\NotFoundException $e) {
|
} catch (HTTPException\NotFoundException $e) {
|
||||||
System::jsonError(404, ['error' => 'Record not found']);
|
$this->jsonError(404, ['error' => 'Record not found']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,13 +45,13 @@ class NoScrape extends BaseModule
|
||||||
// view infos about a known profile (needs a login)
|
// view infos about a known profile (needs a login)
|
||||||
$which = $a->getLoggedInUserNickname();
|
$which = $a->getLoggedInUserNickname();
|
||||||
} else {
|
} else {
|
||||||
System::jsonError(403, 'Authentication required');
|
$this->jsonError(403, 'Authentication required');
|
||||||
}
|
}
|
||||||
|
|
||||||
$owner = User::getOwnerDataByNick($which);
|
$owner = User::getOwnerDataByNick($which);
|
||||||
|
|
||||||
if (empty($owner['uid'])) {
|
if (empty($owner['uid'])) {
|
||||||
System::jsonError(404, 'Profile not found');
|
$this->jsonError(404, 'Profile not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
$json_info = [
|
$json_info = [
|
||||||
|
|
|
@ -89,7 +89,7 @@ class Profile extends BaseProfile
|
||||||
header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
|
header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
|
||||||
$this->jsonExit($data, 'application/activity+json');
|
$this->jsonExit($data, 'application/activity+json');
|
||||||
} catch (HTTPException\NotFoundException $e) {
|
} catch (HTTPException\NotFoundException $e) {
|
||||||
System::jsonError(404, ['error' => 'Record not found']);
|
$this->jsonError(404, ['error' => 'Record not found']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,10 +97,10 @@ class Profile extends BaseProfile
|
||||||
// Known deleted user
|
// Known deleted user
|
||||||
$data = ActivityPub\Transmitter::getDeletedUser($this->parameters['nickname']);
|
$data = ActivityPub\Transmitter::getDeletedUser($this->parameters['nickname']);
|
||||||
|
|
||||||
System::jsonError(410, $data);
|
$this->jsonError(410, $data);
|
||||||
} else {
|
} else {
|
||||||
// Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
|
// Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
|
||||||
System::jsonError(404, []);
|
$this->jsonError(404, []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user