]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Error.php
Move jsonError out of Factory\Api\Mastodon\Error->RecordNotFound
[friendica.git] / src / Factory / Api / Mastodon / Error.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Factory\Api\Mastodon;
23
24 use Friendica\App\Arguments;
25 use Friendica\BaseFactory;
26 use Friendica\Core\L10n;
27 use Friendica\Core\System;
28 use Psr\Log\LoggerInterface;
29
30 /** @todo A Factory shouldn't return something to the frontpage, it's for creating content, not showing it */
31 class Error extends BaseFactory
32 {
33         /** @var Arguments */
34         private $args;
35         /** @var string[] The $_SERVER array */
36         private $server;
37         /** @var L10n */
38         private $l10n;
39
40         public function __construct(LoggerInterface $logger, Arguments $args, L10n $l10n, array $server)
41         {
42                 parent::__construct($logger);
43                 $this->args   = $args;
44                 $this->server = $server;
45                 $this->l10n   = $l10n;
46         }
47
48         private function logError(int $errorno, string $error)
49         {
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'] ?? '']);
51         }
52
53         public function RecordNotFound(): \Friendica\Object\Api\Mastodon\Error
54         {
55                 $error             = $this->l10n->t('Record not found');
56                 $error_description = '';
57                 return new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
58         }
59
60         public function UnprocessableEntity(string $error = '')
61         {
62                 $error             = $error ?: $this->l10n->t('Unprocessable Entity');
63                 $error_description = '';
64                 $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
65
66                 $this->logError(422, $error);
67                 $this->jsonError(422, $errorObj->toArray());
68         }
69
70         public function Unauthorized(string $error = '', string $error_description = '')
71         {
72                 $error             = $error ?: $this->l10n->t('Unauthorized');
73                 $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
74
75                 $this->logError(401, $error);
76                 $this->jsonError(401, $errorObj->toArray());
77         }
78
79         public function Forbidden(string $error = '')
80         {
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);
84
85                 $this->logError(403, $error);
86                 $this->jsonError(403, $errorObj->toArray());
87         }
88
89         public function InternalError(string $error = '')
90         {
91                 $error             = $error ?: $this->l10n->t('Internal Server Error');
92                 $error_description = '';
93                 $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
94
95                 $this->logError(500, $error);
96                 $this->jsonError(500, $errorObj->toArray());
97         }
98 }