throw JedisDataException when sending NULL values to redis as it is not a valid value in the protocol
This commit is contained in:
@@ -3,6 +3,7 @@ package redis.clients.util;
|
|||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import redis.clients.jedis.Protocol;
|
import redis.clients.jedis.Protocol;
|
||||||
|
import redis.clients.jedis.exceptions.JedisDataException;
|
||||||
import redis.clients.jedis.exceptions.JedisException;
|
import redis.clients.jedis.exceptions.JedisException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,6 +13,10 @@ import redis.clients.jedis.exceptions.JedisException;
|
|||||||
public class SafeEncoder {
|
public class SafeEncoder {
|
||||||
public static byte[] encode(final String str) {
|
public static byte[] encode(final String str) {
|
||||||
try {
|
try {
|
||||||
|
if (str == null) {
|
||||||
|
throw new JedisDataException(
|
||||||
|
"value sent to redis cannot be null");
|
||||||
|
}
|
||||||
return str.getBytes(Protocol.CHARSET);
|
return str.getBytes(Protocol.CHARSET);
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw new JedisException(e);
|
throw new JedisException(e);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import redis.clients.jedis.Jedis;
|
|||||||
import redis.clients.jedis.JedisShardInfo;
|
import redis.clients.jedis.JedisShardInfo;
|
||||||
import redis.clients.jedis.Protocol;
|
import redis.clients.jedis.Protocol;
|
||||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||||
|
import redis.clients.jedis.exceptions.JedisDataException;
|
||||||
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
|
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
|
||||||
import redis.clients.util.SafeEncoder;
|
import redis.clients.util.SafeEncoder;
|
||||||
|
|
||||||
@@ -53,4 +54,9 @@ public class JedisTest extends JedisCommandTestBase {
|
|||||||
Thread.sleep(20000);
|
Thread.sleep(20000);
|
||||||
jedis.hmget("foobar", "foo");
|
jedis.hmget("foobar", "foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = JedisDataException.class)
|
||||||
|
public void failWhenSendingNullValues() {
|
||||||
|
jedis.set("foo", null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user