<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of Contao Manager.
 *
 * (c) Contao Association
 *
 * @license LGPL-3.0-or-later
 */

namespace Contao\ManagerApi\Controller\Server;

use Contao\ManagerApi\HttpKernel\ApiProblemResponse;
use Contao\ManagerApi\System\SelfUpdate;
use Crell\ApiProblem\ApiProblem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/server/self-update", methods={"GET"})
 */
class SelfUpdateController
{
    /**
     * Gets response about update status of the Contao Manager.
     */
    public function __invoke(SelfUpdate $updater): Response
    {
        if (!$updater->canUpdate()) {
            return new ApiProblemResponse(
                (new ApiProblem('This version cannot be updated.'))
                    ->setStatus(Response::HTTP_NOT_IMPLEMENTED)
            );
        }

        $error = null;

        try {
            $latestVersion = $updater->getNewVersion();
        } catch (\Throwable $e) {
            $error = $e->getMessage();
            $latestVersion = $updater->getOldVersion();
        }

        return new JsonResponse(
            [
                'current_version' => $updater->getOldVersion(),
                'latest_version' => $latestVersion,
                'channel' => $updater->getChannel(),
                'supported' => $updater->supportsUpdate(),
                'error' => $error,
            ]
        );
    }
}
<commit_msg>Correctly handle request errors to update server<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of Contao Manager.
 *
 * (c) Contao Association
 *
 * @license LGPL-3.0-or-later
 */

namespace Contao\ManagerApi\Controller\Server;

use Contao\ManagerApi\HttpKernel\ApiProblemResponse;
use Contao\ManagerApi\System\SelfUpdate;
use Crell\ApiProblem\ApiProblem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/server/self-update", methods={"GET"})
 */
class SelfUpdateController
{
    /**
     * Gets response about update status of the Contao Manager.
     */
    public function __invoke(SelfUpdate $updater): Response
    {
        if (!$updater->canUpdate()) {
            return new ApiProblemResponse(
                (new ApiProblem('This version cannot be updated.'))
                    ->setStatus(Response::HTTP_NOT_IMPLEMENTED)
            );
        }

        try {
            $error = null;
            $supportsUpdate = $updater->supportsUpdate();
            $latestVersion = $updater->getNewVersion();
        } catch (\Throwable $e) {
            $error = $e->getMessage();
            $supportsUpdate = true;
            $latestVersion = $updater->getOldVersion();
        }

        return new JsonResponse(
            [
                'current_version' => $updater->getOldVersion(),
                'latest_version' => $latestVersion,
                'channel' => $updater->getChannel(),
                'supported' => $supportsUpdate,
                'error' => $error,
            ]
        );
    }
}

{'fraction_non_alphanumeric': 0.12519512956603185, 'fraction_numerical': 0.0018732438339057135, 'mean_word_length': 2.2627291242362526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6193449', 'n_tokens_mistral': 907, 'n_tokens_neox': 900, 'n_words': 178}