Skip to content

Commit f56989f

Browse files
jxblummp911de
authored andcommitted
Apply consistent Exception variable names to all catch blocks.
We now consistently align with the core Spring Framework's use of 'ex' as the variable name for Exceptions handled in catch blocks, and 'ignore' for all Exceptions thrown, but ignored by framework code. Both 'ex' and 'ignore' were appropriately used based on the context and nautre of the Exception handler in the catch block. Additionally, we use the 'expected' variable name for Exception thrown in tests where the thrown Exception is the expected outcome of the test case. Only 1 exception exists to these name conventions, and that is 'nested', which was necessarily used in ScanCursor due to the nested try-catch blocks. Applied consistent use of String.format(..) to Exception messages requiring formatting. Formatted catch block according to source code formatting style. Closes #2748 Original pull request: #2749
1 parent 68f514b commit f56989f

File tree

71 files changed

+363
-362
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+363
-362
lines changed

src/main/java/org/springframework/data/redis/cache/DefaultRedisCacheWriter.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,14 @@ private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection c
377377
while (doCheckLock(name, connection)) {
378378
Thread.sleep(this.sleepTime.toMillis());
379379
}
380-
} catch (InterruptedException cause) {
380+
} catch (InterruptedException ex) {
381381

382382
// Re-interrupt current Thread to allow other participants to react.
383383
Thread.currentThread().interrupt();
384384

385-
throw new PessimisticLockingFailureException(String.format("Interrupted while waiting to unlock cache %s", name),
386-
cause);
385+
String message = String.format("Interrupted while waiting to unlock cache %s", name);
386+
387+
throw new PessimisticLockingFailureException(message, ex);
387388
} finally {
388389
this.statistics.incLockTime(name, System.nanoTime() - lockWaitTimeNs);
389390
}

src/main/java/org/springframework/data/redis/cache/RedisCache.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ protected <T> T loadCacheValue(Object key, Callable<T> valueLoader) {
189189

190190
try {
191191
value = valueLoader.call();
192-
} catch (Exception cause) {
193-
throw new ValueRetrievalException(key, valueLoader, cause);
192+
} catch (Exception ex) {
193+
throw new ValueRetrievalException(key, valueLoader, ex);
194194
}
195195

196196
put(key, value);
@@ -425,14 +425,14 @@ protected String convertKey(Object key) {
425425
if (conversionService.canConvert(source, TypeDescriptor.valueOf(String.class))) {
426426
try {
427427
return conversionService.convert(key, String.class);
428-
} catch (ConversionFailedException cause) {
428+
} catch (ConversionFailedException ex) {
429429

430430
// May fail if the given key is a collection
431431
if (isCollectionLikeOrMap(source)) {
432432
return convertCollectionLikeOrMapKey(key, source);
433433
}
434434

435-
throw cause;
435+
throw ex;
436436
}
437437
}
438438

src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public void close() throws DataAccessException {
114114

115115
try {
116116
connection.close();
117-
} catch (IOException e) {
118-
LOGGER.info("Failed to close sentinel connection", e);
117+
} catch (IOException ex) {
118+
LOGGER.info("Failed to close sentinel connection", ex);
119119
}
120120
}
121121
}

src/main/java/org/springframework/data/redis/connection/ClusterCommandExecutor.java

+25-20
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ private <S, T> NodeResult<T> executeCommandOnSingleNode(ClusterCommandCallback<S
131131

132132
if (redirectCount > this.maxRedirects) {
133133

134-
throw new TooManyClusterRedirectionsException(String.format(
135-
"Cannot follow Cluster Redirects over more than %s legs; "
136-
+ "Consider increasing the number of redirects to follow; Current value is: %s.",
137-
redirectCount, this.maxRedirects));
134+
String message = String.format("Cannot follow Cluster Redirects over more than %s legs; "
135+
+ "Consider increasing the number of redirects to follow; Current value is: %s.",
136+
redirectCount, this.maxRedirects);
137+
138+
throw new TooManyClusterRedirectionsException(message);
138139
}
139140

140141
RedisClusterNode nodeToUse = lookupNode(node);
@@ -145,15 +146,19 @@ private <S, T> NodeResult<T> executeCommandOnSingleNode(ClusterCommandCallback<S
145146

146147
try {
147148
return new NodeResult<>(node, commandCallback.doInCluster(client));
148-
} catch (RuntimeException cause) {
149+
} catch (RuntimeException ex) {
149150

150-
RuntimeException translatedException = convertToDataAccessException(cause);
151+
RuntimeException translatedException = convertToDataAccessException(ex);
151152

152153
if (translatedException instanceof ClusterRedirectException clusterRedirectException) {
153-
return executeCommandOnSingleNode(commandCallback, topologyProvider.getTopology().lookup(
154-
clusterRedirectException.getTargetHost(), clusterRedirectException.getTargetPort()), redirectCount + 1);
154+
155+
String targetHost = clusterRedirectException.getTargetHost();
156+
int targetPort = clusterRedirectException.getTargetPort();
157+
RedisClusterNode clusterNode = topologyProvider.getTopology().lookup(targetHost, targetPort);
158+
159+
return executeCommandOnSingleNode(commandCallback, clusterNode, redirectCount + 1);
155160
} else {
156-
throw translatedException != null ? translatedException : cause;
161+
throw translatedException != null ? translatedException : ex;
157162
}
158163
} finally {
159164
this.resourceProvider.returnResourceForSpecificNode(nodeToUse, client);
@@ -172,8 +177,8 @@ private RedisClusterNode lookupNode(RedisClusterNode node) {
172177

173178
try {
174179
return topologyProvider.getTopology().lookup(node);
175-
} catch (ClusterStateFailureException cause) {
176-
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node), cause);
180+
} catch (ClusterStateFailureException ex) {
181+
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node), ex);
177182
}
178183
}
179184

@@ -209,8 +214,8 @@ public <S, T> MultiNodeResult<T> executeCommandAsyncOnNodes(ClusterCommandCallba
209214
for (RedisClusterNode node : nodes) {
210215
try {
211216
resolvedRedisClusterNodes.add(topology.lookup(node));
212-
} catch (ClusterStateFailureException cause) {
213-
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node), cause);
217+
} catch (ClusterStateFailureException ex) {
218+
throw new IllegalArgumentException(String.format("Node %s is unknown to cluster", node), ex);
214219
}
215220
}
216221

@@ -249,13 +254,13 @@ <T> MultiNodeResult<T> collectResults(Map<NodeExecution, Future<NodeResult<T>>>
249254
}
250255

251256
entryIterator.remove();
252-
} catch (ExecutionException exception) {
257+
} catch (ExecutionException ex) {
253258
entryIterator.remove();
254-
exceptionCollector.addException(nodeExecution, exception.getCause());
259+
exceptionCollector.addException(nodeExecution, ex.getCause());
255260
} catch (TimeoutException ignore) {
256-
} catch (InterruptedException exception) {
261+
} catch (InterruptedException ex) {
257262
Thread.currentThread().interrupt();
258-
exceptionCollector.addException(nodeExecution, exception);
263+
exceptionCollector.addException(nodeExecution, ex);
259264
break OUT;
260265
}
261266
}
@@ -316,11 +321,11 @@ private <S, T> NodeResult<T> executeMultiKeyCommandOnSingleNode(MultiKeyClusterC
316321

317322
try {
318323
return new NodeResult<>(node, commandCallback.doInCluster(client, key), key);
319-
} catch (RuntimeException cause) {
324+
} catch (RuntimeException ex) {
320325

321-
RuntimeException translatedException = convertToDataAccessException(cause);
326+
RuntimeException translatedException = convertToDataAccessException(ex);
322327

323-
throw translatedException != null ? translatedException : cause;
328+
throw translatedException != null ? translatedException : ex;
324329
} finally {
325330
this.resourceProvider.returnResourceForSpecificNode(node, client);
326331
}

src/main/java/org/springframework/data/redis/connection/RedisNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static RedisNode fromString(String hostPortString) {
101101
int port = -1;
102102
try {
103103
port = Integer.parseInt(portString);
104-
} catch (RuntimeException e) {
104+
} catch (RuntimeException ignore) {
105105
throw new IllegalArgumentException(String.format("Unparseable port number: %s", hostPortString));
106106
}
107107

src/main/java/org/springframework/data/redis/connection/convert/Converters.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ public static Properties toProperties(String source) {
109109

110110
try (StringReader stringReader = new StringReader(source)) {
111111
info.load(stringReader);
112-
} catch (Exception cause) {
113-
throw new RedisSystemException("Cannot read Redis info", cause);
112+
} catch (Exception ex) {
113+
throw new RedisSystemException("Cannot read Redis info", ex);
114114
}
115115

116116
return info;

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterConnection.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public JedisClusterConnection(JedisCluster cluster) {
123123
Object custerCommandExecutor = executorDfa.getPropertyValue("executor");
124124
DirectFieldAccessor dfa = new DirectFieldAccessor(custerCommandExecutor);
125125
clusterCommandExecutor.setMaxRedirects((Integer) dfa.getPropertyValue("maxRedirects"));
126-
} catch (Exception e) {
127-
// ignore it and work with the executor default
126+
} catch (Exception ignore) {
127+
// ignore and work with the executor default
128128
}
129129
}
130130

@@ -381,8 +381,8 @@ public void subscribe(MessageListener listener, byte[]... channels) {
381381
JedisMessageListener jedisPubSub = new JedisMessageListener(listener);
382382
subscription = new JedisSubscription(listener, jedisPubSub, channels, null);
383383
cluster.subscribe(jedisPubSub, channels);
384-
} catch (Exception cause) {
385-
throw convertJedisAccessException(cause);
384+
} catch (Exception ex) {
385+
throw convertJedisAccessException(ex);
386386
}
387387
}
388388

@@ -398,8 +398,8 @@ public void pSubscribe(MessageListener listener, byte[]... patterns) {
398398
JedisMessageListener jedisPubSub = new JedisMessageListener(listener);
399399
subscription = new JedisSubscription(listener, jedisPubSub, null, patterns);
400400
cluster.psubscribe(jedisPubSub, patterns);
401-
} catch (Exception cause) {
402-
throw convertJedisAccessException(cause);
401+
} catch (Exception ex) {
402+
throw convertJedisAccessException(ex);
403403
}
404404
}
405405

@@ -643,8 +643,8 @@ public void close() throws DataAccessException {
643643
if (!closed && disposeClusterCommandExecutorOnClose) {
644644
try {
645645
clusterCommandExecutor.destroy();
646-
} catch (Exception cause) {
647-
log.warn("Cannot properly close cluster command executor", cause);
646+
} catch (Exception ex) {
647+
log.warn("Cannot properly close cluster command executor", ex);
648648
}
649649
}
650650

@@ -864,8 +864,8 @@ public ClusterTopology getTopology() {
864864

865865
return cached;
866866

867-
} catch (Exception cause) {
868-
errors.put(entry.getKey(), cause);
867+
} catch (Exception ex) {
868+
errors.put(entry.getKey(), ex);
869869
}
870870
}
871871

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ protected JedisConnection(Jedis jedis, @Nullable Pool<Jedis> pool, JedisClientCo
193193
if (nodeConfig.getDatabase() != jedis.getDB()) {
194194
try {
195195
select(nodeConfig.getDatabase());
196-
} catch (DataAccessException cause) {
196+
} catch (DataAccessException ex) {
197197
close();
198-
throw cause;
198+
throw ex;
199199
}
200200
}
201201
}
@@ -413,17 +413,17 @@ private List<Object> convertPipelineResults() {
413413
if (!result.isStatus()) {
414414
results.add(result.conversionRequired() ? result.convert(data) : data);
415415
}
416-
} catch (JedisDataException e) {
417-
DataAccessException dataAccessException = convertJedisAccessException(e);
416+
} catch (JedisDataException ex) {
417+
DataAccessException dataAccessException = convertJedisAccessException(ex);
418418
if (cause == null) {
419419
cause = dataAccessException;
420420
}
421421
results.add(dataAccessException);
422-
} catch (DataAccessException e) {
422+
} catch (DataAccessException ex) {
423423
if (cause == null) {
424-
cause = e;
424+
cause = ex;
425425
}
426-
results.add(e);
426+
results.add(ex);
427427
}
428428
}
429429

@@ -488,8 +488,8 @@ public List<Object> exec() {
488488
? new TransactionResultConverter<>(txResults, JedisExceptionConverter.INSTANCE).convert(results)
489489
: results;
490490

491-
} catch (Exception cause) {
492-
throw convertJedisAccessException(cause);
491+
} catch (Exception ex) {
492+
throw convertJedisAccessException(ex);
493493
} finally {
494494
txResults.clear();
495495
transaction = null;
@@ -684,7 +684,7 @@ protected boolean isActive(RedisNode node) {
684684
verification = getJedis(node);
685685
verification.connect();
686686
return verification.ping().equalsIgnoreCase("pong");
687-
} catch (Exception cause) {
687+
} catch (Exception ignore) {
688688
return false;
689689
} finally {
690690
if (verification != null) {
@@ -708,17 +708,17 @@ private <T> T doWithJedis(Function<Jedis, T> callback) {
708708

709709
try {
710710
return callback.apply(getJedis());
711-
} catch (Exception cause) {
712-
throw convertJedisAccessException(cause);
711+
} catch (Exception ex) {
712+
throw convertJedisAccessException(ex);
713713
}
714714
}
715715

716716
private void doWithJedis(Consumer<Jedis> callback) {
717717

718718
try {
719719
callback.accept(getJedis());
720-
} catch (Exception cause) {
721-
throw convertJedisAccessException(cause);
720+
} catch (Exception ex) {
721+
throw convertJedisAccessException(ex);
722722
}
723723
}
724724

@@ -727,9 +727,9 @@ private void doExceptionThrowingOperationSafely(ExceptionThrowingOperation opera
727727
try {
728728
operation.run();
729729
}
730-
catch (Exception cause) {
730+
catch (Exception ex) {
731731
if (LOGGER.isDebugEnabled()) {
732-
LOGGER.debug(logMessage, cause);
732+
LOGGER.debug(logMessage, ex);
733733
}
734734
}
735735
}

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,8 @@ public void stop() {
703703
try {
704704
clusterCommandExecutor.destroy();
705705
this.clusterCommandExecutor = null;
706-
} catch (Exception cause) {
707-
throw new RuntimeException(cause);
706+
} catch (Exception ex) {
707+
throw new RuntimeException(ex);
708708
}
709709
}
710710

@@ -715,8 +715,8 @@ public void stop() {
715715
try {
716716
this.cluster.close();
717717
this.cluster = null;
718-
} catch (Exception cause) {
719-
log.warn("Cannot properly close Jedis cluster", cause);
718+
} catch (Exception ex) {
719+
log.warn("Cannot properly close Jedis cluster", ex);
720720
}
721721
}
722722

@@ -869,8 +869,8 @@ protected Jedis fetchJedisConnector() {
869869
jedis.connect();
870870

871871
return jedis;
872-
} catch (Exception cause) {
873-
throw new RedisConnectionFailureException("Cannot get Jedis connection", cause);
872+
} catch (Exception ex) {
873+
throw new RedisConnectionFailureException("Cannot get Jedis connection", ex);
874874
}
875875
}
876876

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnection.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ public Long clusterCountKeysInSlot(int slot) {
382382

383383
try {
384384
return getConnection().clusterCountKeysInSlot(slot);
385-
} catch (Exception cause) {
386-
throw this.exceptionConverter.translate(cause);
385+
} catch (Exception ex) {
386+
throw this.exceptionConverter.translate(ex);
387387
}
388388
}
389389

@@ -453,8 +453,8 @@ public List<byte[]> clusterGetKeysInSlot(int slot, Integer count) {
453453

454454
try {
455455
return getConnection().clusterGetKeysInSlot(slot, count);
456-
} catch (Exception cause) {
457-
throw this.exceptionConverter.translate(cause);
456+
} catch (Exception ex) {
457+
throw this.exceptionConverter.translate(ex);
458458
}
459459
}
460460

0 commit comments

Comments
 (0)