friendica/src/Factory/Api/Mastodon/Error.php

90 lines
3.1 KiB
PHP
Raw Normal View History

2020-10-31 23:32:26 +01:00
<?php
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2020-10-31 23:32:26 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Mastodon;
2021-06-05 22:36:45 +02:00
use Friendica\App\Arguments;
2020-10-31 23:32:26 +01:00
use Friendica\BaseFactory;
2021-06-05 22:36:45 +02:00
use Friendica\Core\L10n;
2020-10-31 23:32:26 +01:00
use Friendica\Core\System;
2021-06-05 22:36:45 +02:00
use Psr\Log\LoggerInterface;
2020-10-31 23:32:26 +01:00
2021-06-05 22:36:45 +02:00
/** @todo A Factory shouldn't return something to the frontpage, it's for creating content, not showing it */
2020-10-31 23:32:26 +01:00
class Error extends BaseFactory
{
2021-06-05 22:36:45 +02:00
/** @var Arguments */
private $args;
/** @var string[] The $_SERVER array */
private $server;
/** @var L10n */
private $l10n;
2021-06-09 00:09:32 +02:00
2021-06-05 22:36:45 +02:00
public function __construct(LoggerInterface $logger, Arguments $args, L10n $l10n, array $server)
{
parent::__construct($logger);
2021-06-09 00:09:32 +02:00
$this->args = $args;
2021-06-05 22:36:45 +02:00
$this->server = $server;
2021-06-09 00:09:32 +02:00
$this->l10n = $l10n;
2021-06-05 22:36:45 +02:00
}
2021-05-19 11:03:41 +02:00
private function logError(int $errorno, string $error)
{
$this->logger->info('API Error', ['no' => $errorno, 'error' => $error, 'method' => $this->args->getMethod(), 'command' => $this->args->getQueryString(), 'user-agent' => $this->server['HTTP_USER_AGENT'] ?? '']);
2021-05-19 11:03:41 +02:00
}
public function RecordNotFound(): \Friendica\Object\Api\Mastodon\Error
2020-10-31 23:32:26 +01:00
{
2021-06-09 00:09:32 +02:00
$error = $this->l10n->t('Record not found');
2020-10-31 23:32:26 +01:00
$error_description = '';
return new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
2020-10-31 23:32:26 +01:00
}
2021-05-12 05:05:22 +02:00
public function UnprocessableEntity(string $error = ''): \Friendica\Object\Api\Mastodon\Error
2021-05-12 05:05:22 +02:00
{
2021-06-09 00:09:32 +02:00
$error = $error ?: $this->l10n->t('Unprocessable Entity');
2021-05-12 05:05:22 +02:00
$error_description = '';
return new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
2021-05-12 05:05:22 +02:00
}
public function Unauthorized(string $error = '', string $error_description = ''): \Friendica\Object\Api\Mastodon\Error
2021-05-12 05:05:22 +02:00
{
2021-06-09 00:09:32 +02:00
$error = $error ?: $this->l10n->t('Unauthorized');
return new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
2021-05-12 05:05:22 +02:00
}
public function Forbidden(string $error = ''): \Friendica\Object\Api\Mastodon\Error
2021-05-16 09:37:11 +02:00
{
2021-06-09 00:09:32 +02:00
$error = $error ?: $this->l10n->t('Token is not authorized with a valid user or is missing a required scope');
2021-05-16 09:37:11 +02:00
$error_description = '';
return new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
2021-05-16 09:37:11 +02:00
}
2021-05-12 05:05:22 +02:00
public function InternalError(string $error = '')
{
2021-06-09 00:09:32 +02:00
$error = $error ?: $this->l10n->t('Internal Server Error');
2021-05-12 05:05:22 +02:00
$error_description = '';
2021-06-09 00:09:32 +02:00
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
2021-05-12 05:05:22 +02:00
2021-05-19 11:03:41 +02:00
$this->logError(500, $error);
$this->jsonError(500, $errorObj->toArray());
2021-05-12 05:05:22 +02:00
}
2020-10-31 23:32:26 +01:00
}