Reliably expose nested cause exception message for PersistenceException
Issue: SPR-16559
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -44,19 +44,19 @@ public class NestedExceptionTests {
|
|||||||
nex.printStackTrace(pw);
|
nex.printStackTrace(pw);
|
||||||
pw.flush();
|
pw.flush();
|
||||||
String stackTrace = new String(baos.toByteArray());
|
String stackTrace = new String(baos.toByteArray());
|
||||||
assertFalse(stackTrace.indexOf(mesg) == -1);
|
assertTrue(stackTrace.contains(mesg));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nestedRuntimeExceptionWithRootCause() {
|
public void nestedRuntimeExceptionWithRootCause() {
|
||||||
String myMessage = "mesg for this exception";
|
String myMessage = "mesg for this exception";
|
||||||
String rootCauseMesg = "this is the obscure message of the root cause";
|
String rootCauseMsg = "this is the obscure message of the root cause";
|
||||||
Exception rootCause = new Exception(rootCauseMesg);
|
Exception rootCause = new Exception(rootCauseMsg);
|
||||||
// Making a class abstract doesn't _really_ prevent instantiation :-)
|
// Making a class abstract doesn't _really_ prevent instantiation :-)
|
||||||
NestedRuntimeException nex = new NestedRuntimeException(myMessage, rootCause) {};
|
NestedRuntimeException nex = new NestedRuntimeException(myMessage, rootCause) {};
|
||||||
assertEquals(nex.getCause(), rootCause);
|
assertEquals(nex.getCause(), rootCause);
|
||||||
assertTrue(nex.getMessage().indexOf(myMessage) != -1);
|
assertTrue(nex.getMessage().contains(myMessage));
|
||||||
assertTrue(nex.getMessage().indexOf(rootCauseMesg) != -1);
|
assertTrue(nex.getMessage().endsWith(rootCauseMsg));
|
||||||
|
|
||||||
// check PrintStackTrace
|
// check PrintStackTrace
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
@@ -64,8 +64,8 @@ public class NestedExceptionTests {
|
|||||||
nex.printStackTrace(pw);
|
nex.printStackTrace(pw);
|
||||||
pw.flush();
|
pw.flush();
|
||||||
String stackTrace = new String(baos.toByteArray());
|
String stackTrace = new String(baos.toByteArray());
|
||||||
assertFalse(stackTrace.indexOf(rootCause.getClass().getName()) == -1);
|
assertTrue(stackTrace.contains(rootCause.getClass().getName()));
|
||||||
assertFalse(stackTrace.indexOf(rootCauseMesg) == -1);
|
assertTrue(stackTrace.contains(rootCauseMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -82,19 +82,19 @@ public class NestedExceptionTests {
|
|||||||
nex.printStackTrace(pw);
|
nex.printStackTrace(pw);
|
||||||
pw.flush();
|
pw.flush();
|
||||||
String stackTrace = new String(baos.toByteArray());
|
String stackTrace = new String(baos.toByteArray());
|
||||||
assertFalse(stackTrace.indexOf(mesg) == -1);
|
assertTrue(stackTrace.contains(mesg));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nestedCheckedExceptionWithRootCause() {
|
public void nestedCheckedExceptionWithRootCause() {
|
||||||
String myMessage = "mesg for this exception";
|
String myMessage = "mesg for this exception";
|
||||||
String rootCauseMesg = "this is the obscure message of the root cause";
|
String rootCauseMsg = "this is the obscure message of the root cause";
|
||||||
Exception rootCause = new Exception(rootCauseMesg);
|
Exception rootCause = new Exception(rootCauseMsg);
|
||||||
// Making a class abstract doesn't _really_ prevent instantiation :-)
|
// Making a class abstract doesn't _really_ prevent instantiation :-)
|
||||||
NestedCheckedException nex = new NestedCheckedException(myMessage, rootCause) {};
|
NestedCheckedException nex = new NestedCheckedException(myMessage, rootCause) {};
|
||||||
assertEquals(nex.getCause(), rootCause);
|
assertEquals(nex.getCause(), rootCause);
|
||||||
assertTrue(nex.getMessage().indexOf(myMessage) != -1);
|
assertTrue(nex.getMessage().contains(myMessage));
|
||||||
assertTrue(nex.getMessage().indexOf(rootCauseMesg) != -1);
|
assertTrue(nex.getMessage().endsWith(rootCauseMsg));
|
||||||
|
|
||||||
// check PrintStackTrace
|
// check PrintStackTrace
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
@@ -102,8 +102,8 @@ public class NestedExceptionTests {
|
|||||||
nex.printStackTrace(pw);
|
nex.printStackTrace(pw);
|
||||||
pw.flush();
|
pw.flush();
|
||||||
String stackTrace = new String(baos.toByteArray());
|
String stackTrace = new String(baos.toByteArray());
|
||||||
assertFalse(stackTrace.indexOf(rootCause.getClass().getName()) == -1);
|
assertTrue(stackTrace.contains(rootCause.getClass().getName()));
|
||||||
assertFalse(stackTrace.indexOf(rootCauseMesg) == -1);
|
assertTrue(stackTrace.contains(rootCauseMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -425,8 +425,13 @@ public class LocalSessionFactoryBuilder extends Configuration {
|
|||||||
throw new IllegalStateException("Interrupted during initialization of Hibernate SessionFactory", ex);
|
throw new IllegalStateException("Interrupted during initialization of Hibernate SessionFactory", ex);
|
||||||
}
|
}
|
||||||
catch (ExecutionException ex) {
|
catch (ExecutionException ex) {
|
||||||
|
Throwable cause = ex.getCause();
|
||||||
|
if (cause instanceof HibernateException) {
|
||||||
|
// Rethrow a provider configuration exception (possibly with a nested cause) directly
|
||||||
|
throw (HibernateException) cause;
|
||||||
|
}
|
||||||
throw new IllegalStateException("Failed to asynchronously initialize Hibernate SessionFactory: " +
|
throw new IllegalStateException("Failed to asynchronously initialize Hibernate SessionFactory: " +
|
||||||
ex.getMessage(), ex.getCause());
|
ex.getMessage(), cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -385,11 +385,32 @@ public abstract class AbstractEntityManagerFactoryBean implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
private EntityManagerFactory buildNativeEntityManagerFactory() {
|
private EntityManagerFactory buildNativeEntityManagerFactory() {
|
||||||
EntityManagerFactory emf = createNativeEntityManagerFactory();
|
EntityManagerFactory emf;
|
||||||
|
try {
|
||||||
|
emf = createNativeEntityManagerFactory();
|
||||||
|
}
|
||||||
|
catch (PersistenceException ex) {
|
||||||
|
if (ex.getClass() == PersistenceException.class) {
|
||||||
|
// Plain PersistenceException wrapper for underlying exception?
|
||||||
|
// Make sure the nested exception message is properly exposed,
|
||||||
|
// along the lines of Spring's NestedRuntimeException.getMessage()
|
||||||
|
Throwable cause = ex.getCause();
|
||||||
|
if (cause != null) {
|
||||||
|
String message = ex.getMessage();
|
||||||
|
String causeString = cause.toString();
|
||||||
|
if (!message.endsWith(causeString)) {
|
||||||
|
throw new PersistenceException(message + "; nested exception is " + causeString, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
|
||||||
JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter();
|
JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter();
|
||||||
if (jpaVendorAdapter != null) {
|
if (jpaVendorAdapter != null) {
|
||||||
jpaVendorAdapter.postProcessEntityManagerFactory(emf);
|
jpaVendorAdapter.postProcessEntityManagerFactory(emf);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
logger.info("Initialized JPA EntityManagerFactory for persistence unit '" + getPersistenceUnitName() + "'");
|
logger.info("Initialized JPA EntityManagerFactory for persistence unit '" + getPersistenceUnitName() + "'");
|
||||||
}
|
}
|
||||||
@@ -416,6 +437,7 @@ public abstract class AbstractEntityManagerFactoryBean implements
|
|||||||
ifcs.add(EntityManagerFactory.class);
|
ifcs.add(EntityManagerFactory.class);
|
||||||
}
|
}
|
||||||
ifcs.add(EntityManagerFactoryInfo.class);
|
ifcs.add(EntityManagerFactoryInfo.class);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return (EntityManagerFactory) Proxy.newProxyInstance(this.beanClassLoader,
|
return (EntityManagerFactory) Proxy.newProxyInstance(this.beanClassLoader,
|
||||||
ClassUtils.toClassArray(ifcs), new ManagedEntityManagerFactoryInvocationHandler(this));
|
ClassUtils.toClassArray(ifcs), new ManagedEntityManagerFactoryInvocationHandler(this));
|
||||||
@@ -521,8 +543,13 @@ public abstract class AbstractEntityManagerFactoryBean implements
|
|||||||
throw new IllegalStateException("Interrupted during initialization of native EntityManagerFactory", ex);
|
throw new IllegalStateException("Interrupted during initialization of native EntityManagerFactory", ex);
|
||||||
}
|
}
|
||||||
catch (ExecutionException ex) {
|
catch (ExecutionException ex) {
|
||||||
|
Throwable cause = ex.getCause();
|
||||||
|
if (cause instanceof PersistenceException) {
|
||||||
|
// Rethrow a provider configuration exception (possibly with a nested cause) directly
|
||||||
|
throw (PersistenceException) cause;
|
||||||
|
}
|
||||||
throw new IllegalStateException("Failed to asynchronously initialize native EntityManagerFactory: " +
|
throw new IllegalStateException("Failed to asynchronously initialize native EntityManagerFactory: " +
|
||||||
ex.getMessage(), ex.getCause());
|
ex.getMessage(), cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user