Skip to content

Commit

Permalink
issue/#660 Added log with error detail when execution failed and also…
Browse files Browse the repository at this point in the history
… rollback failed (non transactional execution). (#670)
  • Loading branch information
osantana85 committed Apr 22, 2024
1 parent dd9cc34 commit 59f34da
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.mongock.api.exception;

/**
*
*/
public class MongockRollbackException extends MongockException {

private MongockException executionException;
private MongockException rollbackException;

public MongockRollbackException(MongockException executionException, MongockException rollbackException) {
super();
this.executionException = executionException;
this.rollbackException = rollbackException;
}

public MongockException getExecutionException() {
return executionException;
}

public MongockException getRollbackException() {
return rollbackException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.mongock.api.config.TransactionStrategy;
import io.mongock.api.config.executor.ChangeExecutorConfiguration;
import io.mongock.api.exception.MongockException;
import io.mongock.api.exception.MongockRollbackException;
import io.mongock.driver.api.common.SystemChange;
import io.mongock.driver.api.driver.ConnectionDriver;
import io.mongock.driver.api.entry.ChangeEntry;
Expand Down Expand Up @@ -125,10 +126,17 @@ protected void processSingleChangeLog(String executionId, String executionHostna
changeLog.getType().getAnnotation(SystemChange.class).updatesSystemTable()) {
loadExecutedChangeEntries();
}
} catch (Exception e) {
} catch (Exception executionEx) {
if (changeLog.isFailFast()) {
rollbackProcessedChangeSetsIfApply(executionId, executionHostname, changeSetsToRollBack);
throw e;
try {
rollbackProcessedChangeSetsIfApply(executionId, executionHostname, changeSetsToRollBack);
}
catch (Exception rollbackEx) {
MongockException mongockExecutionEx = MongockException.class.isAssignableFrom(executionEx.getClass()) ? (MongockException) executionEx : new MongockException(executionEx);
MongockException mongockRollbackEx = MongockException.class.isAssignableFrom(rollbackEx.getClass()) ? (MongockException) rollbackEx : new MongockException(rollbackEx);
throw new MongockRollbackException(mongockExecutionEx, mongockRollbackEx);
}
throw executionEx;
}
} finally {
clearChangeSetsToRollbackIfApply(isStrategyPerChangeUnit());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.mongock.runner.core.executor;

import io.mongock.api.exception.MongockException;
import io.mongock.api.exception.MongockRollbackException;
import io.mongock.driver.api.lock.LockCheckException;
import io.mongock.runner.core.event.EventPublisher;
import io.mongock.runner.core.event.result.MigrationSuccessResult;
Expand Down Expand Up @@ -68,11 +69,20 @@ public void execute() throws MongockException {
}

} catch (Exception ex) {
MongockException exWrapper = MongockException.class.isAssignableFrom(ex.getClass()) ? (MongockException) ex : new MongockException(ex);
logger.error("Error in mongock process. ABORTED OPERATION", exWrapper);
eventPublisher.publishMigrationFailedEvent(exWrapper);
throw exWrapper;

if (MongockRollbackException.class.isAssignableFrom(ex.getClass())) {
MongockRollbackException mongockRollbackException = (MongockRollbackException)ex;
logger.error("Error in mongock process. ABORTED OPERATION");
logger.error("EXECUTION error detail:", mongockRollbackException.getExecutionException());
logger.error("ROLLBACK error detail:", mongockRollbackException.getRollbackException());
eventPublisher.publishMigrationFailedEvent(mongockRollbackException.getRollbackException());
throw mongockRollbackException.getRollbackException();
}
else {
MongockException exWrapper = MongockException.class.isAssignableFrom(ex.getClass()) ? (MongockException) ex : new MongockException(ex);
logger.error("Error in mongock process. ABORTED OPERATION", exWrapper);
eventPublisher.publishMigrationFailedEvent(exWrapper);
throw exWrapper;
}
}
}
}
Expand Down

0 comments on commit 59f34da

Please sign in to comment.