Skip to content

Added patch to throw exception in afterExecute and beforeExecute #41

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<groupId>com.flipkart.databuilderframework</groupId>
<version>0.5.8</version>
<version>0.5.8-PATCH-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>

<artifactId>databuilderframework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ default void postProcessing(DataFlowInstance dataFlowInstance,
default boolean shouldThrowException() {
return false;
}

default boolean shouldThrowExceptionInBeforeExecute() {
return false;
}

default boolean shouldThrowExceptionInAfterExecute() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static enum ErrorCode {
NO_BUILDER_FOUND_FOR_NAME,
INSTANTIATION_FAILURE,
BUILDER_RESOLUTION_CONFLICT_FOR_DATA,
BUILDER_EXECUTION_ERROR
BUILDER_EXECUTION_ERROR,
BUILDER_PRE_EXECUTION_ERROR,
BUILDER_POST_EXECUTION_ERROR
}

private final ErrorCode errorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.flipkart.databuilderframework.engine;

import com.flipkart.databuilderframework.engine.util.DataBuilderExceptionUtil;
import com.flipkart.databuilderframework.model.*;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -224,7 +225,7 @@ public DataContainer call() throws Exception {
try {
listener.beforeExecute(dataBuilderContext, dataFlowInstance, builderMeta, dataDelta, responseData);
} catch (Throwable t) {
logger.error("Error running pre-execution execution listener: ", t);
DataBuilderExceptionUtil.handleExceptionInBeforeExecute(listener, logger, t);
}
}
try {
Expand All @@ -236,7 +237,7 @@ public DataContainer call() throws Exception {
try {
listener.afterExecute(dataBuilderContext, dataFlowInstance, builderMeta, dataDelta, responseData, response);
} catch (Throwable t) {
logger.error("Error running post-execution listener: ", t);
DataBuilderExceptionUtil.handleExceptionInAfterExecute(listener, logger, t);
}
}
if(null != response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.flipkart.databuilderframework.engine;

import com.flipkart.databuilderframework.engine.util.DataBuilderExceptionUtil;
import com.flipkart.databuilderframework.model.*;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -243,7 +244,7 @@ public DataContainer call() throws Exception {
try {
listener.beforeExecute(dataBuilderContext, dataFlowInstance, builderMeta, dataDelta, responseData);
} catch (Throwable t) {
logger.error("Error running pre-execution execution listener: ", t);
DataBuilderExceptionUtil.handleExceptionInBeforeExecute(listener, logger, t);
}
}
try {
Expand All @@ -255,7 +256,7 @@ public DataContainer call() throws Exception {
try {
listener.afterExecute(dataBuilderContext, dataFlowInstance, builderMeta, dataDelta, responseData, response);
} catch (Throwable t) {
logger.error("Error running post-execution listener: ", t);
DataBuilderExceptionUtil.handleExceptionInAfterExecute(listener, logger, t);
}
}
if(null != response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.flipkart.databuilderframework.engine;

import com.flipkart.databuilderframework.engine.util.DataBuilderExceptionUtil;
import com.flipkart.databuilderframework.model.*;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -67,7 +68,7 @@ protected DataExecutionResponse run(DataBuilderContext dataBuilderContext,
try {
listener.beforeExecute(dataBuilderContext, dataFlowInstance, builderMeta, dataDelta, responseData);
} catch (Throwable t) {
logger.error("Error running pre-execution execution listener: ", t);
DataBuilderExceptionUtil.handleExceptionInBeforeExecute(listener, logger, t);
}
}
try {
Expand All @@ -86,13 +87,12 @@ protected DataExecutionResponse run(DataBuilderContext dataBuilderContext,
newlyGeneratedData.add(response.getData());
}
}
//logger.debug("Ran " + builderMeta.getName());
processedBuilders.add(builderMeta);
for (DataBuilderExecutionListener listener : dataBuilderExecutionListener) {
try {
listener.afterExecute(dataBuilderContext, dataFlowInstance, builderMeta, dataDelta, responseData, response);
} catch (Throwable t) {
logger.error("Error running post-execution listener: ", t);
DataBuilderExceptionUtil.handleExceptionInAfterExecute(listener, logger, t);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.flipkart.databuilderframework.engine.util;

import com.flipkart.databuilderframework.engine.DataBuilderExecutionListener;
import com.flipkart.databuilderframework.engine.DataBuilderFrameworkException;
import lombok.experimental.UtilityClass;
import org.slf4j.Logger;

@UtilityClass
public class DataBuilderExceptionUtil {

public static void handleExceptionInBeforeExecute(final DataBuilderExecutionListener listener,
final Logger logger,
final Throwable t) throws DataBuilderFrameworkException {
final String errorMessage = "Error running pre-execution execution listener: ";
logger.error(errorMessage, t);
if (listener.shouldThrowExceptionInBeforeExecute()) {
throw new DataBuilderFrameworkException(DataBuilderFrameworkException.ErrorCode.BUILDER_PRE_EXECUTION_ERROR,
errorMessage + t.getMessage(), t);
}
}

public static void handleExceptionInAfterExecute(final DataBuilderExecutionListener listener,
final Logger logger,
final Throwable t) throws DataBuilderFrameworkException {
final String errorMessage = "Error running post-execution execution listener: ";
logger.error(errorMessage, t);
if (listener.shouldThrowExceptionInAfterExecute()) {
throw new DataBuilderFrameworkException(DataBuilderFrameworkException.ErrorCode.BUILDER_POST_EXECUTION_ERROR,
errorMessage + t.getMessage(), t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,99 @@ public void postProcessing(DataFlowInstance dataFlowInstance,
}
}

private static class TestListenerBeforeExecutionErrorWithExceptionThrown implements DataBuilderExecutionListener {

@Override
public void preProcessing(DataFlowInstance dataFlowInstance,
DataDelta dataDelta) {
log.info("Being called for: " + dataFlowInstance.getId());
}

@Override
public void beforeExecute(DataBuilderContext builderContext,
DataFlowInstance dataFlowInstance,
DataBuilderMeta builderToBeApplied,
DataDelta dataDelta, Map<String, Data> prevResponses) throws Exception {
throw new Exception("Blah blah");
}

@Override
public void afterExecute(DataBuilderContext builderContext,
DataFlowInstance dataFlowInstance,
DataBuilderMeta builderToBeApplied,
DataDelta dataDelta, Map<String, Data> prevResponses, Data currentResponse) {
log.info("{} called for: {}", builderToBeApplied.getName(), dataFlowInstance.getId());
}

@Override
public void afterException(DataBuilderContext builderContext,
DataFlowInstance dataFlowInstance,
DataBuilderMeta builderToBeApplied,
DataDelta dataDelta,
Map<String, Data> prevResponses, Throwable frameworkException) {
log.info("{} called for: {}", builderToBeApplied.getName(), dataFlowInstance.getId());
}

@Override
public void postProcessing(DataFlowInstance dataFlowInstance,
DataDelta dataDelta, DataExecutionResponse response,
Throwable frameworkException) {
log.info("Being called for: {}", dataFlowInstance.getId());
}

@Override
public boolean shouldThrowExceptionInBeforeExecute() {
return true;
}
}

private static class TestListenerAfterExecutionErrorWithExceptionThrown implements DataBuilderExecutionListener {

@Override
public void preProcessing(DataFlowInstance dataFlowInstance,
DataDelta dataDelta) {
log.info("Being called for: {}", dataFlowInstance.getId());
}


@Override
public void beforeExecute(DataBuilderContext builderContext,
DataFlowInstance dataFlowInstance,
DataBuilderMeta builderToBeApplied,
DataDelta dataDelta, Map<String, Data> prevResponses) {
log.info("{} called for: {}", builderToBeApplied.getName(), dataFlowInstance.getId());
}

@Override
public void afterExecute(DataBuilderContext builderContext,
DataFlowInstance dataFlowInstance,
DataBuilderMeta builderToBeApplied,
DataDelta dataDelta, Map<String, Data> prevResponses, Data currentResponse) throws Exception {
throw new Exception("Blah blah");
}

@Override
public void afterException(DataBuilderContext builderContext,
DataFlowInstance dataFlowInstance,
DataBuilderMeta builderToBeApplied,
DataDelta dataDelta,
Map<String, Data> prevResponses, Throwable frameworkException) {
log.info("{} called for: {}", builderToBeApplied.getName(), dataFlowInstance.getId());
}

@Override
public void postProcessing(DataFlowInstance dataFlowInstance,
DataDelta dataDelta, DataExecutionResponse response,
Throwable frameworkException) {
log.info("Being called for: {}", dataFlowInstance.getId());
}

@Override
public boolean shouldThrowExceptionInAfterExecute() {
return true;
}
}

private DataBuilderMetadataManager dataBuilderMetadataManager = new DataBuilderMetadataManager();
private DataFlowExecutor executor = new MultiThreadedDataFlowExecutor(
new InstantiatingDataBuilderFactory(dataBuilderMetadataManager),
Expand All @@ -196,30 +289,22 @@ public void setup() throws Exception {
.withAnnotatedDataBuilder(TestBuilderError.class)
.withTargetData("Y")
.build();
executor.registerExecutionListener(new TestListener());
executor.registerExecutionListener(new TestListenerBeforeExecutionError());
executor.registerExecutionListener(new TestListenerAfterExecutionError());
executor.registerExecutionListener(new TestListenerAfterExceptionError());

dataFlowValidationError = new DataFlowBuilder()
.withAnnotatedDataBuilder(TestBuilderDataValidationError.class)
.withTargetData("Y")
.build();
executor.registerExecutionListener(new TestListener());
executor.registerExecutionListener(new TestListenerBeforeExecutionError());
executor.registerExecutionListener(new TestListenerAfterExecutionError());
executor.registerExecutionListener(new TestListenerAfterExceptionError());

dataFlowValidationErrorWithPartialData = new DataFlowBuilder()
.withAnnotatedDataBuilder(TestBuilderA.class)
.withAnnotatedDataBuilder(TestBuilderDataValidationError.class)
.withTargetData("Y")
.build();

executor.registerExecutionListener(new TestListener());
executor.registerExecutionListener(new TestListenerBeforeExecutionError());
executor.registerExecutionListener(new TestListenerAfterExecutionError());
executor.registerExecutionListener(new TestListenerAfterExceptionError());

}

@Test
Expand Down Expand Up @@ -351,4 +436,40 @@ public void testRunValidationErrorWithPartialData() throws Exception {
fail("Should have thrown exception");
}
}

@Test
public void testRunSingleStepWithExceptionThrownInBeforeExecuteInExecutionListener() throws Exception {
DataFlowInstance dataFlowInstance = new DataFlowInstance();
dataFlowInstance.setId("testflow");
dataFlowInstance.setDataFlow(dataFlow);
executor.registerExecutionListener(new TestListenerBeforeExecutionErrorWithExceptionThrown());

DataDelta dataDelta = new DataDelta(Lists.newArrayList(
new TestDataA("Hello"), new TestDataB("World"),
new TestDataD("this"), new TestDataG("Hmmm")));
try {
executor.run(dataFlowInstance, dataDelta);
fail("It should not come here.");
} catch (DataBuilderFrameworkException exception) {
Assert.assertEquals(DataBuilderFrameworkException.ErrorCode.BUILDER_EXECUTION_ERROR, exception.getErrorCode());
}
}

@Test
public void testRunSingleStepWithExceptionThrownInAfterExecuteInExecutionListener() throws Exception {
DataFlowInstance dataFlowInstance = new DataFlowInstance();
dataFlowInstance.setId("testflow");
dataFlowInstance.setDataFlow(dataFlow);
executor.registerExecutionListener(new TestListenerAfterExecutionErrorWithExceptionThrown());

DataDelta dataDelta = new DataDelta(Lists.newArrayList(
new TestDataA("Hello"), new TestDataB("World"),
new TestDataD("this"), new TestDataG("Hmmm")));
try {
executor.run(dataFlowInstance, dataDelta);
fail("It should not come here.");
} catch (DataBuilderFrameworkException exception) {
Assert.assertEquals(DataBuilderFrameworkException.ErrorCode.BUILDER_EXECUTION_ERROR, exception.getErrorCode());
}
}
}
Loading