Skip to content

Commit

Permalink
addUniqueError method to add kraftwerk error
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisszmundy committed Aug 21, 2024
1 parent 0272158 commit d0ebcf0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public void applyUnimodalSequence(UserInputs userInputs, String dataMode, VtlBin
Variable variable = variablesMap.getVariable(variableName);
if (variable.getSasFormat() != null && variable.getExpectedLength()<variable.getMaxLengthData() && variable.getType() != VariableType.BOOLEAN){
log.warn(String.format("%s expected length is %s but max length received is %d",variable.getName(),variable.getExpectedLength(), variable.getMaxLengthData()));
ErrorVariableLength error = new ErrorVariableLength(variable, dataMode);
if (!kraftwerkExecutionContext.getErrors().contains(error)){
kraftwerkExecutionContext.getErrors().add(error);
}
kraftwerkExecutionContext.addUniqueError(new ErrorVariableLength(variable, dataMode));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public String getFormattedString() {
}

return toWrite.toString();
}

public void addUniqueError(KraftwerkError kraftwerkError){
if (!errors.contains(kraftwerkError)){
errors.add(kraftwerkError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,28 @@ public void evalVtlScript(String vtlScript, VtlBindings bindings, KraftwerkExecu

} catch (ScriptException e) {
log.warn("ScriptException - VTL instruction given is invalid and has been skipped : {}",vtlScript);
addError(vtlScript, kraftwerkExecutionContext, e);
kraftwerkExecutionContext.addUniqueError(new ErrorVtlTransformation(vtlScript, e.getMessage()));
} catch (NumberFormatException e) {
log.warn("NumberFormatException - Corresponding variable could not be calculated.");
addError(vtlScript, kraftwerkExecutionContext, e);
kraftwerkExecutionContext.addUniqueError(new ErrorVtlTransformation(vtlScript, e.getMessage()));
} catch (UnsupportedOperationException e) {
log.warn("UnsupportedOperationException - Corresponding variable could not be calculated.");
addError(vtlScript, kraftwerkExecutionContext, e);
kraftwerkExecutionContext.addUniqueError(new ErrorVtlTransformation(vtlScript, e.getMessage()));
} catch (NullPointerException e) {
log.debug("NullPointerException - Probable cause: one of the operator used not yet supported by Trevas java library.");
addError(vtlScript, kraftwerkExecutionContext, e);
kraftwerkExecutionContext.addUniqueError(new ErrorVtlTransformation(vtlScript, e.getMessage()));
} catch (Error e) {
log.debug("Error - Probable cause: Syntax error.");
addError(vtlScript, kraftwerkExecutionContext, e);
kraftwerkExecutionContext.addUniqueError(new ErrorVtlTransformation(vtlScript, e.getMessage()));
} catch (Exception e) {
log.warn("Exception - UNKNOWN EXCEPTION PLEASE REPORT IT!");
addError(vtlScript, kraftwerkExecutionContext, e);
kraftwerkExecutionContext.addUniqueError(new ErrorVtlTransformation(vtlScript, e.getMessage()));
}
} else {
log.info("null or empty VTL instruction given. VTL bindings has not been changed.");
}
}

private static void addError(String vtlScript, KraftwerkExecutionContext kraftwerkExecutionContext, Throwable e) {
ErrorVtlTransformation error = new ErrorVtlTransformation(vtlScript, e.getMessage());
if (!kraftwerkExecutionContext.getErrors().contains(error)){
kraftwerkExecutionContext.getErrors().add(error);
}
}

/**
* Evaluate the given VTL instructions and update the bindings.
* The name of the input datasets in the script must refer to the names given in the bindings.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package fr.insee.kraftwerk.core.utils.log;

import fr.insee.bpm.metadata.model.Group;
import fr.insee.bpm.metadata.model.Variable;
import fr.insee.bpm.metadata.model.VariableType;
import fr.insee.kraftwerk.core.KraftwerkError;
import fr.insee.kraftwerk.core.metadata.ErrorVariableLength;
import fr.insee.kraftwerk.core.vtl.ErrorVtlTransformation;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -32,4 +38,46 @@ void getFormattedString_test(){
//THEN
Assertions.assertThat(formattedString).contains("TEST.xml", "RACINE", simpleDateFormat.format(start), simpleDateFormat.format(stop));
}

@Test
void addUniqueError_test(){
//Given
KraftwerkExecutionContext kraftwerkExecutionContext = new KraftwerkExecutionContext();
KraftwerkError kraftwerkError = new ErrorVtlTransformation("test","test");

//When
kraftwerkExecutionContext.addUniqueError(kraftwerkError);

//Then
Assertions.assertThat(kraftwerkExecutionContext.getErrors()).isNotEmpty().containsExactly(kraftwerkError);
}

@Test
void addUniqueError_2xSame_test(){
//Given
KraftwerkExecutionContext kraftwerkExecutionContext = new KraftwerkExecutionContext();
KraftwerkError kraftwerkError = new ErrorVtlTransformation("test","test");

//When
kraftwerkExecutionContext.addUniqueError(kraftwerkError);
kraftwerkExecutionContext.addUniqueError(kraftwerkError);

//Then
Assertions.assertThat(kraftwerkExecutionContext.getErrors()).isNotEmpty().hasSize(1).containsExactly(kraftwerkError);
}

@Test
void addUniqueError_2xDifferent_test(){
//Given
KraftwerkExecutionContext kraftwerkExecutionContext = new KraftwerkExecutionContext();
KraftwerkError kraftwerkError = new ErrorVtlTransformation("test","test");
KraftwerkError kraftwerkError2 = new ErrorVariableLength(new Variable("TESTVAR",new Group("TESTGROUP"), VariableType.STRING),"WEB");

//When
kraftwerkExecutionContext.addUniqueError(kraftwerkError);
kraftwerkExecutionContext.addUniqueError(kraftwerkError2);

//Then
Assertions.assertThat(kraftwerkExecutionContext.getErrors()).isNotEmpty().hasSize(2).containsExactly(kraftwerkError, kraftwerkError2);
}
}

0 comments on commit d0ebcf0

Please sign in to comment.