Skip to content

Commit

Permalink
fix(core): better handling of invalid task type (#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu authored Feb 15, 2023
1 parent 2e0f907 commit d895ec3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
34 changes: 29 additions & 5 deletions core/src/main/java/io/kestra/core/serializers/YamlFlowParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.kestra.core.models.flows.Flow;
import io.kestra.core.models.templates.Template;
import io.kestra.core.models.validations.ManualConstraintViolation;
import io.kestra.core.serializers.helpers.HandleBarDeserializer;
import jakarta.inject.Singleton;
Expand All @@ -18,6 +17,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;
import javax.validation.ConstraintViolationException;

@Singleton
Expand Down Expand Up @@ -77,12 +77,36 @@ private <T> T readFlow(ObjectMapper mapper, String input, Class<T> objectClass,
} catch (JsonProcessingException e) {
if (e.getCause() instanceof ConstraintViolationException) {
throw (ConstraintViolationException) e.getCause();
} else {
}
else if (e instanceof InvalidTypeIdException) {
// This error is thrown when a non-existing task is used
InvalidTypeIdException invalidTypeIdException = (InvalidTypeIdException) e;
throw new ConstraintViolationException(
"Invalid type: " + invalidTypeIdException.getTypeId(),
Set.of(
ManualConstraintViolation.of(
"Invalid type: " + invalidTypeIdException.getTypeId(),
input,
String.class,
invalidTypeIdException.getPathReference(),
null
),
ManualConstraintViolation.of(
e.getMessage(),
input,
String.class,
invalidTypeIdException.getPathReference(),
null
)
)
);
}
else {
throw new ConstraintViolationException(
"Illegal "+ resource +" yaml:" + e.getMessage(),
"Illegal "+ resource +" yaml: " + e.getMessage(),
Collections.singleton(
ManualConstraintViolation.of(
"Caused by: " + e.getCause() + "\nMessage: " + e.getMessage(),
e.getCause() == null ? e.getMessage() : e.getMessage() + "\nCaused by: " + e.getCause().getMessage(),
input,
String.class,
"flow",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ void triggerEmpty() {
assertThat(((Schedule) parse.getTriggers().get(0)).getBackfill().getStart(), nullValue());
}

@Test
void invalidTask() {
ConstraintViolationException exception = assertThrows(
ConstraintViolationException.class,
() -> this.parse("flows/invalids/invalid-task.yaml")
);

assertThat(exception.getConstraintViolations().size(), is(2));
assertThat(new ArrayList<>(exception.getConstraintViolations()).stream().filter(e -> e.getMessage().contains("Invalid type")).findFirst().orElseThrow().getMessage(), containsString("Invalid type: io.kestra.core.tasks.debugs.MissingOne"));
}


@Test
void includeFailed() {
ConstraintViolationException exception = assertThrows(
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/resources/flows/invalids/invalid-task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
id: duplicate
namespace: io.kestra.tests

listeners:
- tasks:
- id: date1
type: io.kestra.core.tasks.debugs.MissingOne
format: "{{taskrun.startDate}}"

tasks:
- id: date2
type: io.kestra.core.tasks.debugs.MissingTwo
format: "{{taskrun.startDate}}"


errors:
- id: date3
type: io.kestra.core.tasks.debugs.MissingThree
format: "{{taskrun.startDate}}"

0 comments on commit d895ec3

Please sign in to comment.