Skip to content

Commit f66b853

Browse files
bug #39434 [Cache] Bugfix provide the correct host and port when throwing the exception (renan)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Cache] Bugfix provide the correct host and port when throwing the exception | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | none | License | MIT | Doc PR | none The message before was including the result of the `getMasterAddrByName` call of which ended with: ``` Failed to retrieve master information from master name "mymaster" and address ":0". ``` Commits ------- 276bbb5 [Cache] Bugfix provide the correct host and port when throwing the exception
2 parents 7c98ee1 + 276bbb5 commit f66b853

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterSentinelTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,13 @@ public function testInvalidDSNHasBothClusterAndSentinel()
4141
$dsn = 'redis:?host[redis1]&host[redis2]&host[redis3]&redis_cluster=1&redis_sentinel=mymaster';
4242
RedisAdapter::createConnection($dsn);
4343
}
44+
45+
public function testExceptionMessageWhenFailingToRetrieveMasterInformation()
46+
{
47+
$hosts = getenv('REDIS_SENTINEL_HOSTS');
48+
$firstHost = explode(' ', $hosts)[0];
49+
$this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
50+
$this->expectExceptionMessage('Failed to retrieve master information from master name "invalid-masterset-name" and address "'.$firstHost.'".');
51+
AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['redis_sentinel' => 'invalid-masterset-name']);
52+
}
4453
}

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,11 @@ public static function createConnection($dsn, array $options = [])
186186
if (isset($params['redis_sentinel'])) {
187187
$sentinel = new \RedisSentinel($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']);
188188

189-
if (![$host, $port] = $sentinel->getMasterAddrByName($params['redis_sentinel'])) {
189+
if (!$address = $sentinel->getMasterAddrByName($params['redis_sentinel'])) {
190190
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from master name "%s" and address "%s:%d".', $params['redis_sentinel'], $host, $port));
191191
}
192+
193+
[$host, $port] = $address;
192194
}
193195

194196
try {

0 commit comments

Comments
 (0)