Skip to content

Commit 1af997f

Browse files
authored
fix string deserialize exception (#74)
* fix deserialize string exception * add wrongtype tests
1 parent 43f61e0 commit 1af997f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

core/src/main/java/dev/keva/core/command/mapping/CommandMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import lombok.Getter;
2020
import lombok.extern.slf4j.Slf4j;
2121
import lombok.val;
22+
import org.apache.commons.lang3.SerializationException;
2223
import org.reflections.Reflections;
2324

2425
import java.lang.reflect.InvocationTargetException;
@@ -120,7 +121,7 @@ public void init() {
120121
} catch (Exception e) {
121122
log.error(e.getMessage(), e);
122123
if (e instanceof InvocationTargetException) {
123-
if (e.getCause() instanceof ClassCastException) {
124+
if (e.getCause() instanceof SerializationException || e.getCause() instanceof ClassCastException) {
124125
return new ErrorReply("ERR WRONGTYPE Operation against a key holding the wrong kind of value");
125126
}
126127
return new ErrorReply("ERR " + e.getCause().getMessage());

core/src/test/java/dev/keva/core/server/AbstractServerTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,39 @@ void del() {
106106
}
107107
}
108108

109+
@Test
110+
void wrongTypeSet() {
111+
try {
112+
val setAbc = jedis.set("abc", "123");
113+
assertEquals("OK", setAbc);
114+
jedis.lpush("abc", "123");
115+
} catch (Exception e) {
116+
assertEquals(JedisDataException.class, e.getClass());
117+
}
118+
}
119+
120+
@Test
121+
void wrongTypeSet2() {
122+
try {
123+
val setAbc = jedis.lpush("abc", "123");
124+
assertEquals(1, setAbc);
125+
jedis.hset("abc", "key", "val");
126+
} catch (Exception e) {
127+
assertEquals(JedisDataException.class, e.getClass());
128+
}
129+
}
130+
131+
@Test
132+
void wrongTypeSet3() {
133+
try {
134+
val setAbc = jedis.hset("abc", "key", "val");
135+
assertEquals(1, setAbc);
136+
jedis.lpush("abc", "123");
137+
} catch (Exception e) {
138+
assertEquals(JedisDataException.class, e.getClass());
139+
}
140+
}
141+
109142
@Test
110143
void transaction() {
111144
try {

0 commit comments

Comments
 (0)