improve a couple of exception messages

This commit is contained in:
Gavin King 2023-07-16 12:19:21 +02:00
parent 52bfbe06f2
commit 8611abe902
5 changed files with 14 additions and 12 deletions

View File

@ -27,7 +27,7 @@ public class InstantiationException extends HibernateException {
} }
/** /**
* Constructs a InstantiationException. * Constructs an {@code InstantiationException}.
* *
* @param message A message explaining the exception condition * @param message A message explaining the exception condition
* @param clazz The Class we are attempting to instantiate * @param clazz The Class we are attempting to instantiate
@ -37,7 +37,7 @@ public class InstantiationException extends HibernateException {
} }
/** /**
* Constructs a InstantiationException. * Constructs an {@code InstantiationException}.
* *
* @param message A message explaining the exception condition * @param message A message explaining the exception condition
* @param clazz The Class we are attempting to instantiate * @param clazz The Class we are attempting to instantiate
@ -49,7 +49,7 @@ public class InstantiationException extends HibernateException {
} }
/** /**
* Returns the Class we were attempting to instantiate. * Returns the {@link Class} we were attempting to instantiate.
* *
* @return The class we are unable to instantiate * @return The class we are unable to instantiate
*/ */
@ -59,7 +59,9 @@ public class InstantiationException extends HibernateException {
@Override @Override
public String getMessage() { public String getMessage() {
return super.getMessage() + " '" + clazz.getName() + "'"; final String message = super.getMessage() + " '" + clazz.getName() + "'";
final Throwable cause = getCause();
return cause != null ? message + " due to: " + cause.getMessage() : message;
} }
} }

View File

@ -6,7 +6,7 @@
*/ */
package org.hibernate; package org.hibernate;
import org.hibernate.internal.util.StringHelper; import static org.hibernate.internal.util.StringHelper.qualify;
/** /**
* A problem occurred accessing a property of an instance of a * A problem occurred accessing a property of an instance of a
@ -64,7 +64,7 @@ public class PropertyAccessException extends HibernateException {
@Override @Override
public String getMessage() { public String getMessage() {
return originalMessage() return originalMessage()
+ " : `" + StringHelper.qualify( persistentClass.getName(), propertyName ) + ": '" + qualify( persistentClass.getName(), propertyName ) + "'"
+ ( wasSetter ? "` (setter)" : "` (getter)" ); + ( wasSetter ? " (setter)" : " (getter)" );
} }
} }

View File

@ -48,7 +48,7 @@ public class NativeQueryConstructorTransformer<T> implements TupleTransformer<T>
} }
} }
catch (Exception e) { catch (Exception e) {
throw new InstantiationException( "Cannot instantiate query result type ", resultClass, e ); throw new InstantiationException( "Cannot instantiate query result type", resultClass, e );
} }
if ( constructor == null ) { if ( constructor == null ) {
throw new InstantiationException( "Result class must have a single constructor with exactly " throw new InstantiationException( "Result class must have a single constructor with exactly "

View File

@ -83,14 +83,14 @@ public class GetterMethodImplTest {
Getter runtimeException = getter( TargetThrowingExceptions.class, "runtimeException" ); Getter runtimeException = getter( TargetThrowingExceptions.class, "runtimeException" );
assertThatThrownBy( () -> runtimeException.get( target ) ) assertThatThrownBy( () -> runtimeException.get( target ) )
.isInstanceOf( PropertyAccessException.class ) .isInstanceOf( PropertyAccessException.class )
.hasMessage( "Exception occurred inside : `" + TargetThrowingExceptions.class.getName() +".runtimeException` (getter)" ) .hasMessage( "Exception occurred inside: '" + TargetThrowingExceptions.class.getName() +".runtimeException' (getter)" )
.getCause() // Not the root cause, the *direct* cause! We don't want extra wrapping. .getCause() // Not the root cause, the *direct* cause! We don't want extra wrapping.
.isExactlyInstanceOf( RuntimeException.class ); .isExactlyInstanceOf( RuntimeException.class );
Getter checkedException = getter( TargetThrowingExceptions.class, "checkedException" ); Getter checkedException = getter( TargetThrowingExceptions.class, "checkedException" );
assertThatThrownBy( () -> checkedException.get( target ) ) assertThatThrownBy( () -> checkedException.get( target ) )
.isInstanceOf( PropertyAccessException.class ) .isInstanceOf( PropertyAccessException.class )
.hasMessage( "Exception occurred inside : `" + TargetThrowingExceptions.class.getName() +".checkedException` (getter)" ) .hasMessage( "Exception occurred inside: '" + TargetThrowingExceptions.class.getName() +".checkedException' (getter)" )
.getCause() // Not the root cause, the *direct* cause! We don't want extra wrapping. .getCause() // Not the root cause, the *direct* cause! We don't want extra wrapping.
.isExactlyInstanceOf( Exception.class ); .isExactlyInstanceOf( Exception.class );

View File

@ -86,14 +86,14 @@ public class SetterMethodImplTest {
Setter runtimeException = setter( TargetThrowingExceptions.class, "runtimeException", String.class ); Setter runtimeException = setter( TargetThrowingExceptions.class, "runtimeException", String.class );
assertThatThrownBy( () -> runtimeException.set( target, "foo" ) ) assertThatThrownBy( () -> runtimeException.set( target, "foo" ) )
.isInstanceOf( PropertyAccessException.class ) .isInstanceOf( PropertyAccessException.class )
.hasMessage( "Exception occurred inside : `" + TargetThrowingExceptions.class.getName() +".runtimeException` (setter)" ) .hasMessage( "Exception occurred inside: '" + TargetThrowingExceptions.class.getName() +".runtimeException' (setter)" )
.getCause() // Not the root cause, the *direct* cause! We don't want extra wrapping. .getCause() // Not the root cause, the *direct* cause! We don't want extra wrapping.
.isExactlyInstanceOf( RuntimeException.class ); .isExactlyInstanceOf( RuntimeException.class );
Setter checkedException = setter( TargetThrowingExceptions.class, "checkedException", String.class ); Setter checkedException = setter( TargetThrowingExceptions.class, "checkedException", String.class );
assertThatThrownBy( () -> checkedException.set( target, "foo" ) ) assertThatThrownBy( () -> checkedException.set( target, "foo" ) )
.isInstanceOf( PropertyAccessException.class ) .isInstanceOf( PropertyAccessException.class )
.hasMessage( "Exception occurred inside : `" + TargetThrowingExceptions.class.getName() +".checkedException` (setter)" ) .hasMessage( "Exception occurred inside: '" + TargetThrowingExceptions.class.getName() +".checkedException' (setter)" )
.getCause() // Not the root cause, the *direct* cause! We don't want extra wrapping. .getCause() // Not the root cause, the *direct* cause! We don't want extra wrapping.
.isExactlyInstanceOf( Exception.class ); .isExactlyInstanceOf( Exception.class );