3 * @copyright Copyright (C) 2010-2021, 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\BaseFactory;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
29 class Error extends BaseFactory
31 private function logError(int $errorno, string $error)
33 Logger::info('API Error', ['no' => $errorno, 'error' => $error, 'method' => $_SERVER['REQUEST_METHOD'] ?? '', 'command' => DI::args()->getQueryString(), 'user-agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
36 public function RecordNotFound()
38 $error = DI::l10n()->t('Record not found');
39 $error_description = '';
40 $errorobj = New \Friendica\Object\Api\Mastodon\Error($error, $error_description);
42 $this->logError(404, $error);
43 System::jsonError(404, $errorobj->toArray());
46 public function UnprocessableEntity(string $error = '')
48 $error = $error ?: DI::l10n()->t('Unprocessable Entity');
49 $error_description = '';
50 $errorobj = New \Friendica\Object\Api\Mastodon\Error($error, $error_description);
52 $this->logError(422, $error);
53 System::jsonError(422, $errorobj->toArray());
56 public function Unauthorized(string $error = '')
58 $error = $error ?: DI::l10n()->t('Unauthorized');
59 $error_description = '';
60 $errorobj = New \Friendica\Object\Api\Mastodon\Error($error, $error_description);
62 $this->logError(401, $error);
63 System::jsonError(401, $errorobj->toArray());
66 public function Forbidden(string $error = '')
68 $error = $error ?: DI::l10n()->t('Token is not authorized with a valid user or is missing a required scope');
69 $error_description = '';
70 $errorobj = New \Friendica\Object\Api\Mastodon\Error($error, $error_description);
72 $this->logError(403, $error);
73 System::jsonError(403, $errorobj->toArray());
76 public function InternalError(string $error = '')
78 $error = $error ?: DI::l10n()->t('Internal Server Error');
79 $error_description = '';
80 $errorobj = New \Friendica\Object\Api\Mastodon\Error($error, $error_description);
82 $this->logError(500, $error);
83 System::jsonError(500, $errorobj->toArray());