Skip to content

Provide better error message when executing a DbAction fails #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>1.0.0.DATAJDBC-150-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,26 @@ public static <T> DeleteAll<T> deleteAll(Class<T> type, JdbcPropertyPath propert
return new DeleteAll<>(type, propertyPath, dependingOn);
}

abstract void executeWith(Interpreter interpreter);
/**
* Executing this DbAction with the given {@link Interpreter}.
*
* @param interpreter the {@link Interpreter} responsible for actually executing the {@link DbAction}.
*/
void executeWith(Interpreter interpreter) {

try {
doExecuteWith(interpreter);
} catch (Exception e) {
throw new DbActionExecutionException(this, e);
}
}

/**
* Executing this DbAction with the given {@link Interpreter} without any exception handling.
*
* @param interpreter the {@link Interpreter} responsible for actually executing the {@link DbAction}.
*/
protected abstract void doExecuteWith(Interpreter interpreter);

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

@Override
void executeWith(Interpreter interpreter) {
protected void doExecuteWith(Interpreter interpreter) {
interpreter.interpret(this);
}
}
Expand All @@ -130,7 +149,7 @@ private Update(T entity, JdbcPropertyPath propertyPath, DbAction dependingOn) {
}

@Override
void executeWith(Interpreter interpreter) {
protected void doExecuteWith(Interpreter interpreter) {
interpreter.interpret(this);
}
}
Expand Down Expand Up @@ -159,7 +178,7 @@ private Delete(Object rootId, Class<T> type, T entity, JdbcPropertyPath property
}

@Override
void executeWith(Interpreter interpreter) {
protected void doExecuteWith(Interpreter interpreter) {
interpreter.interpret(this);
}
}
Expand All @@ -176,7 +195,7 @@ private DeleteAll(Class<T> entityType, JdbcPropertyPath propertyPath, DbAction d
}

@Override
void executeWith(Interpreter interpreter) {
protected void doExecuteWith(Interpreter interpreter) {
interpreter.interpret(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.conversion;

/**
* Exception thrown when during the execution of a {@link DbAction} an exception gets thrown. Provides additional
* context information about the action and the entity.
*
* @author Jens Schauder
*/
public class DbActionExecutionException extends RuntimeException {
public DbActionExecutionException(DbAction<?> action, Throwable cause) {
super( //
String.format("Failed to execute %s for instance %s of type %s with path %s", //
action.getClass().getSimpleName(), //
action.getEntity(), //
action.getEntityType(), //
action.getPropertyPath().toDotPath() //
), //
cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.conversion;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

import org.junit.Test;

/**
* Unit tests for {@link DbAction}s
*
* @author Jens Schauder
*/
public class DbActionUnitTests {

@Test // DATAJDBC-150
public void exceptionFromActionContainsUsefulInformationWhenInterpreterFails() {

DummyEntity entity = new DummyEntity();
DbAction.Insert<DummyEntity> insert = DbAction.insert(entity, JdbcPropertyPath.from("someName", DummyEntity.class),
null);

Interpreter failingInterpreter = mock(Interpreter.class);
doThrow(new RuntimeException()).when(failingInterpreter).interpret(any(DbAction.Insert.class));

assertThatExceptionOfType(DbActionExecutionException.class) //
.isThrownBy(() -> insert.executeWith(failingInterpreter)) //
.withMessageContaining("Insert") //
.withMessageContaining(entity.toString());

}

static class DummyEntity {
String someName;
}
}