- 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->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);
+ }
}
self::exit();
}
+ /**
+ * @deprecated since 2023.09 Use BaseModule->jsonError instead
+ */
public static function jsonError($httpCode, $content, $content_type = 'application/json')
{
if ($httpCode >= 400) {
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
$this->logError(404, $error);
- System::jsonError(404, $errorObj->toArray());
+ $this->jsonError(404, $errorObj->toArray());
}
public function UnprocessableEntity(string $error = '')
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
$this->logError(422, $error);
- System::jsonError(422, $errorObj->toArray());
+ $this->jsonError(422, $errorObj->toArray());
}
public function Unauthorized(string $error = '', string $error_description = '')
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
$this->logError(401, $error);
- System::jsonError(401, $errorObj->toArray());
+ $this->jsonError(401, $errorObj->toArray());
}
public function Forbidden(string $error = '')
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
$this->logError(403, $error);
- System::jsonError(403, $errorObj->toArray());
+ $this->jsonError(403, $errorObj->toArray());
}
public function InternalError(string $error = '')
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
$this->logError(500, $error);
- System::jsonError(500, $errorObj->toArray());
+ $this->jsonError(500, $errorObj->toArray());
}
}
*/
protected function rawContent(array $request = [])
{
- System::jsonError(404, ['error' => 'Record not found']);
+ $this->jsonError(404, ['error' => 'Record not found']);
}
}
$item['gravity'] = Item::GRAVITY_COMMENT;
$item['object-type'] = Activity\ObjectType::COMMENT;
} else {
- self::checkThrottleLimit();
+ $this->checkThrottleLimit();
$item['gravity'] = Item::GRAVITY_PARENT;
$item['object-type'] = Activity\ObjectType::NOTE;
$item['gravity'] = Item::GRAVITY_COMMENT;
$item['object-type'] = Activity\ObjectType::COMMENT;
} else {
- self::checkThrottleLimit();
+ $this->checkThrottleLimit();
$item['gravity'] = Item::GRAVITY_PARENT;
$item['object-type'] = Activity\ObjectType::NOTE;
}
}
- public static function checkThrottleLimit()
+ public function checkThrottleLimit()
{
$uid = self::getCurrentUserID();
$posts_day = Post::countThread($condition);
if ($posts_day > $throttle_day) {
- Logger::notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
- $error = DI::l10n()->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);
+ $this->logger->notice('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
+ $error = $this->t('Too Many Requests');
+ $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);
- System::jsonError(429, $errorobj->toArray());
+ $this->jsonError(429, $errorobj->toArray());
}
}
if ($posts_week > $throttle_week) {
Logger::notice('Weekly posting limit reached', ['uid' => $uid, 'posts' => $posts_week, 'limit' => $throttle_week]);
- $error = DI::l10n()->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 = $this->t('Too Many Requests');
+ $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);
- System::jsonError(429, $errorobj->toArray());
+ $this->jsonError(429, $errorobj->toArray());
}
}
if ($posts_month > $throttle_month) {
Logger::notice('Monthly posting limit reached', ['uid' => $uid, 'posts' => $posts_month, 'limit' => $throttle_month]);
- $error = DI::l10n()->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 = $this->t('Too Many Requests');
+ $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);
- System::jsonError(429, $errorobj->toArray());
+ $this->jsonError(429, $errorobj->toArray());
}
}
}
$this->jsonExit(['status' => 'OK', 'message' => $message]);
} catch (\Exception $e) {
DI::sysmsg()->addNotice($e->getMessage());
- System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
+ $this->jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
}
}
header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
$this->jsonExit($data, 'application/activity+json');
} catch (HTTPException\NotFoundException $e) {
- System::jsonError(404, ['error' => 'Record not found']);
+ $this->jsonError(404, ['error' => 'Record not found']);
}
}
// view infos about a known profile (needs a login)
$which = $a->getLoggedInUserNickname();
} else {
- System::jsonError(403, 'Authentication required');
+ $this->jsonError(403, 'Authentication required');
}
$owner = User::getOwnerDataByNick($which);
if (empty($owner['uid'])) {
- System::jsonError(404, 'Profile not found');
+ $this->jsonError(404, 'Profile not found');
}
$json_info = [
header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
$this->jsonExit($data, 'application/activity+json');
} catch (HTTPException\NotFoundException $e) {
- System::jsonError(404, ['error' => 'Record not found']);
+ $this->jsonError(404, ['error' => 'Record not found']);
}
}
// Known deleted user
$data = ActivityPub\Transmitter::getDeletedUser($this->parameters['nickname']);
- System::jsonError(410, $data);
+ $this->jsonError(410, $data);
} else {
// Any other case (unknown, blocked, nverified, expired, no profile, no self contact)
- System::jsonError(404, []);
+ $this->jsonError(404, []);
}
}
}