]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Error.php
Apply feedback and describe the encoding method
[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()
54         {
55                 $error             = $this->l10n->t('Record not found');
56                 $error_description = '';
57                 $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
58
59                 $this->logError(404, $error);
60                 System::jsonError(404, $errorObj->toArray());
61         }
62
63         public function UnprocessableEntity(string $error = '')
64         {
65                 $error             = $error ?: $this->l10n->t('Unprocessable Entity');
66                 $error_description = '';
67                 $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
68
69                 $this->logError(422, $error);
70                 System::jsonError(422, $errorObj->toArray());
71         }
72
73         public function Unauthorized(string $error = '')
74         {
75                 $error             = $error ?: $this->l10n->t('Unauthorized');
76                 $error_description = '';
77                 $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
78
79                 $this->logError(401, $error);
80                 System::jsonError(401, $errorObj->toArray());
81         }
82
83         public function Forbidden(string $error = '')
84         {
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);
88
89                 $this->logError(403, $error);
90                 System::jsonError(403, $errorObj->toArray());
91         }
92
93         public function InternalError(string $error = '')
94         {
95                 $error             = $error ?: $this->l10n->t('Internal Server Error');
96                 $error_description = '';
97                 $errorObj          = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
98
99                 $this->logError(500, $error);
100                 System::jsonError(500, $errorObj->toArray());
101         }
102 }