How to prevent Jedis wrapping your exceptions

Yusuf Erdo
Think out of the net
1 min readNov 4, 2015

When you integrate Jedis in to your application, and if you are using Spring 3.2.x with it then you will realize some of your exception are wrapped by Jedis and you get:

Caused by: org.springframework.data.redis.RedisSystemException: Unknown jedis exception; nested exception is …

Nested exception would be your actual exception. When your exception is wrapped with another exception you might not be able to catch the correct exception. And also it simply confuses everyone.

Spring DAO looks for beans with interface PersistenceExceptionTranslator and JedisConnectionFactory implements this indirectly via RedisConnectionFactory. So suddenly you have a new Exception translator and JedisConnectionFactory is more aggressive to wrap exceptions.

So this is a problem, how can you avoid. I think later versions of Spring might not have this problem and it might be configurable for Exception translators. But for Spring 3.2.x we seem to be unlucky, but hey, there’s this piece of code:

// Let it throw raw if the type of the exception is on the throws clause of the method.
if (!this.alwaysTranslate && ReflectionUtils.declaresException(mi.getMethod(), ex.getClass())) {
throw ex;
}
else {
throw DataAccessUtils.translateIfNecessary(ex, this.persistenceExceptionTranslator);
}

So ReflectionUtils.declaresException(mi.getMethod(), ex.getClass()) is the key.

If you explicitly declare your exception on whichever method throws it, you will get that exception without being wrapped.

--

--

Yusuf Erdo
Think out of the net

Writing about #genai #llm #openai #typescript #graphql #serverless #lambda #dynamodb #elasticsearch