DATACASS-304 - CqlTemplate.doExecute does not translate Cassandra Exceptions.

This commit is contained in:
John Blum
2016-07-01 17:44:37 -07:00
parent ab73021187
commit 570d71a905
4 changed files with 59 additions and 23 deletions

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2013-2014 the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,7 +19,7 @@ import org.springframework.dao.UncategorizedDataAccessException;
/**
* Wrapper for all non RuntimeExceptions throws by the Cassandra Driver
*
*
* @author David Webb
*/
public class CassandraUncategorizedDataAccessException extends UncategorizedDataAccessException {
@@ -28,12 +28,12 @@ public class CassandraUncategorizedDataAccessException extends UncategorizedData
/**
* Create the Exception
*
* @param msg
*
* @param message
* @param cause
*/
public CassandraUncategorizedDataAccessException(String msg, Throwable cause) {
super(msg, cause);
public CassandraUncategorizedDataAccessException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -251,8 +251,8 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations {
try {
return callback.doInSession(getSession());
} catch (Exception e) {
throw translateExceptionIfPossible(e);
} catch (Throwable t) {
throw translateExceptionIfPossible(t);
}
}
@@ -335,7 +335,7 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations {
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception) {
throw translateExceptionIfPossible((Exception) e.getCause());
throw translateExceptionIfPossible(e.getCause());
}
throw new CassandraUncategorizedDataAccessException("Unknown Throwable", e.getCause());
}
@@ -886,8 +886,8 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations {
* @see <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#dao-exceptions">Consistent exception hierarchy</a>
*/
@SuppressWarnings("all")
protected RuntimeException translateExceptionIfPossible(Exception e) {
return translateExceptionIfPossible(e, getExceptionTranslator());
protected RuntimeException translateExceptionIfPossible(Throwable t) {
return translateExceptionIfPossible(t, getExceptionTranslator());
}
/**
@@ -899,14 +899,14 @@ public class CqlTemplate extends CassandraAccessor implements CqlOperations {
* @return
*/
@SuppressWarnings("all")
protected static RuntimeException translateExceptionIfPossible(Exception e,
protected static RuntimeException translateExceptionIfPossible(Throwable t,
PersistenceExceptionTranslator exceptionTranslator) {
Assert.notNull(e, "Exception must not be null");
Assert.notNull(t, "Throwble must not be null");
Assert.notNull(exceptionTranslator, "PersistenceExceptionTranslator must not be null");
return (e instanceof RuntimeException) ? potentiallyConvertRuntimeException((RuntimeException) e, exceptionTranslator)
: new CassandraUncategorizedDataAccessException("Caught Uncategorized Exception", e);
return (t instanceof RuntimeException) ? potentiallyConvertRuntimeException((RuntimeException) t, exceptionTranslator)
: new CassandraUncategorizedDataAccessException("Caught Uncategorized Exception", t);
}
/**

View File

@@ -30,6 +30,7 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cassandra.support.CassandraExceptionTranslator;
import org.springframework.cassandra.support.exception.CassandraReadTimeoutException;
import org.springframework.cassandra.support.exception.CassandraUncategorizedException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
@@ -38,6 +39,7 @@ import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.DriverException;
import com.datastax.driver.core.exceptions.ReadTimeoutException;
import com.datastax.driver.core.querybuilder.Select;
@@ -80,8 +82,11 @@ public class CqlTemplateUnitTests {
verify(mockSession, times(1)).execute(eq("test"));
}
/**
* @see <a href="https://jira.spring.io/browse/DATACASS-304">DATACASS-304</a>
*/
@Test
public void doExecuteInSessionCallbackTranslatesException() {
public void doExecuteInSessionCallbackTranslatesToCassandraException() {
exception.expect(CassandraReadTimeoutException.class);
exception.expectCause(org.hamcrest.Matchers.isA(ReadTimeoutException.class));
@@ -92,6 +97,38 @@ public class CqlTemplateUnitTests {
});
}
/**
* @see <a href="https://jira.spring.io/browse/DATACASS-304">DATACASS-304</a>
*/
@Test
public void doExecuteInSessionCallbackTranslatesToCassandraUncategorizedException() {
exception.expect(CassandraUncategorizedException.class);
exception.expectCause(org.hamcrest.Matchers.isA(DriverException.class));
exception.expectMessage(containsString("test"));
template.doExecute(new SessionCallback<String>() {
@Override public String doInSession(Session session) throws DataAccessException {
throw new DriverException("test");
}
});
}
/**
* @see <a href="https://jira.spring.io/browse/DATACASS-304">DATACASS-304</a>
*/
@Test
public void doExecuteInSessionCallbackTranslatesToCassandraUncategorizedDataAccessException() {
exception.expect(CassandraUncategorizedDataAccessException.class);
exception.expectCause(org.hamcrest.Matchers.isA(Error.class));
exception.expectMessage(containsString("test"));
template.doExecute(new SessionCallback<String>() {
@Override public String doInSession(Session session) throws DataAccessException {
throw new Error("test");
}
});
}
@Test
public void doExecuteWithNullSessionCallbackThrowsIllegalArgumentException() {
exception.expect(IllegalArgumentException.class);

View File

@@ -32,6 +32,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cassandra.support.exception.CassandraInvalidQueryException;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.cassandra.config.SchemaAction;
@@ -144,13 +145,11 @@ public class RepositoryQueryMethodParameterTypesIntegrationTests
}
/**
* @see DATACASS-296
* @see <a href="https://jira.spring.io/browse/DATACASS-296">DATACASS-296</a>
* @see <a href="https://jira.spring.io/browse/DATACASS-304">DATACASS-304</a>
*/
@Test(expected = InvalidQueryException.class)
@Test(expected = CassandraInvalidQueryException.class)
public void shouldThrowExceptionUsingWrongMethodParameter() {
// NOTE: InvalidQueryException is a driver exception. This should get fixed with DATACASS-304
session.execute("CREATE INDEX IF NOT EXISTS allpossibletypes_date ON allpossibletypes ( date )");
allPossibleTypesRepository.findWithDateParameter(Date.from(Instant.ofEpochSecond(44234123421L)));
}