different exception propagation.

This commit is contained in:
2023-03-15 17:54:29 -05:00
parent 9cbb899788
commit 248db43c8f
5 changed files with 145 additions and 83 deletions

View File

@ -26,6 +26,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import com.kingsrook.qqq.backend.core.actions.ActionHelper;
import com.kingsrook.qqq.backend.core.actions.scripts.logging.QCodeExecutionLoggerInterface;
import com.kingsrook.qqq.backend.core.actions.scripts.logging.ScriptExecutionLoggerInterface;
@ -72,6 +73,8 @@ public class RunAdHocRecordScriptAction
**
*******************************************************************************/
public void run(RunAdHocRecordScriptInput input, RunAdHocRecordScriptOutput output) throws QException
{
try
{
ActionHelper.validateSession(input);
@ -147,6 +150,11 @@ public class RunAdHocRecordScriptAction
output.setOutput(executeCodeOutput.getOutput());
output.setLogger(executionLogger);
}
catch(Exception e)
{
output.setException(Optional.of(e));
}
}

View File

@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.core.model.actions.scripts;
import java.io.Serializable;
import java.util.Optional;
import com.kingsrook.qqq.backend.core.actions.scripts.logging.QCodeExecutionLoggerInterface;
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionOutput;
@ -33,8 +34,8 @@ import com.kingsrook.qqq.backend.core.model.actions.AbstractActionOutput;
public class RunAdHocRecordScriptOutput extends AbstractActionOutput
{
private Serializable output;
private QCodeExecutionLoggerInterface logger;
private Optional<Exception> exception = Optional.empty();
@ -101,4 +102,35 @@ public class RunAdHocRecordScriptOutput extends AbstractActionOutput
return (this);
}
/*******************************************************************************
** Getter for exception
*******************************************************************************/
public Optional<Exception> getException()
{
return (this.exception);
}
/*******************************************************************************
** Setter for exception
*******************************************************************************/
public void setException(Optional<Exception> exception)
{
this.exception = exception;
}
/*******************************************************************************
** Fluent setter for exception
*******************************************************************************/
public RunAdHocRecordScriptOutput withException(Optional<Exception> exception)
{
this.exception = exception;
return (this);
}
}

View File

@ -65,6 +65,8 @@ public class RunRecordScriptLoadStep extends AbstractLoadStep implements Process
.withSingularPastMessage("had the script ran against it.")
.withPluralPastMessage("had the script ran against them.");
private ProcessSummaryLine unloggedExceptionLine = new ProcessSummaryLine(Status.ERROR, null, "had an error that was not logged.");
private List<Serializable> okScriptLogIds = new ArrayList<>();
private List<Serializable> errorScriptLogIds = new ArrayList<>();
@ -115,18 +117,24 @@ public class RunRecordScriptLoadStep extends AbstractLoadStep implements Process
Integer scriptId = runBackendStepInput.getValueInteger("scriptId");
StoreScriptLogAndScriptLogLineExecutionLogger scriptLogger = new StoreScriptLogAndScriptLogLineExecutionLogger(null, null); // downstream these will get set!
try
{
RunAdHocRecordScriptInput input = new RunAdHocRecordScriptInput();
input.setRecordList(runBackendStepInput.getRecords());
input.setCodeReference(new AdHocScriptCodeReference().withScriptId(scriptId));
input.setLogger(scriptLogger);
RunAdHocRecordScriptOutput output = new RunAdHocRecordScriptOutput();
Exception caughtException = null;
try
{
new RunAdHocRecordScriptAction().run(input, output);
if(output.getException().isPresent())
{
caughtException = output.getException().get();
}
}
catch(Exception e)
{
LOG.info("Exception running record script", e, logPair("scriptId", scriptId));
caughtException = e;
}
if(scriptLogger.getScriptLog() != null)
@ -138,6 +146,10 @@ public class RunRecordScriptLoadStep extends AbstractLoadStep implements Process
(hadError ? errorScriptLogIds : okScriptLogIds).add(id);
}
}
else if(caughtException != null)
{
unloggedExceptionLine.incrementCount(runBackendStepInput.getRecords().size());
}
}
@ -163,6 +175,8 @@ public class RunRecordScriptLoadStep extends AbstractLoadStep implements Process
.withLinkText("Created " + String.format("%,d", errorScriptLogIds.size()) + " Script Log" + StringUtils.plural(errorScriptLogIds) + " with Errors"));
}
unloggedExceptionLine.addSelfToListIfAnyCount(summary);
return (summary);
}
}

View File

@ -46,7 +46,7 @@ import com.kingsrook.qqq.backend.core.model.scripts.ScriptsMetaDataProvider;
import com.kingsrook.qqq.backend.core.utils.TestUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThat;
/*******************************************************************************
@ -72,9 +72,10 @@ class RunAdHocRecordScriptActionTest extends BaseTest
RunAdHocRecordScriptOutput runAdHocRecordScriptOutput = new RunAdHocRecordScriptOutput();
assertThatThrownBy(() -> new RunAdHocRecordScriptAction().run(runAdHocRecordScriptInput, runAdHocRecordScriptOutput))
new RunAdHocRecordScriptAction().run(runAdHocRecordScriptInput, runAdHocRecordScriptOutput);
assertThat(runAdHocRecordScriptOutput.getException()).isPresent().get()
.isInstanceOf(QException.class)
.hasMessageContaining("Script revision was not found");
.hasFieldOrPropertyWithValue("message", "Script revision was not found.");
}

View File

@ -22,17 +22,22 @@
package com.kingsrook.qqq.backend.core.processes.implementations.scripts;
import java.util.List;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.actions.processes.RunProcessAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.processes.ProcessSummaryLine;
import com.kingsrook.qqq.backend.core.model.actions.processes.ProcessSummaryLineInterface;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunProcessInput;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunProcessOutput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.scripts.ScriptsMetaDataProvider;
import com.kingsrook.qqq.backend.core.utils.TestUtils;
import com.kingsrook.qqq.backend.core.utils.collections.MapBuilder;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertTrue;
/*******************************************************************************
@ -50,6 +55,8 @@ class RunRecordScriptTest extends BaseTest
QInstance qInstance = QContext.getQInstance();
new ScriptsMetaDataProvider().defineAll(qInstance, TestUtils.MEMORY_BACKEND_NAME, null);
TestUtils.insertRecords(qInstance, qInstance.getTable(TestUtils.TABLE_NAME_PERSON_MEMORY), List.of(new QRecord().withValue("id", 1)));
RunProcessInput runProcessInput = new RunProcessInput();
runProcessInput.setProcessName(ScriptsMetaDataProvider.RUN_RECORD_SCRIPT_PROCESS_NAME);
runProcessInput.setFrontendStepBehavior(RunProcessInput.FrontendStepBehavior.SKIP);
@ -63,9 +70,9 @@ class RunRecordScriptTest extends BaseTest
// so, turns out, we don't know how to do a join yet in memory backend, so this can't quite work //
// still good to run the code and at least get this far w/ an expected exception. //
///////////////////////////////////////////////////////////////////////////////////////////////////
assertThatThrownBy(() -> new RunProcessAction().execute(runProcessInput))
.isInstanceOf(QException.class)
.hasMessageContaining("Script revision was not found");
RunProcessOutput runProcessOutput = new RunProcessAction().execute(runProcessInput);
System.out.println(runProcessOutput);
assertTrue(((List<ProcessSummaryLineInterface>) runProcessOutput.getValues().get("processResults")).stream().anyMatch(psli -> psli instanceof ProcessSummaryLine psl && psl.getMessage().contains("error that was not logged")));
}
}