3 * @copyright Copyright (C) 2010-2022, 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()
55 $error = $this->l10n->t('Record not found');
56 $error_description = '';
57 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
59 $this->logError(404, $error);
60 System::jsonError(404, $errorObj->toArray());
63 public function UnprocessableEntity(string $error = '')
65 $error = $error ?: $this->l10n->t('Unprocessable Entity');
66 $error_description = '';
67 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
69 $this->logError(422, $error);
70 System::jsonError(422, $errorObj->toArray());
73 public function Unauthorized(string $error = '')
75 $error = $error ?: $this->l10n->t('Unauthorized');
76 $error_description = '';
77 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
79 $this->logError(401, $error);
80 System::jsonError(401, $errorObj->toArray());
83 public function Forbidden(string $error = '')
85 $error = $error ?: $this->l10n->t('Token is not authorized with a valid user or is missing a required scope');
86 $error_description = '';
87 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
89 $this->logError(403, $error);
90 System::jsonError(403, $errorObj->toArray());
93 public function InternalError(string $error = '')
95 $error = $error ?: $this->l10n->t('Internal Server Error');
96 $error_description = '';
97 $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
99 $this->logError(500, $error);
100 System::jsonError(500, $errorObj->toArray());