Files
grocy/controllers/ExceptionController.php
T

85 lines
2.2 KiB
PHP
Raw Normal View History

2020-08-29 12:05:32 +02:00
<?php
namespace Grocy\Controllers;
2026-04-20 22:46:47 +02:00
use Grocy\Controllers\Api\BaseApiController;
use Grocy\Services\ApplicationService;
2023-07-29 14:02:56 +02:00
use DI\Container;
2026-04-20 22:46:47 +02:00
use Psr\Http\Message\ResponseFactoryInterface;
2020-08-29 12:05:32 +02:00
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Slim\Exception\HttpException;
use Slim\Exception\HttpForbiddenException;
use Slim\Exception\HttpNotFoundException;
use Throwable;
class ExceptionController extends BaseApiController
{
2026-04-20 22:46:47 +02:00
public function __construct(Container $container, ResponseFactoryInterface $responseFactory)
2020-08-29 16:41:27 +02:00
{
parent::__construct($container);
2026-04-20 22:46:47 +02:00
$this->ResponseFactory = $responseFactory;
2020-08-29 16:41:27 +02:00
}
2020-08-29 12:05:32 +02:00
2026-04-20 22:46:47 +02:00
private $ResponseFactory;
2021-07-16 17:32:08 +02:00
public function __invoke(ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails, bool $logErrors, bool $logErrorDetails, ?LoggerInterface $logger = null)
2020-08-29 16:41:27 +02:00
{
2026-04-20 22:46:47 +02:00
$response = $this->ResponseFactory->createResponse();
2020-08-29 16:41:27 +02:00
$isApiRoute = string_starts_with($request->getUri()->getPath(), '/api/');
2020-08-31 20:40:31 +02:00
2020-12-20 16:52:13 +01:00
if (!defined('GROCY_AUTHENTICATED'))
{
define('GROCY_AUTHENTICATED', false);
}
if ($isApiRoute)
{
2020-08-29 16:41:27 +02:00
$status = 500;
if ($exception instanceof HttpException)
{
2020-08-29 16:41:27 +02:00
$status = $exception->getCode();
}
2020-08-29 16:41:27 +02:00
$data = [
2020-08-31 20:40:31 +02:00
'error_message' => $exception->getMessage()
2020-08-29 16:41:27 +02:00
];
if ($displayErrorDetails)
{
2020-08-29 16:41:27 +02:00
$data['error_details'] = [
'stack_trace' => $exception->getTraceAsString(),
'file' => $exception->getFile(),
2020-08-31 20:40:31 +02:00
'line' => $exception->getLine()
2020-08-29 16:41:27 +02:00
];
}
return $this->ApiResponse($response->withStatus($status)->withHeader('Content-Type', 'application/json'), $data);
2020-08-29 16:41:27 +02:00
}
if ($exception instanceof HttpNotFoundException)
{
2021-07-16 17:32:08 +02:00
if (!defined('GROCY_AUTHENTICATED'))
{
define('GROCY_AUTHENTICATED', false);
}
2026-04-20 22:46:47 +02:00
return $this->RenderPage($response->withStatus(404), 'errors/404', [
2020-08-29 16:41:27 +02:00
'exception' => $exception
]);
}
if ($exception instanceof HttpForbiddenException)
{
2026-04-20 22:46:47 +02:00
return $this->RenderPage($response->withStatus(403), 'errors/403', [
2020-08-29 16:41:27 +02:00
'exception' => $exception
]);
}
2020-08-29 12:05:32 +02:00
2026-04-20 22:46:47 +02:00
return $this->RenderPage($response->withStatus(500), 'errors/500', [
'exception' => $exception,
2026-04-20 22:46:47 +02:00
'systemInfo' => ApplicationService::GetInstance()->GetSystemInfo()
2020-08-29 16:41:27 +02:00
]);
}
2020-08-29 12:05:32 +02:00
}