3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Factory\Api\Mastodon;
24 use Friendica\App\Arguments;
25 use Friendica\BaseFactory;
26 use Friendica\Core\L10n;
27 use Friendica\Core\System;
28 use Psr\Log\LoggerInterface;
30 /** @todo A Factory shouldn't return something to the frontpage, it's for creating content, not showing it */
31 class Error extends BaseFactory
35 /** @var string[] The $_SERVER array */
40 public function __construct(LoggerInterface $logger, Arguments $args, L10n $l10n, array $server)
42 parent::__construct($logger);
44 $this->server = $server;
48 private function logError(int $errorno, string $error)
50 $this->logger->info('API Error', ['no' => $errorno, 'error' => $error, 'method' => $this->args->getMethod(), 'command' => $this->args->getQueryString(), 'user-agent' => $this->server['HTTP_USER_AGENT'] ?? '']);
53 public function RecordNotFound(): \Friendica\Object\Api\Mastodon\Error
55 $error = $this->l10n->t('Record not found');
56 $error_description = '';
57 return new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
60 public function UnprocessableEntity(string $error = '')
62 $error = $error ?: $this->l10n->t('Unprocessable Entity');
63 $error_description = '';
64 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
66 $this->logError(422, $error);
67 $this->jsonError(422, $errorObj->toArray());
70 public function Unauthorized(string $error = '', string $error_description = '')
72 $error = $error ?: $this->l10n->t('Unauthorized');
73 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
75 $this->logError(401, $error);
76 $this->jsonError(401, $errorObj->toArray());
79 public function Forbidden(string $error = '')
81 $error = $error ?: $this->l10n->t('Token is not authorized with a valid user or is missing a required scope');
82 $error_description = '';
83 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
85 $this->logError(403, $error);
86 $this->jsonError(403, $errorObj->toArray());
89 public function InternalError(string $error = '')
91 $error = $error ?: $this->l10n->t('Internal Server Error');
92 $error_description = '';
93 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
95 $this->logError(500, $error);
96 $this->jsonError(500, $errorObj->toArray());