Skip to content

Commit 14e2f67

Browse files
Remove full DSNs from exception messages
1 parent b421063 commit 14e2f67

39 files changed

+98
-82
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static function createConnection(string $dsn, array $options = [])
140140
return CouchbaseCollectionAdapter::createConnection($dsn, $options);
141141
}
142142

143-
throw new InvalidArgumentException(sprintf('Unsupported DSN: "%s".', $dsn));
143+
throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:" nor "couchbase:".');
144144
}
145145

146146
/**

src/Symfony/Component/Cache/Adapter/CouchbaseBucketAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function createConnection($servers, array $options = []): \Couchba
8383

8484
foreach ($servers as $dsn) {
8585
if (0 !== strpos($dsn, 'couchbase:')) {
86-
throw new InvalidArgumentException(sprintf('Invalid Couchbase DSN: "%s" does not start with "couchbase:".', $dsn));
86+
throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".');
8787
}
8888

8989
preg_match($dsnPattern, $dsn, $matches);

src/Symfony/Component/Cache/Adapter/CouchbaseCollectionAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function createConnection($dsn, array $options = [])
7979

8080
foreach ($dsn as $server) {
8181
if (0 !== strpos($server, 'couchbase:')) {
82-
throw new InvalidArgumentException(sprintf('Invalid Couchbase DSN: "%s" does not start with "couchbase:".', $server));
82+
throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".');
8383
}
8484

8585
preg_match($dsnPattern, $server, $matches);

src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct($connOrDsn, string $namespace = '', int $defaultLife
7070
$this->conn = $connOrDsn;
7171
} elseif (\is_string($connOrDsn)) {
7272
if (!class_exists(DriverManager::class)) {
73-
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrDsn));
73+
throw new InvalidArgumentException('Failed to parse DSN. Try running "composer require doctrine/dbal".');
7474
}
7575
if (class_exists(DsnParser::class)) {
7676
$params = (new DsnParser([

src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static function createConnection($servers, array $options = [])
109109
continue;
110110
}
111111
if (!str_starts_with($dsn, 'memcached:')) {
112-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s" does not start with "memcached:".', $dsn));
112+
throw new InvalidArgumentException('Invalid Memcached DSN: it does not start with "memcached:".');
113113
}
114114
$params = preg_replace_callback('#^memcached:(//)?(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) {
115115
if (!empty($m[2])) {
@@ -119,15 +119,15 @@ public static function createConnection($servers, array $options = [])
119119
return 'file:'.($m[1] ?? '');
120120
}, $dsn);
121121
if (false === $params = parse_url($params)) {
122-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
122+
throw new InvalidArgumentException('Invalid Memcached DSN.');
123123
}
124124
$query = $hosts = [];
125125
if (isset($params['query'])) {
126126
parse_str($params['query'], $query);
127127

128128
if (isset($query['host'])) {
129129
if (!\is_array($hosts = $query['host'])) {
130-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
130+
throw new InvalidArgumentException('Invalid Memcached DSN: query parameter "host" must be an array.');
131131
}
132132
foreach ($hosts as $host => $weight) {
133133
if (false === $port = strrpos($host, ':')) {
@@ -146,7 +146,7 @@ public static function createConnection($servers, array $options = [])
146146
}
147147
}
148148
if (!isset($params['host']) && !isset($params['path'])) {
149-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
149+
throw new InvalidArgumentException('Invalid Memcached DSN: missing host or path.');
150150
}
151151
if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
152152
$params['weight'] = $m[1];

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public static function createConnection(string $dsn, array $options = [])
9696
} elseif (str_starts_with($dsn, 'rediss:')) {
9797
$scheme = 'rediss';
9898
} else {
99-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s" does not start with "redis:" or "rediss".', $dsn));
99+
throw new InvalidArgumentException('Invalid Redis DSN: it does not start with "redis[s]:".');
100100
}
101101

102102
if (!\extension_loaded('redis') && !class_exists(\Predis\Client::class)) {
103-
throw new CacheException(sprintf('Cannot find the "redis" extension nor the "predis/predis" package: "%s".', $dsn));
103+
throw new CacheException('Cannot find the "redis" extension nor the "predis/predis" package.');
104104
}
105105

106106
$params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:[^:@]*+:)?([^@]*+)@)?#', function ($m) use (&$auth) {
@@ -116,7 +116,7 @@ public static function createConnection(string $dsn, array $options = [])
116116
}, $dsn);
117117

118118
if (false === $params = parse_url($params)) {
119-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
119+
throw new InvalidArgumentException('Invalid Redis DSN.');
120120
}
121121

122122
$query = $hosts = [];
@@ -129,7 +129,7 @@ public static function createConnection(string $dsn, array $options = [])
129129

130130
if (isset($query['host'])) {
131131
if (!\is_array($hosts = $query['host'])) {
132-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
132+
throw new InvalidArgumentException('Invalid Redis DSN: query parameter "host" must be an array.');
133133
}
134134
foreach ($hosts as $host => $parameters) {
135135
if (\is_string($parameters)) {
@@ -153,7 +153,7 @@ public static function createConnection(string $dsn, array $options = [])
153153
$params['dbindex'] = $m[1] ?? '0';
154154
$params['path'] = substr($params['path'], 0, -\strlen($m[0]));
155155
} elseif (isset($params['host'])) {
156-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s", the "dbindex" parameter must be a number.', $dsn));
156+
throw new InvalidArgumentException('Invalid Redis DSN: query parameter "dbindex" must be a number.');
157157
}
158158
}
159159

@@ -165,13 +165,13 @@ public static function createConnection(string $dsn, array $options = [])
165165
}
166166

167167
if (!$hosts) {
168-
throw new InvalidArgumentException(sprintf('Invalid Redis DSN: "%s".', $dsn));
168+
throw new InvalidArgumentException('Invalid Redis DSN: missing host.');
169169
}
170170

171171
$params += $query + $options + self::$defaultConnectionOptions;
172172

173173
if (isset($params['redis_sentinel']) && !class_exists(\Predis\Client::class) && !class_exists(\RedisSentinel::class)) {
174-
throw new CacheException(sprintf('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher: "%s".', $dsn));
174+
throw new CacheException('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher.');
175175
}
176176

177177
if (isset($params['lazy'])) {
@@ -180,7 +180,7 @@ public static function createConnection(string $dsn, array $options = [])
180180
$params['redis_cluster'] = filter_var($params['redis_cluster'], \FILTER_VALIDATE_BOOLEAN);
181181

182182
if ($params['redis_cluster'] && isset($params['redis_sentinel'])) {
183-
throw new InvalidArgumentException(sprintf('Cannot use both "redis_cluster" and "redis_sentinel" at the same time: "%s".', $dsn));
183+
throw new InvalidArgumentException('Cannot use both "redis_cluster" and "redis_sentinel" at the same time.');
184184
}
185185

186186
if (null === $params['class'] && \extension_loaded('redis')) {
@@ -189,15 +189,15 @@ public static function createConnection(string $dsn, array $options = [])
189189
$class = $params['class'] ?? \Predis\Client::class;
190190

191191
if (isset($params['redis_sentinel']) && !is_a($class, \Predis\Client::class, true) && !class_exists(\RedisSentinel::class)) {
192-
throw new CacheException(sprintf('Cannot use Redis Sentinel: class "%s" does not extend "Predis\Client" and ext-redis >= 5.2 not found: "%s".', $class, $dsn));
192+
throw new CacheException(sprintf('Cannot use Redis Sentinel: class "%s" does not extend "Predis\Client" and ext-redis >= 5.2 not found.', $class));
193193
}
194194
}
195195

196196
if (is_a($class, \Redis::class, true)) {
197197
$connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
198198
$redis = new $class();
199199

200-
$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts, $tls) {
200+
$initializer = static function ($redis) use ($connect, $params, $auth, $hosts, $tls) {
201201
$hostIndex = 0;
202202
do {
203203
$host = $hosts[$hostIndex]['host'] ?? $hosts[$hostIndex]['path'];
@@ -243,7 +243,7 @@ public static function createConnection(string $dsn, array $options = [])
243243
} while (++$hostIndex < \count($hosts) && !$address);
244244

245245
if (isset($params['redis_sentinel']) && !$address) {
246-
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from sentinel "%s" and dsn "%s".', $params['redis_sentinel'], $dsn));
246+
throw new InvalidArgumentException(sprintf('Failed to retrieve master information from sentinel "%s".', $params['redis_sentinel']));
247247
}
248248

249249
try {
@@ -262,22 +262,22 @@ public static function createConnection(string $dsn, array $options = [])
262262
restore_error_handler();
263263
}
264264
if (!$isConnected) {
265-
$error = preg_match('/^Redis::p?connect\(\): (.*)/', $error ?? '', $error) ? sprintf(' (%s)', $error[1]) : '';
266-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$error.'.');
265+
$error = preg_match('/^Redis::p?connect\(\): (.*)/', $error ?? $redis->getLastError() ?? '', $error) ? sprintf(' (%s)', $error[1]) : '';
266+
throw new InvalidArgumentException('Redis connection failed: '.$error.'.');
267267
}
268268

269269
if ((null !== $auth && !$redis->auth($auth))
270270
|| ($params['dbindex'] && !$redis->select($params['dbindex']))
271271
) {
272272
$e = preg_replace('/^ERR /', '', $redis->getLastError());
273-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e.'.');
273+
throw new InvalidArgumentException('Redis connection failed: '.$e.'.');
274274
}
275275

276276
if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
277277
$redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
278278
}
279279
} catch (\RedisException $e) {
280-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
280+
throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage());
281281
}
282282

283283
return true;
@@ -302,14 +302,14 @@ public static function createConnection(string $dsn, array $options = [])
302302
try {
303303
$redis = new $class($hosts, $params);
304304
} catch (\RedisClusterException $e) {
305-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
305+
throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage());
306306
}
307307

308308
if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {
309309
$redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
310310
}
311311
} elseif (is_a($class, \RedisCluster::class, true)) {
312-
$initializer = static function () use ($class, $params, $dsn, $hosts) {
312+
$initializer = static function () use ($class, $params, $hosts) {
313313
foreach ($hosts as $i => $host) {
314314
switch ($host['scheme']) {
315315
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
@@ -321,7 +321,7 @@ public static function createConnection(string $dsn, array $options = [])
321321
try {
322322
$redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent'], $params['auth'] ?? '', ...\defined('Redis::SCAN_PREFIX') ? [$params['ssl'] ?? null] : []);
323323
} catch (\RedisClusterException $e) {
324-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
324+
throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage());
325325
}
326326

327327
if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) {

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/SessionHandlerFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function createHandler($connection): AbstractSessionHandler
6363
case str_starts_with($connection, 'rediss:'):
6464
case str_starts_with($connection, 'memcached:'):
6565
if (!class_exists(AbstractAdapter::class)) {
66-
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
66+
throw new \InvalidArgumentException('Unsupported Redis or Memcached DSN. Try running "composer require symfony/cache".');
6767
}
6868
$handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
6969
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
@@ -72,7 +72,7 @@ public static function createHandler($connection): AbstractSessionHandler
7272

7373
case str_starts_with($connection, 'pdo_oci://'):
7474
if (!class_exists(DriverManager::class)) {
75-
throw new \InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require doctrine/dbal".', $connection));
75+
throw new \InvalidArgumentException('Unsupported PDO OCI DSN. Try running "composer require doctrine/dbal".');
7676
}
7777
$connection[3] = '-';
7878
$params = class_exists(DsnParser::class) ? (new DsnParser())->parse($connection) : ['url' => $connection];

src/Symfony/Component/Lock/Store/DoctrineDbalPostgreSqlStore.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct($connOrUrl)
5252
$this->conn = $connOrUrl;
5353
} elseif (\is_string($connOrUrl)) {
5454
if (!class_exists(DriverManager::class)) {
55-
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
55+
throw new InvalidArgumentException('Failed to parse DSN. Try running "composer require doctrine/dbal".');
5656
}
5757
if (class_exists(DsnParser::class)) {
5858
$params = (new DsnParser([
@@ -274,7 +274,7 @@ private function unlockShared(Key $key): void
274274
private function filterDsn(string $dsn): string
275275
{
276276
if (!str_contains($dsn, '://')) {
277-
throw new InvalidArgumentException(sprintf('String "%s" is not a valid DSN for Doctrine DBAL.', $dsn));
277+
throw new InvalidArgumentException('DSN is invalid for Doctrine DBAL.');
278278
}
279279

280280
[$scheme, $rest] = explode(':', $dsn, 2);

src/Symfony/Component/Lock/Store/DoctrineDbalStore.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct($connOrUrl, array $options = [], float $gcProbabilit
6969
$this->conn = $connOrUrl;
7070
} elseif (\is_string($connOrUrl)) {
7171
if (!class_exists(DriverManager::class)) {
72-
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $connOrUrl));
72+
throw new InvalidArgumentException('Failed to parse the DSN. Try running "composer require doctrine/dbal".');
7373
}
7474
if (class_exists(DsnParser::class)) {
7575
$params = (new DsnParser([

src/Symfony/Component/Lock/Store/StoreFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function createStore($connection)
7575
case str_starts_with($connection, 'rediss:'):
7676
case str_starts_with($connection, 'memcached:'):
7777
if (!class_exists(AbstractAdapter::class)) {
78-
throw new InvalidArgumentException(sprintf('Unsupported DSN "%s". Try running "composer require symfony/cache".', $connection));
78+
throw new InvalidArgumentException('Unsupported Redis or Memcached DSN. Try running "composer require symfony/cache".');
7979
}
8080
$storeClass = str_starts_with($connection, 'memcached:') ? MemcachedStore::class : RedisStore::class;
8181
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);

src/Symfony/Component/Lock/Store/ZookeeperStore.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public function __construct(\Zookeeper $zookeeper)
3737
public static function createConnection(string $dsn): \Zookeeper
3838
{
3939
if (!str_starts_with($dsn, 'zookeeper:')) {
40-
throw new InvalidArgumentException(sprintf('Unsupported DSN: "%s".', $dsn));
40+
throw new InvalidArgumentException('Unsupported DSN for Zookeeper.');
4141
}
4242

4343
if (false === $params = parse_url($dsn)) {
44-
throw new InvalidArgumentException(sprintf('Invalid Zookeeper DSN: "%s".', $dsn));
44+
throw new InvalidArgumentException('Invalid Zookeeper DSN.');
4545
}
4646

4747
$host = $params['host'] ?? '';

src/Symfony/Component/Mailer/Tests/Transport/DsnTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ public static function invalidDsnProvider(): iterable
9292
{
9393
yield [
9494
'some://',
95-
'The "some://" mailer DSN is invalid.',
95+
'The mailer DSN is invalid.',
9696
];
9797

9898
yield [
9999
'//sendmail',
100-
'The "//sendmail" mailer DSN must contain a scheme.',
100+
'The mailer DSN must contain a scheme.',
101101
];
102102

103103
yield [
104104
'file:///some/path',
105-
'The "file:///some/path" mailer DSN must contain a host (use "default" by default).',
105+
'The mailer DSN must contain a host (use "default" by default).',
106106
];
107107
}
108108
}

src/Symfony/Component/Mailer/Tests/TransportTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public function testFromWrongString(string $dsn, string $error)
9090

9191
public static function fromWrongStringProvider(): iterable
9292
{
93-
yield 'garbage at the end' => ['dummy://a some garbage here', 'The DSN has some garbage at the end: " some garbage here".'];
93+
yield 'garbage at the end' => ['dummy://a some garbage here', 'The mailer DSN has some garbage at the end.'];
9494

95-
yield 'not a valid DSN' => ['something not a dsn', 'The "something" mailer DSN must contain a scheme.'];
95+
yield 'not a valid DSN' => ['something not a dsn', 'The mailer DSN must contain a scheme.'];
9696

97-
yield 'failover not closed' => ['failover(dummy://a', 'The "(dummy://a" mailer DSN must contain a scheme.'];
97+
yield 'failover not closed' => ['failover(dummy://a', 'The mailer DSN must contain a scheme.'];
9898

9999
yield 'not a valid keyword' => ['foobar(dummy://a)', 'The "foobar" keyword is not valid (valid ones are "failover", "roundrobin")'];
100100
}

0 commit comments

Comments
 (0)