Skip to content

Commit b599c73

Browse files
authored
Catch the right Redis exception type in graceful-degradation blocks (#1830)
1 parent 3ab4fc1 commit b599c73

8 files changed

Lines changed: 19 additions & 19 deletions

File tree

src/Controller/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function getPackagesMetadata(FavoriteManager $favMgr, DownloadManager
6767
if ($search) {
6868
$favorites = $favMgr->getFaverCounts($ids);
6969
}
70-
} catch (\Predis\Connection\ConnectionException $e) {
70+
} catch (\Predis\PredisException $e) {
7171
}
7272

7373
return ['downloads' => $downloads, 'favers' => $favorites];

src/Controller/ExploreController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
use App\Entity\Version;
1717
use App\Model\DownloadManager;
1818
use App\Model\FavoriteManager;
19-
use Doctrine\DBAL\ConnectionException;
2019
use Pagerfanta\Adapter\FixedAdapter;
2120
use Pagerfanta\Pagerfanta;
2221
use Predis\Client as RedisClient;
22+
use Predis\PredisException;
2323
use Symfony\Component\HttpFoundation\JsonResponse;
2424
use Symfony\Component\HttpFoundation\Request;
2525
use Symfony\Component\HttpFoundation\Response;
@@ -53,7 +53,7 @@ public function exploreAction(RedisClient $redis): Response
5353
return array_search($a->getId(), $popularIds) > array_search($b->getId(), $popularIds) ? 1 : -1;
5454
});
5555
}
56-
} catch (ConnectionException $e) {
56+
} catch (PredisException $e) {
5757
$popular = [];
5858
}
5959

@@ -96,7 +96,7 @@ public function popularAction(Request $req, RedisClient $redis, FavoriteManager
9696
$packages->setNormalizeOutOfRangePages(true);
9797
$packages->setMaxPerPage($perPage);
9898
$packages->setCurrentPage(max(1, $req->query->getInt('page', 1)));
99-
} catch (ConnectionException $e) {
99+
} catch (PredisException $e) {
100100
$packages = new Pagerfanta(new FixedAdapter(0, []));
101101
}
102102

src/Controller/PackageController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
use Pagerfanta\Doctrine\ORM\QueryAdapter;
6262
use Pagerfanta\Pagerfanta;
6363
use Predis\Client as RedisClient;
64-
use Predis\Connection\ConnectionException;
64+
use Predis\PredisException;
6565
use Psr\Log\LoggerInterface;
6666
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
6767
use Symfony\Component\Form\Extension\Core\Type\TextType;
@@ -418,7 +418,7 @@ public function viewProvidersAction(Request $req, string $name, RedisClient $red
418418

419419
return $trendiness[$a->getId()] > $trendiness[$b->getId()] ? -1 : 1;
420420
});
421-
} catch (ConnectionException $e) {
421+
} catch (PredisException $e) {
422422
}
423423

424424
if ($req->getRequestFormat() === 'json') {
@@ -581,7 +581,7 @@ public function viewPackageAction(Request $req, string $name, CsrfTokenManagerIn
581581
}
582582
$data['downloads'] = $this->downloadManager->getDownloads($package);
583583
$data['favers'] = $this->favoriteManager->getFaverCount($package);
584-
} catch (\RuntimeException|ConnectionException $e) {
584+
} catch (\RuntimeException|PredisException $e) {
585585
$data['downloads'] = null;
586586
$data['favers'] = null;
587587
}
@@ -681,7 +681,7 @@ public function viewPackageAction(Request $req, string $name, CsrfTokenManagerIn
681681
if ($user) {
682682
$data['is_favorite'] = $this->favoriteManager->isMarked($user, $package);
683683
}
684-
} catch (\RuntimeException|ConnectionException) {
684+
} catch (\RuntimeException|PredisException) {
685685
}
686686

687687
$data['dependents'] = Killswitch::isEnabled(Killswitch::PAGE_DETAILS_ENABLED) && Killswitch::isEnabled(Killswitch::LINKS_ENABLED) ? $repo->getDependentCount($package->getName()) : 0;
@@ -799,15 +799,15 @@ public function viewPackageDownloadsAction(Request $req, string $name): Response
799799
try {
800800
$data['downloads']['total'] = $this->downloadManager->getDownloads($package);
801801
$data['favers'] = $this->favoriteManager->getFaverCount($package);
802-
} catch (ConnectionException) {
802+
} catch (PredisException) {
803803
$data['downloads']['total'] = null;
804804
$data['favers'] = null;
805805
}
806806

807807
foreach ($versions as $version) {
808808
try {
809809
$data['downloads']['versions'][$version->getVersion()] = $this->downloadManager->getDownloads($package, $version);
810-
} catch (ConnectionException) {
810+
} catch (PredisException) {
811811
$data['downloads']['versions'][$version->getVersion()] = null;
812812
}
813813
}

src/Controller/WebController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use App\Service\BlogRssFetcher;
2222
use App\Util\Killswitch;
2323
use Predis\Client as RedisClient;
24-
use Predis\Connection\ConnectionException;
24+
use Predis\PredisException;
2525
use Psr\Cache\CacheItemInterface;
2626
use Symfony\Component\HttpFoundation\JsonResponse;
2727
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -181,7 +181,7 @@ public function statsAction(RedisClient $redis): Response
181181
'labels' => array_keys($dlChartMonthly),
182182
'values' => $redis->mget(array_values($dlChartMonthly)),
183183
];
184-
} catch (ConnectionException $e) {
184+
} catch (PredisException $e) {
185185
$downloads = 'N/A';
186186
$dlChart = $dlChartMonthly = null;
187187
}

src/Model/PackageManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function deletePackage(Package $package, ?string $reason = null, ?string
157157
// delete redis stats
158158
try {
159159
$this->redis->del('views:'.$packageId);
160-
} catch (\Predis\Connection\ConnectionException $e) {
160+
} catch (\Predis\PredisException $e) {
161161
}
162162

163163
// attempt search index cleanup

src/Security/RecaptchaHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use App\Entity\User;
1616
use Predis\Client;
17-
use Predis\Connection\ConnectionException;
17+
use Predis\PredisException;
1818
use Symfony\Component\HttpFoundation\RequestStack;
1919
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2020
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
@@ -52,7 +52,7 @@ public function requiresRecaptcha(RecaptchaContext $context): bool
5252

5353
try {
5454
$result = $this->redisCache->mget($keys);
55-
} catch (ConnectionException) {
55+
} catch (PredisException) {
5656
return false;
5757
}
5858
foreach ($result as $count) {

src/Security/Voter/PackageVoter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use App\Entity\Package;
1616
use App\Entity\User;
1717
use App\Model\DownloadManager;
18-
use Predis\Connection\ConnectionException;
18+
use Predis\PredisException;
1919
use Symfony\Bundle\SecurityBundle\Security;
2020
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2121
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
@@ -85,7 +85,7 @@ private function canDelete(Package $package, User $user): bool
8585

8686
try {
8787
$downloads = $this->downloadManager->getDownloads($package);
88-
} catch (ConnectionException $e) {
88+
} catch (PredisException $e) {
8989
return false;
9090
}
9191

src/Validator/PopularPackageSafetyValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use App\Model\DownloadManager;
1818
use Doctrine\DBAL\Connection;
1919
use Doctrine\Persistence\ManagerRegistry;
20-
use Predis\Connection\ConnectionException;
20+
use Predis\PredisException;
2121
use Symfony\Bundle\SecurityBundle\Security;
2222
use Symfony\Component\Validator\Constraint;
2323
use Symfony\Component\Validator\ConstraintValidator;
@@ -61,7 +61,7 @@ public function validate(mixed $value, Constraint $constraint): void
6161

6262
try {
6363
$downloads = $this->downloadManager->getTotalDownloads($value);
64-
} catch (ConnectionException $e) {
64+
} catch (PredisException $e) {
6565
$downloads = \PHP_INT_MAX;
6666
}
6767

0 commit comments

Comments
 (0)