Skip to content

Update error handling and retries #120

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

Merged
merged 1 commit into from
Sep 4, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.api.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.serverlessworkflow.api.error.ErrorDefinition;
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
import io.serverlessworkflow.api.utils.Utils;
import io.serverlessworkflow.api.workflow.Errors;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ErrorsDeserializer extends StdDeserializer<Errors> {

private static final long serialVersionUID = 510l;
private static Logger logger = LoggerFactory.getLogger(ErrorsDeserializer.class);

@SuppressWarnings("unused")
private WorkflowPropertySource context;

public ErrorsDeserializer() {
this(Errors.class);
}

public ErrorsDeserializer(Class<?> vc) {
super(vc);
}

public ErrorsDeserializer(WorkflowPropertySource context) {
this(Errors.class);
this.context = context;
}

@Override
public Errors deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException {

ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode node = jp.getCodec().readTree(jp);

Errors errors = new Errors();
List<ErrorDefinition> errorDefinitions = new ArrayList<>();

if (node.isArray()) {
for (final JsonNode nodeEle : node) {
errorDefinitions.add(mapper.treeToValue(nodeEle, ErrorDefinition.class));
}
} else {
String errorsFileDef = node.asText();
String errorsFileSrc = Utils.getResourceFileAsString(errorsFileDef);
JsonNode errorsRefNode;
ObjectMapper jsonWriter = new ObjectMapper();
if (errorsFileSrc != null && errorsFileSrc.trim().length() > 0) {
// if its a yaml def convert to json first
if (!errorsFileSrc.trim().startsWith("{")) {
// convert yaml to json to validate
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
Object obj = yamlReader.readValue(errorsFileSrc, Object.class);

errorsRefNode = jsonWriter.readTree(new JSONObject(jsonWriter.writeValueAsString(obj)).toString());
} else {
errorsRefNode = jsonWriter.readTree(new JSONObject(errorsFileSrc).toString());
}

JsonNode refErrors = errorsRefNode.get("errors");
if (refErrors != null) {
for (final JsonNode nodeEle : refErrors) {
errorDefinitions.add(mapper.treeToValue(nodeEle, ErrorDefinition.class));
}
} else {
logger.error("Unable to find error definitions in reference file: {}", errorsFileSrc);
}

} else {
logger.error("Unable to load errors defs reference file: {}", errorsFileSrc);
}

}
errors.setErrorDefs(errorDefinitions);
return errors;

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private void addDefaultDeserializers() {
addDeserializer(DataInputSchema.class, new DataInputSchemaDeserializer(workflowPropertySource));
addDeserializer(AuthDefinition.class, new AuthDefinitionDeserializer(workflowPropertySource));
addDeserializer(StateExecTimeout.class, new StateExecTimeoutDeserializer(workflowPropertySource));
addDeserializer(Errors.class, new ErrorsDeserializer(workflowPropertySource));
}

public ExtensionSerializer getExtensionSerializer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.serverlessworkflow.api.Workflow;
import io.serverlessworkflow.api.error.ErrorDefinition;
import io.serverlessworkflow.api.events.EventDefinition;
import io.serverlessworkflow.api.functions.FunctionDefinition;
import io.serverlessworkflow.api.interfaces.Extension;
Expand Down Expand Up @@ -102,6 +103,10 @@ public void serialize(Workflow workflow,
gen.writeBooleanField("keepActive", workflow.isKeepActive());
}

if (workflow.isAutoRetries()) {
gen.writeBooleanField("autoRetries", workflow.isAutoRetries());
}

if (workflow.getMetadata() != null && !workflow.getMetadata().isEmpty()) {
gen.writeObjectField("metadata",
workflow.getMetadata());
Expand Down Expand Up @@ -140,6 +145,17 @@ public void serialize(Workflow workflow,
gen.writeEndArray();
}

if (workflow.getErrors() != null && !workflow.getErrors().getErrorDefs().isEmpty()) {
gen.writeArrayFieldStart("errors");
for (ErrorDefinition error : workflow.getErrors().getErrorDefs()) {
gen.writeObject(error);
}
gen.writeEndArray();
} else {
gen.writeArrayFieldStart("errors");
gen.writeEndArray();
}

if (workflow.getSecrets() != null && !workflow.getSecrets().getSecretDefs().isEmpty()) {
gen.writeArrayFieldStart("secrets");
for (String secretDef : workflow.getSecrets().getSecretDefs()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.api.workflow;

import io.serverlessworkflow.api.error.ErrorDefinition;

import java.util.List;

public class Errors {
private String refValue;
private List<ErrorDefinition> errorDefs;

public Errors() {
}

public Errors(List<ErrorDefinition> errorDefs) {
this.errorDefs = errorDefs;
}

public Errors(String refValue) {
this.refValue = refValue;
}

public String getRefValue() {
return refValue;
}

public void setRefValue(String refValue) {
this.refValue = refValue;
}

public List<ErrorDefinition> getErrorDefs() {
return errorDefs;
}

public void setErrorDefs(List<ErrorDefinition> errorDefs) {
this.errorDefs = errorDefs;
}
}
20 changes: 20 additions & 0 deletions api/src/main/resources/schema/actions/action.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@
"sleep": {
"$ref": "../sleep/sleep.json"
},
"retryRef": {
"type": "string",
"description": "References a defined workflow retry definition. If not defined the default retry policy is assumed"
},
"nonRetryableErrors": {
"type": "array",
"description": "List of unique references to defined workflow errors for which the action should not be retried. Used only when `autoRetries` is set to `true`",
"minItems": 1,
"items": {
"type": "string"
}
},
"retryableErrors": {
"type": "array",
"description": "List of unique references to defined workflow errors for which the action should be retried. Used only when `autoRetries` is set to `false`",
"minItems": 1,
"items": {
"type": "string"
}
},
"actionDataFilter": {
"$ref": "../filters/actiondatafilter.json"
}
Expand Down
20 changes: 9 additions & 11 deletions api/src/main/resources/schema/error/error.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
"type": "object",
"javaType": "io.serverlessworkflow.api.error.Error",
"properties": {
"error": {
"errorRef": {
"type": "string",
"description": "Domain-specific error name, or '*' to indicate all possible errors",
"description": "Reference to a unique workflow error definition. Used of errorRefs is not used",
"minLength": 1
},
"code": {
"type": "string",
"description": "Error code. Can be used in addition to the name to help runtimes resolve to technical errors/exceptions. Should not be defined if error is set to '*'",
"minLength": 1
},
"retryRef": {
"type": "string",
"description": "References a unique name of a retry definition.",
"minLength": 1
"errorRefs": {
"type": "array",
"description": "References one or more workflow error definitions. Used if errorRef is not used",
"minItems": 1,
"items": {
"type": "string"
}
},
"transition": {
"$ref": "../transitions/transition.json",
Expand Down
23 changes: 23 additions & 0 deletions api/src/main/resources/schema/error/errordef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"type": "object",
"javaType": "io.serverlessworkflow.api.error.ErrorDefinition",
"properties": {
"name": {
"type": "string",
"description": "Domain-specific error name",
"minLength": 1
},
"code": {
"type": "string",
"description": "Error code. Can be used in addition to the name to help runtimes resolve to technical errors/exceptions. Should not be defined if error is set to '*'",
"minLength": 1
},
"description": {
"type": "string",
"description": "Error description"
}
},
"required": [
"name"
]
}
10 changes: 10 additions & 0 deletions api/src/main/resources/schema/workflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
"default": false,
"description": "If 'true', workflow instances is not terminated when there are no active execution paths. Instance can be terminated via 'terminate end definition' or reaching defined 'execTimeout'"
},
"autoRetries": {
"type": "boolean",
"default": false,
"description": "If set to true, actions should automatically be retried on unchecked errors. Default is false"
},
"metadata": {
"$ref": "metadata/metadata.json"
},
Expand All @@ -63,6 +68,11 @@
"existingJavaType": "io.serverlessworkflow.api.workflow.Functions",
"description": "Workflow function definitions"
},
"errors": {
"type": "object",
"existingJavaType": "io.serverlessworkflow.api.workflow.Errors",
"description": "Workflow error definitions"
},
"retries": {
"type": "object",
"existingJavaType": "io.serverlessworkflow.api.workflow.Retries",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,40 @@ public void testActionsSleep(String workflowLocation) {
assertEquals("${ .customer }", functionRef2.getArguments().get("applicant").asText());

}

@ParameterizedTest
@ValueSource(strings = {"/features/errors.json", "/features/errors.yml"})
public void testErrorsParams(String workflowLocation) {
Workflow workflow = Workflow.fromSource(WorkflowTestUtils.readWorkflowFile(workflowLocation));

assertNotNull(workflow);
assertNotNull(workflow.getId());
assertNotNull(workflow.getName());
assertNotNull(workflow.getStates());
assertTrue(workflow.isAutoRetries());

assertNotNull(workflow.getStates());
assertEquals(1, workflow.getStates().size());

assertNotNull(workflow.getErrors());
assertEquals(2, workflow.getErrors().getErrorDefs().size());

assertTrue(workflow.getStates().get(0) instanceof OperationState);

OperationState operationState = (OperationState) workflow.getStates().get(0);
assertNotNull(operationState.getActions());
assertEquals(1, operationState.getActions().size());
List<Action> actions = operationState.getActions();
assertNotNull(actions.get(0).getFunctionRef());
assertEquals("addPet", actions.get(0).getFunctionRef().getRefName());
assertNotNull(actions.get(0).getRetryRef());
assertEquals("testRetry", actions.get(0).getRetryRef());
assertNotNull(actions.get(0).getNonRetryableErrors());
assertEquals(2, actions.get(0).getNonRetryableErrors().size());

assertNotNull(operationState.getOnErrors());
assertEquals(1, operationState.getOnErrors().size());
assertNotNull(operationState.getOnErrors().get(0).getErrorRefs());
assertEquals(2, operationState.getOnErrors().get(0).getErrorRefs().size());
}
}
2 changes: 1 addition & 1 deletion api/src/test/resources/examples/jobmonitoring.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
],
"onErrors": [
{
"error": "*",
"errorRef": "AllErrors",
"transition": "SubmitError"
}
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/resources/examples/jobmonitoring.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ states:
actionDataFilter:
results: "${ .jobuid }"
onErrors:
- error: "*"
- errorRef: "AllErrors"
transition: SubmitError
stateDataFilter:
output: "${ .jobuid }"
Expand Down
6 changes: 3 additions & 3 deletions api/src/test/resources/examples/provisionorder.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
"transition": "ApplyOrder",
"onErrors": [
{
"error": "Missing order id",
"errorRef": "Missing order id",
"transition": "MissingId"
},
{
"error": "Missing order item",
"errorRef": "Missing order item",
"transition": "MissingItem"
},
{
"error": "Missing order quantity",
"errorRef": "Missing order quantity",
"transition": "MissingQuantity"
}
]
Expand Down
Loading