Skip to content

Commit 1c57978

Browse files
committed
Consistent factory methods for QueryCreationException.
Closes #3373
1 parent 000ccd5 commit 1c57978

File tree

1 file changed

+60
-23
lines changed

1 file changed

+60
-23
lines changed

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

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
public final class QueryCreationException extends RepositoryCreationException {
3232

3333
private static final @Serial long serialVersionUID = -1238456123580L;
34-
private static final String MESSAGE_TEMPLATE = "Could not create query for method %s; Could not find property %s on domain class %s";
34+
private static final String MESSAGE_TEMPLATE = "Could not create query for method [%s]; Could not find property '%s' on domain class '%s'";
3535

3636
private final Method method;
3737

3838
/**
3939
* Creates a new {@link QueryCreationException}.
40+
*
41+
* @param message the detail message.
42+
* @param method the query method causing the exception.
4043
*/
4144
private QueryCreationException(String message, QueryMethod method) {
4245

@@ -46,8 +49,23 @@ private QueryCreationException(String message, QueryMethod method) {
4649

4750
/**
4851
* Creates a new {@link QueryCreationException}.
52+
*
53+
* @param message the detail message.
54+
* @param cause the root cause from the data access API in use.
55+
* @param method the query method causing the exception.
56+
* @since 4.0
4957
*/
50-
private QueryCreationException(String message, Throwable cause, Class<?> repositoryInterface, Method method) {
58+
private QueryCreationException(String message, @Nullable Throwable cause, QueryMethod method) {
59+
60+
super(message, cause, method.getMetadata().getRepositoryInterface());
61+
this.method = method.getMethod();
62+
}
63+
64+
/**
65+
* Creates a new {@link QueryCreationException}.
66+
*/
67+
private QueryCreationException(@Nullable String message, @Nullable Throwable cause, Class<?> repositoryInterface,
68+
Method method) {
5169

5270
super(message, cause, repositoryInterface);
5371
this.method = method;
@@ -56,9 +74,9 @@ private QueryCreationException(String message, Throwable cause, Class<?> reposit
5674
/**
5775
* Rejects the given domain class property.
5876
*
59-
* @param method
60-
* @param propertyName
61-
* @return
77+
* @param method the {@link QueryMethod} to create the exception for.
78+
* @param propertyName the property name that could not be found.
79+
* @return the {@link QueryCreationException}.
6280
*/
6381
public static QueryCreationException invalidProperty(QueryMethod method, String propertyName) {
6482

@@ -69,48 +87,67 @@ public static QueryCreationException invalidProperty(QueryMethod method, String
6987
/**
7088
* Creates a new {@link QueryCreationException}.
7189
*
72-
* @param method
73-
* @param message
74-
* @return
90+
* @param method the {@link QueryMethod} to create the exception for.
91+
* @param message the message to use.
92+
* @return the {@link QueryCreationException}.
7593
*/
7694
public static QueryCreationException create(QueryMethod method, String message) {
77-
78-
return new QueryCreationException(String.format("Could not create query for %s; Reason: %s", method, message),
79-
method);
95+
return new QueryCreationException(createMessage(message, method.getMethod()), method);
8096
}
8197

8298
/**
8399
* Creates a new {@link QueryCreationException} for the given {@link QueryMethod} and {@link Throwable} as cause.
84100
*
85-
* @param method
86-
* @param cause
87-
* @return
101+
* @param method the query method causing the exception.
102+
* @param cause the root cause from the data access API in use.
103+
* @return the {@link QueryCreationException}.
88104
*/
89105
@SuppressWarnings("NullAway")
90106
public static QueryCreationException create(QueryMethod method, Throwable cause) {
91-
return new QueryCreationException(cause.getMessage(), cause, method.getMetadata().getRepositoryInterface(),
92-
method.getMethod());
107+
return new QueryCreationException(cause.getMessage(), cause, method);
108+
}
109+
110+
/**
111+
* Creates a new {@link QueryCreationException} for the given {@link QueryMethod}, {@code message} and
112+
* {@link Throwable} as cause.
113+
*
114+
* @param method the query method causing the exception.
115+
* @param message the detail message.
116+
* @param cause the root cause from the data access API in use.
117+
* @return the {@link QueryCreationException}.
118+
* @since 4.0
119+
*/
120+
@SuppressWarnings("NullAway")
121+
public static QueryCreationException create(QueryMethod method, String message, Throwable cause) {
122+
return create(message, cause, method.getMetadata().getRepositoryInterface(), method.getMethod());
93123
}
94124

95125
/**
96126
* Creates a new {@link QueryCreationException} for the given {@link QueryMethod} and {@link Throwable} as cause.
97127
*
98-
* @param method
99-
* @param cause
100-
* @return
128+
* @param message the detail message.
129+
* @param cause the root cause from the data access API in use.
130+
* @param repositoryInterface the repository interface.
131+
* @param method the query method causing the exception.
132+
* @return the {@link QueryCreationException}.
101133
* @since 2.5
102134
*/
103-
public static QueryCreationException create(@Nullable String message, Throwable cause, Class<?> repositoryInterface,
104-
Method method) {
105-
return new QueryCreationException(String.format("Could not create query for %s; Reason: %s", method, message),
135+
public static QueryCreationException create(@Nullable String message, @Nullable Throwable cause,
136+
Class<?> repositoryInterface, Method method) {
137+
return new QueryCreationException(createMessage(message, method),
106138
cause, repositoryInterface, method);
107139
}
108140

141+
private static String createMessage(@Nullable String message, Method method) {
142+
return String.format("Could not create query for [%s]; Reason: %s", method, message);
143+
}
144+
109145
/**
110-
* @return
146+
* @return the method causing the exception.
111147
* @since 2.5
112148
*/
113149
public Method getMethod() {
114150
return method;
115151
}
152+
116153
}

0 commit comments

Comments
 (0)