mirror of
https://github.com/google/nomulus.git
synced 2025-05-24 21:20:08 +02:00
Don't wrap exceptions thrown inside Cloud SQL transactions (#338)
* Don't wrap exceptions thrown inside Cloud SQL transactions There's no reason to wrap them in PersistenceException, and it makes dealing with thrown exceptions harder as seen in the diffs on test classes in this commit.
This commit is contained in:
parent
6f87fc115f
commit
5f62f91cd5
6 changed files with 13 additions and 31 deletions
|
@ -41,6 +41,7 @@ public final class RegistryLockDao {
|
||||||
Long.class)
|
Long.class)
|
||||||
.setParameter("verificationCode", verificationCode)
|
.setParameter("verificationCode", verificationCode)
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
|
// TODO(gbrodman): Don't throw NPE here. Maybe NoResultException fits better?
|
||||||
checkNotNull(revisionId, "No registry lock with this code");
|
checkNotNull(revisionId, "No registry lock with this code");
|
||||||
return em.find(RegistryLock.class, revisionId);
|
return em.find(RegistryLock.class, revisionId);
|
||||||
});
|
});
|
||||||
|
|
|
@ -79,17 +79,14 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
|
||||||
T result = work.run();
|
T result = work.run();
|
||||||
txn.commit();
|
txn.commit();
|
||||||
return result;
|
return result;
|
||||||
} catch (Throwable transactionException) {
|
} catch (RuntimeException e) {
|
||||||
String rollbackMessage;
|
|
||||||
try {
|
try {
|
||||||
txn.rollback();
|
txn.rollback();
|
||||||
rollbackMessage = "transaction rolled back";
|
logger.atWarning().log("Error during transaction; transaction rolled back");
|
||||||
} catch (Throwable rollbackException) {
|
} catch (Throwable rollbackException) {
|
||||||
logger.atSevere().withCause(rollbackException).log("Rollback failed, suppressing error");
|
logger.atSevere().withCause(rollbackException).log("Rollback failed; suppressing error");
|
||||||
rollbackMessage = "transaction rollback failed";
|
|
||||||
}
|
}
|
||||||
throw new PersistenceException(
|
throw e;
|
||||||
"Error during transaction, " + rollbackMessage, transactionException);
|
|
||||||
} finally {
|
} finally {
|
||||||
txnInfo.clear();
|
txnInfo.clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@ import google.registry.schema.domain.RegistryLock.Action;
|
||||||
import google.registry.testing.AppEngineRule;
|
import google.registry.testing.AppEngineRule;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.persistence.PersistenceException;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
@ -54,15 +53,11 @@ public final class RegistryLockDaoTest {
|
||||||
public void testSaveAndLoad_failure_differentCode() {
|
public void testSaveAndLoad_failure_differentCode() {
|
||||||
RegistryLock lock = createLock();
|
RegistryLock lock = createLock();
|
||||||
RegistryLockDao.save(lock);
|
RegistryLockDao.save(lock);
|
||||||
PersistenceException exception =
|
NullPointerException thrown =
|
||||||
assertThrows(
|
assertThrows(
|
||||||
PersistenceException.class,
|
NullPointerException.class,
|
||||||
() -> RegistryLockDao.getByVerificationCode(UUID.randomUUID().toString()));
|
() -> RegistryLockDao.getByVerificationCode(UUID.randomUUID().toString()));
|
||||||
assertThat(exception)
|
assertThat(thrown).hasMessageThat().isEqualTo("No registry lock with this code");
|
||||||
.hasCauseThat()
|
|
||||||
.hasMessageThat()
|
|
||||||
.isEqualTo("No registry lock with this code");
|
|
||||||
assertThat(exception).hasCauseThat().isInstanceOf(NullPointerException.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -22,7 +22,6 @@ import google.registry.model.transaction.JpaTransactionManagerRule;
|
||||||
import google.registry.schema.tmch.ClaimsList;
|
import google.registry.schema.tmch.ClaimsList;
|
||||||
import google.registry.testing.FakeClock;
|
import google.registry.testing.FakeClock;
|
||||||
import javax.persistence.NoResultException;
|
import javax.persistence.NoResultException;
|
||||||
import javax.persistence.PersistenceException;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
@ -71,9 +70,7 @@ public class ClaimsListDaoTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getCurrent_throwsNoResultExceptionIfTableIsEmpty() {
|
public void getCurrent_throwsNoResultExceptionIfTableIsEmpty() {
|
||||||
PersistenceException thrown =
|
assertThrows(NoResultException.class, ClaimsListDao::getCurrent);
|
||||||
assertThrows(PersistenceException.class, () -> ClaimsListDao.getCurrent());
|
|
||||||
assertThat(thrown).hasCauseThat().isInstanceOf(NoResultException.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -75,11 +75,7 @@ public class CurrencyUnitConverterTest {
|
||||||
jpaTm()
|
jpaTm()
|
||||||
.transact(
|
.transact(
|
||||||
() -> jpaTm().getEntityManager().find(TestEntity.class, "id").currency));
|
() -> jpaTm().getEntityManager().find(TestEntity.class, "id").currency));
|
||||||
assertThat(thrown)
|
assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("Unknown currency 'XXXX'");
|
||||||
.hasCauseThat()
|
|
||||||
.hasCauseThat()
|
|
||||||
.hasMessageThat()
|
|
||||||
.isEqualTo("Unknown currency 'XXXX'");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = "TestEntity") // Override entity name to avoid the nested class reference.
|
@Entity(name = "TestEntity") // Override entity name to avoid the nested class reference.
|
||||||
|
|
|
@ -21,7 +21,6 @@ import static google.registry.testing.JUnitBackports.assertThrows;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import google.registry.model.transaction.JpaTransactionManagerRule;
|
import google.registry.model.transaction.JpaTransactionManagerRule;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import javax.persistence.PersistenceException;
|
|
||||||
import org.joda.money.CurrencyUnit;
|
import org.joda.money.CurrencyUnit;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -68,16 +67,13 @@ public class PremiumListDaoTest {
|
||||||
@Test
|
@Test
|
||||||
public void saveNew_throwsWhenPremiumListAlreadyExists() {
|
public void saveNew_throwsWhenPremiumListAlreadyExists() {
|
||||||
PremiumListDao.saveNew(PremiumList.create("testlist", CurrencyUnit.USD, TEST_PRICES));
|
PremiumListDao.saveNew(PremiumList.create("testlist", CurrencyUnit.USD, TEST_PRICES));
|
||||||
PersistenceException thrown =
|
IllegalArgumentException thrown =
|
||||||
assertThrows(
|
assertThrows(
|
||||||
PersistenceException.class,
|
IllegalArgumentException.class,
|
||||||
() ->
|
() ->
|
||||||
PremiumListDao.saveNew(
|
PremiumListDao.saveNew(
|
||||||
PremiumList.create("testlist", CurrencyUnit.USD, TEST_PRICES)));
|
PremiumList.create("testlist", CurrencyUnit.USD, TEST_PRICES)));
|
||||||
assertThat(thrown)
|
assertThat(thrown).hasMessageThat().contains("A premium list of this name already exists");
|
||||||
.hasCauseThat()
|
|
||||||
.hasMessageThat()
|
|
||||||
.contains("A premium list of this name already exists");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue