Skip to content

Commit 0b2708f

Browse files
schaudergregturn
authored andcommitted
DATAJDBC-150 - Provides better error message when execution of a DbAction fails.
1 parent 0ef1a63 commit 0b2708f

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

src/main/java/org/springframework/data/jdbc/core/conversion/DbAction.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,26 @@ public static <T> DeleteAll<T> deleteAll(Class<T> type, JdbcPropertyPath propert
8686
return new DeleteAll<>(type, propertyPath, dependingOn);
8787
}
8888

89-
abstract void executeWith(Interpreter interpreter);
89+
/**
90+
* Executing this DbAction with the given {@link Interpreter}.
91+
*
92+
* @param interpreter the {@link Interpreter} responsible for actually executing the {@link DbAction}.
93+
*/
94+
void executeWith(Interpreter interpreter) {
95+
96+
try {
97+
doExecuteWith(interpreter);
98+
} catch (Exception e) {
99+
throw new DbActionExecutionException(this, e);
100+
}
101+
}
102+
103+
/**
104+
* Executing this DbAction with the given {@link Interpreter} without any exception handling.
105+
*
106+
* @param interpreter the {@link Interpreter} responsible for actually executing the {@link DbAction}.
107+
*/
108+
protected abstract void doExecuteWith(Interpreter interpreter);
90109

91110
/**
92111
* {@link InsertOrUpdate} must reference an entity.
@@ -113,7 +132,7 @@ private Insert(T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
113132
}
114133

115134
@Override
116-
void executeWith(Interpreter interpreter) {
135+
protected void doExecuteWith(Interpreter interpreter) {
117136
interpreter.interpret(this);
118137
}
119138
}
@@ -130,7 +149,7 @@ private Update(T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
130149
}
131150

132151
@Override
133-
void executeWith(Interpreter interpreter) {
152+
protected void doExecuteWith(Interpreter interpreter) {
134153
interpreter.interpret(this);
135154
}
136155
}
@@ -159,7 +178,7 @@ private Delete(Object rootId, Class<T> type, T entity, JdbcPropertyPath property
159178
}
160179

161180
@Override
162-
void executeWith(Interpreter interpreter) {
181+
protected void doExecuteWith(Interpreter interpreter) {
163182
interpreter.interpret(this);
164183
}
165184
}
@@ -176,7 +195,7 @@ private DeleteAll(Class<T> entityType, JdbcPropertyPath propertyPath, DbAction d
176195
}
177196

178197
@Override
179-
void executeWith(Interpreter interpreter) {
198+
protected void doExecuteWith(Interpreter interpreter) {
180199
interpreter.interpret(this);
181200
}
182201
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.core.conversion;
17+
18+
/**
19+
* Exception thrown when during the execution of a {@link DbAction} an exception gets thrown. Provides additional
20+
* context information about the action and the entity.
21+
*
22+
* @author Jens Schauder
23+
*/
24+
public class DbActionExecutionException extends RuntimeException {
25+
public DbActionExecutionException(DbAction<?> action, Throwable cause) {
26+
super( //
27+
String.format("Failed to execute %s for instance %s of type %s with path %s", //
28+
action.getClass().getSimpleName(), //
29+
action.getEntity(), //
30+
action.getEntityType(), //
31+
action.getPropertyPath().toDotPath() //
32+
), //
33+
cause);
34+
}
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.core.conversion;
17+
18+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.Mockito.doThrow;
21+
import static org.mockito.Mockito.mock;
22+
23+
import org.junit.Test;
24+
25+
/**
26+
* Unit tests for {@link DbAction}s
27+
*
28+
* @author Jens Schauder
29+
*/
30+
public class DbActionUnitTests {
31+
32+
@Test // DATAJDBC-150
33+
public void exceptionFromActionContainsUsefulInformationWhenInterpreterFails() {
34+
35+
DummyEntity entity = new DummyEntity();
36+
DbAction.Insert<DummyEntity> insert = DbAction.insert(entity, JdbcPropertyPath.from("someName", DummyEntity.class),
37+
null);
38+
39+
Interpreter failingInterpreter = mock(Interpreter.class);
40+
doThrow(new RuntimeException()).when(failingInterpreter).interpret(any(DbAction.Insert.class));
41+
42+
assertThatExceptionOfType(DbActionExecutionException.class) //
43+
.isThrownBy(() -> insert.executeWith(failingInterpreter)) //
44+
.withMessageContaining("Insert") //
45+
.withMessageContaining(entity.toString());
46+
47+
}
48+
49+
static class DummyEntity {
50+
String someName;
51+
}
52+
}

0 commit comments

Comments
 (0)