Skip to content

Commit 21125f0

Browse files
committed
Introduce JedisBatchOperationException
- Pipeline and Transaction related exceptions, which do not come from Redis, are changed to throw JedisBatchOperationException. - SafeEncoder is changed to throw JedisException. Based on discussions in #1183
1 parent 53194dd commit 21125f0

File tree

9 files changed

+41
-31
lines changed

9 files changed

+41
-31
lines changed

src/main/java/redis/clients/jedis/BinaryJedis.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import redis.clients.jedis.commands.BinaryScriptingCommands;
2727
import redis.clients.jedis.commands.MultiKeyBinaryCommands;
2828
import redis.clients.jedis.exceptions.InvalidURIException;
29-
import redis.clients.jedis.exceptions.JedisDataException;
29+
import redis.clients.jedis.exceptions.JedisBatchOperationException;
3030
import redis.clients.jedis.exceptions.JedisException;
3131
import redis.clients.jedis.params.ClientKillParams;
3232
import redis.clients.jedis.params.GeoRadiusParam;
@@ -1850,11 +1850,11 @@ public Transaction multi() {
18501850

18511851
protected void checkIsInMultiOrPipeline() {
18521852
if (client.isInMulti()) {
1853-
throw new JedisDataException(
1853+
throw new JedisBatchOperationException(
18541854
"Cannot use Jedis when in Multi. Please use Transaction or reset jedis state.");
18551855
} else if (pipeline != null && pipeline.hasPipelinedResponse()) {
1856-
throw new JedisDataException(
1857-
"Cannot use Jedis when in Pipeline. Please use Pipeline or reset jedis state .");
1856+
throw new JedisBatchOperationException(
1857+
"Cannot use Jedis when in Pipeline. Please use Pipeline or reset jedis state.");
18581858
}
18591859
}
18601860

src/main/java/redis/clients/jedis/Pipeline.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7+
import redis.clients.jedis.exceptions.JedisBatchOperationException;
78
import redis.clients.jedis.exceptions.JedisDataException;
89

910
public class Pipeline extends MultiKeyPipelineBase implements Closeable {
@@ -20,8 +21,8 @@ public List<Object> build(Object data) {
2021
List<Object> values = new ArrayList<Object>();
2122

2223
if (list.size() != responses.size()) {
23-
throw new JedisDataException("Expected data size " + responses.size() + " but was "
24-
+ list.size());
24+
throw new JedisBatchOperationException(
25+
"Expected data size " + responses.size() + " but was " + list.size());
2526
}
2627

2728
for (int i = 0; i < list.size(); i++) {
@@ -126,14 +127,14 @@ public List<Object> syncAndReturnAll() {
126127
}
127128

128129
public Response<String> discard() {
129-
if (currentMulti == null) throw new JedisDataException("DISCARD without MULTI");
130+
if (currentMulti == null) throw new JedisBatchOperationException("DISCARD without MULTI");
130131
client.discard();
131132
currentMulti = null;
132133
return getResponse(BuilderFactory.STRING);
133134
}
134135

135136
public Response<List<Object>> exec() {
136-
if (currentMulti == null) throw new JedisDataException("EXEC without MULTI");
137+
if (currentMulti == null) throw new JedisBatchOperationException("EXEC without MULTI");
137138

138139
client.exec();
139140
Response<List<Object>> response = super.getResponse(currentMulti);
@@ -143,11 +144,10 @@ public Response<List<Object>> exec() {
143144
}
144145

145146
public Response<String> multi() {
146-
if (currentMulti != null) throw new JedisDataException("MULTI calls can not be nested");
147+
if (currentMulti != null) throw new JedisBatchOperationException("MULTI calls can not be nested");
147148

148149
client.multi();
149-
Response<String> response = getResponse(BuilderFactory.STRING); // Expecting
150-
// OK
150+
Response<String> response = getResponse(BuilderFactory.STRING); // Expecting OK
151151
currentMulti = new MultiResponseBuilder();
152152
return response;
153153
}

src/main/java/redis/clients/jedis/Response.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package redis.clients.jedis;
22

3+
import redis.clients.jedis.exceptions.JedisBatchOperationException;
34
import redis.clients.jedis.exceptions.JedisDataException;
45

56
public class Response<T> {
@@ -24,13 +25,12 @@ public void set(Object data) {
2425
}
2526

2627
public T get() {
27-
// if response has dependency response and dependency is not built,
28-
// build it first and no more!!
28+
// if response has dependency response and dependency is not built, build it first and no more!!
2929
if (dependency != null && dependency.set && !dependency.built) {
3030
dependency.build();
3131
}
3232
if (!set) {
33-
throw new JedisDataException(
33+
throw new JedisBatchOperationException(
3434
"Please close pipeline or multi block before calling this method.");
3535
}
3636
if (!built) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package redis.clients.jedis.exceptions;
2+
3+
public class JedisBatchOperationException extends JedisException {
4+
private static final long serialVersionUID = -1464241173812705493L;
5+
6+
public JedisBatchOperationException(String message) {
7+
super(message);
8+
}
9+
10+
}

src/main/java/redis/clients/jedis/util/SafeEncoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.UnsupportedEncodingException;
44

55
import redis.clients.jedis.Protocol;
6-
import redis.clients.jedis.exceptions.JedisDataException;
76
import redis.clients.jedis.exceptions.JedisException;
87

98
/**
@@ -25,7 +24,7 @@ public static byte[][] encodeMany(final String... strs) {
2524
public static byte[] encode(final String str) {
2625
try {
2726
if (str == null) {
28-
throw new JedisDataException("value sent to redis cannot be null");
27+
throw new JedisException("null value cannot be sent to redis");
2928
}
3029
return str.getBytes(Protocol.CHARSET);
3130
} catch (UnsupportedEncodingException e) {

src/test/java/redis/clients/jedis/tests/JedisTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import redis.clients.jedis.Protocol;
1919
import redis.clients.jedis.exceptions.InvalidURIException;
2020
import redis.clients.jedis.exceptions.JedisConnectionException;
21-
import redis.clients.jedis.exceptions.JedisDataException;
2221
import redis.clients.jedis.exceptions.JedisException;
2322
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
2423
import redis.clients.jedis.util.SafeEncoder;
@@ -70,7 +69,7 @@ public void timeoutConnectionWithURI() throws Exception {
7069
jedis.hmget("foobar", "foo");
7170
}
7271

73-
@Test(expected = JedisDataException.class)
72+
@Test(expected = JedisException.class)
7473
public void failWhenSendingNullValues() {
7574
jedis.set("foo", null);
7675
}

src/test/java/redis/clients/jedis/tests/PipeliningTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import redis.clients.jedis.Pipeline;
3232
import redis.clients.jedis.Response;
3333
import redis.clients.jedis.Tuple;
34+
import redis.clients.jedis.exceptions.JedisBatchOperationException;
3435
import redis.clients.jedis.exceptions.JedisDataException;
3536
import redis.clients.jedis.util.SafeEncoder;
3637

@@ -191,7 +192,7 @@ public void pipelineResponseWithoutData() {
191192
assertNull(score.get());
192193
}
193194

194-
@Test(expected = JedisDataException.class)
195+
@Test(expected = JedisBatchOperationException.class)
195196
public void pipelineResponseWithinPipeline() {
196197
jedis.set("string", "foo");
197198

@@ -308,27 +309,27 @@ public void multiWithSync() {
308309
assertEquals("world", r3.get());
309310
}
310311

311-
@Test(expected = JedisDataException.class)
312-
public void pipelineExecShoudThrowJedisDataExceptionWhenNotInMulti() {
312+
@Test(expected = JedisBatchOperationException.class)
313+
public void pipelineExecShoudThrowBatchExceptionWhenNotInMulti() {
313314
Pipeline pipeline = jedis.pipelined();
314315
pipeline.exec();
315316
}
316317

317-
@Test(expected = JedisDataException.class)
318-
public void pipelineDiscardShoudThrowJedisDataExceptionWhenNotInMulti() {
318+
@Test(expected = JedisBatchOperationException.class)
319+
public void pipelineDiscardShoudThrowBatchExceptionWhenNotInMulti() {
319320
Pipeline pipeline = jedis.pipelined();
320321
pipeline.discard();
321322
}
322323

323-
@Test(expected = JedisDataException.class)
324-
public void pipelineMultiShoudThrowJedisDataExceptionWhenAlreadyInMulti() {
324+
@Test(expected = JedisBatchOperationException.class)
325+
public void pipelineMultiShoudThrowBatchExceptionWhenAlreadyInMulti() {
325326
Pipeline pipeline = jedis.pipelined();
326327
pipeline.multi();
327328
pipeline.set("foo", "3");
328329
pipeline.multi();
329330
}
330331

331-
@Test(expected = JedisDataException.class)
332+
@Test(expected = JedisBatchOperationException.class)
332333
public void testJedisThowExceptionWhenInPipeline() {
333334
Pipeline pipeline = jedis.pipelined();
334335
pipeline.set("foo", "3");
@@ -636,7 +637,7 @@ public void testCloseableWithMulti() throws IOException {
636637
try {
637638
pipeline.exec();
638639
fail("close should discard transaction");
639-
} catch (JedisDataException e) {
640+
} catch (JedisBatchOperationException e) {
640641
assertTrue(e.getMessage().contains("EXEC without MULTI"));
641642
// pass
642643
}

src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import redis.clients.jedis.ShardedJedis;
2424
import redis.clients.jedis.ShardedJedisPipeline;
2525
import redis.clients.jedis.Tuple;
26-
import redis.clients.jedis.exceptions.JedisDataException;
26+
import redis.clients.jedis.exceptions.JedisBatchOperationException;
2727

2828
public class ShardedJedisPipelineTest {
2929

@@ -109,7 +109,7 @@ public void pipelineResponse() {
109109
assertEquals(1, zrangeWithScores.get().size());
110110
}
111111

112-
@Test(expected = JedisDataException.class)
112+
@Test(expected = JedisBatchOperationException.class)
113113
public void pipelineResponseWithinPipeline() {
114114
jedis.set("string", "foo");
115115

src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import redis.clients.jedis.Protocol.Keyword;
2323
import redis.clients.jedis.Response;
2424
import redis.clients.jedis.Transaction;
25+
import redis.clients.jedis.exceptions.JedisBatchOperationException;
2526
import redis.clients.jedis.exceptions.JedisDataException;
2627

2728
public class TransactionCommandsTest extends JedisCommandTestBase {
@@ -145,7 +146,7 @@ public void unwatch() throws UnknownHostException, IOException {
145146
assertEquals("OK", resp.get(0));
146147
}
147148

148-
@Test(expected = JedisDataException.class)
149+
@Test(expected = JedisBatchOperationException.class)
149150
public void validateWhenInMulti() {
150151
jedis.multi();
151152
jedis.ping();
@@ -204,7 +205,7 @@ public void transactionResponseBinary() {
204205
assertArrayEquals("foo".getBytes(), set.get());
205206
}
206207

207-
@Test(expected = JedisDataException.class)
208+
@Test(expected = JedisBatchOperationException.class)
208209
public void transactionResponseWithinPipeline() {
209210
jedis.set("string", "foo");
210211

0 commit comments

Comments
 (0)