8000 [Cache] fix catching auth errors · symfony/symfony@ee6b514 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee6b514

Browse files
[Cache] fix catching auth errors
1 parent e5543dc commit ee6b514

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,26 @@ public static function createConnection($dsn, array $options = [])
119119
$initializer = function ($redis) use ($connect, $params, $dsn, $auth) {
120120
try {
121121
@$redis->{$connect}($params['host'], $params['port'], $params['timeout'], $params['persistent_id'], $params['retry_interval']);
122+
123+
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
124+
$isConnected = $redis->isConnected();
125+
restore_error_handler();
126+
if (!$isConnected) {
127+
$error = preg_match('/^Redis::p?connect\(\): (.*)/', $error, $error) ? sprintf(' (%s)', $error[1]) : '';
128+
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$error.'.');
129+
}
130+
131+
if ((null !== $auth && !$redis->auth($auth))
132+
|| ($params['dbindex'] && !$redis->select($params['dbindex']))
133+
|| ($params['read_timeout'] && !$redis->setOption(\Redis::OPT_READ_TIMEOUT, $params['read_timeout']))
134+
) {
135+
$e = preg_replace('/^ERR /', '', $redis->getLastError());
136+
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e.'.');
137+
}
122138
} catch (\RedisException $e) {
123139
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
124140
}
125141

126-
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
127-
$isConnected = $redis->isConnected();
128-
restore_error_handler();
129-
if (!$isConnected) {
130-
$error = preg_match('/^Redis::p?connect\(\): (.*)/', $error, $error) ? sprintf(' (%s)', $error[1]) : '';
131-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$error.'.');
132-
}
133-
134-
if ((null !== $auth && !$redis->auth($auth))
135-
|| ($params['dbindex'] && !$redis->select($params['dbindex']))
136-
|| ($params['read_timeout'] && !$redis->setOption(\Redis::OPT_READ_TIMEOUT, $params['read_timeout']))
137-
) {
138-
$e = preg_replace('/^ERR /', '', $redis->getLastError());
139-
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e.'.');
140-
}
141-
142142
return true;
143143
};
144144

0 commit comments

Comments
 (0)
0