Browse Source

Consistent factory methods for `QueryCreationException`.

Closes #3373
pull/3321/merge
Mark Paluch 3 months ago
parent
commit
1c579789ee
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 83
      src/main/java/org/springframework/data/repository/query/QueryCreationException.java

83
src/main/java/org/springframework/data/repository/query/QueryCreationException.java

@ -31,12 +31,15 @@ import org.springframework.data.repository.core.RepositoryCreationException; @@ -31,12 +31,15 @@ import org.springframework.data.repository.core.RepositoryCreationException;
public final class QueryCreationException extends RepositoryCreationException {
private static final @Serial long serialVersionUID = -1238456123580L;
private static final String MESSAGE_TEMPLATE = "Could not create query for method %s; Could not find property %s on domain class %s";
private static final String MESSAGE_TEMPLATE = "Could not create query for method [%s]; Could not find property '%s' on domain class '%s'";
private final Method method;
/**
* Creates a new {@link QueryCreationException}.
*
* @param message the detail message.
* @param method the query method causing the exception.
*/
private QueryCreationException(String message, QueryMethod method) {
@ -46,8 +49,23 @@ public final class QueryCreationException extends RepositoryCreationException { @@ -46,8 +49,23 @@ public final class QueryCreationException extends RepositoryCreationException {
/**
* Creates a new {@link QueryCreationException}.
*
* @param message the detail message.
* @param cause the root cause from the data access API in use.
* @param method the query method causing the exception.
* @since 4.0
*/
private QueryCreationException(String message, Throwable cause, Class<?> repositoryInterface, Method method) {
private QueryCreationException(String message, @Nullable Throwable cause, QueryMethod method) {
super(message, cause, method.getMetadata().getRepositoryInterface());
this.method = method.getMethod();
}
/**
* Creates a new {@link QueryCreationException}.
*/
private QueryCreationException(@Nullable String message, @Nullable Throwable cause, Class<?> repositoryInterface,
Method method) {
super(message, cause, repositoryInterface);
this.method = method;
@ -56,9 +74,9 @@ public final class QueryCreationException extends RepositoryCreationException { @@ -56,9 +74,9 @@ public final class QueryCreationException extends RepositoryCreationException {
/**
* Rejects the given domain class property.
*
* @param method
* @param propertyName
* @return
* @param method the {@link QueryMethod} to create the exception for.
* @param propertyName the property name that could not be found.
* @return the {@link QueryCreationException}.
*/
public static QueryCreationException invalidProperty(QueryMethod method, String propertyName) {
@ -69,48 +87,67 @@ public final class QueryCreationException extends RepositoryCreationException { @@ -69,48 +87,67 @@ public final class QueryCreationException extends RepositoryCreationException {
/**
* Creates a new {@link QueryCreationException}.
*
* @param method
* @param message
* @return
* @param method the {@link QueryMethod} to create the exception for.
* @param message the message to use.
* @return the {@link QueryCreationException}.
*/
public static QueryCreationException create(QueryMethod method, String message) {
return new QueryCreationException(String.format("Could not create query for %s; Reason: %s", method, message),
method);
return new QueryCreationException(createMessage(message, method.getMethod()), method);
}
/**
* Creates a new {@link QueryCreationException} for the given {@link QueryMethod} and {@link Throwable} as cause.
*
* @param method
* @param cause
* @return
* @param method the query method causing the exception.
* @param cause the root cause from the data access API in use.
* @return the {@link QueryCreationException}.
*/
@SuppressWarnings("NullAway")
public static QueryCreationException create(QueryMethod method, Throwable cause) {
return new QueryCreationException(cause.getMessage(), cause, method.getMetadata().getRepositoryInterface(),
method.getMethod());
return new QueryCreationException(cause.getMessage(), cause, method);
}
/**
* Creates a new {@link QueryCreationException} for the given {@link QueryMethod}, {@code message} and
* {@link Throwable} as cause.
*
* @param method the query method causing the exception.
* @param message the detail message.
* @param cause the root cause from the data access API in use.
* @return the {@link QueryCreationException}.
* @since 4.0
*/
@SuppressWarnings("NullAway")
public static QueryCreationException create(QueryMethod method, String message, Throwable cause) {
return create(message, cause, method.getMetadata().getRepositoryInterface(), method.getMethod());
}
/**
* Creates a new {@link QueryCreationException} for the given {@link QueryMethod} and {@link Throwable} as cause.
*
* @param method
* @param cause
* @return
* @param message the detail message.
* @param cause the root cause from the data access API in use.
* @param repositoryInterface the repository interface.
* @param method the query method causing the exception.
* @return the {@link QueryCreationException}.
* @since 2.5
*/
public static QueryCreationException create(@Nullable String message, Throwable cause, Class<?> repositoryInterface,
Method method) {
return new QueryCreationException(String.format("Could not create query for %s; Reason: %s", method, message),
public static QueryCreationException create(@Nullable String message, @Nullable Throwable cause,
Class<?> repositoryInterface, Method method) {
return new QueryCreationException(createMessage(message, method),
cause, repositoryInterface, method);
}
private static String createMessage(@Nullable String message, Method method) {
return String.format("Could not create query for [%s]; Reason: %s", method, message);
}
/**
* @return
* @return the method causing the exception.
* @since 2.5
*/
public Method getMethod() {
return method;
}
}

Loading…
Cancel
Save