forked from serverlessworkflow/sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix serverlessworkflow#468] Try/raise implementation
Signed-off-by: Francisco Javier Tirado Sarti <ftirados@redhat.com>
- Loading branch information
Showing
18 changed files
with
474 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
impl/core/src/main/java/io/serverlessworkflow/impl/StringFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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.impl; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
@FunctionalInterface | ||
public interface StringFilter extends BiFunction<WorkflowContext, TaskContext<?>, String> {} |
73 changes: 73 additions & 0 deletions
73
impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.impl; | ||
|
||
public record WorkflowError( | ||
String type, int status, String instance, String title, String details) { | ||
|
||
private static final String ERROR_FORMAT = "https://serverlessworkflow.io/spec/1.0.0/errors/%s"; | ||
public static final String RUNTIME_TYPE = String.format(ERROR_FORMAT, "runtime"); | ||
public static final String COMM_TYPE = String.format(ERROR_FORMAT, "communication"); | ||
|
||
public static Builder error(String type, int status) { | ||
return new Builder(type, status); | ||
} | ||
|
||
public static Builder communication(int status, TaskContext<?> context, Exception ex) { | ||
return new Builder(COMM_TYPE, status) | ||
.instance(context.position().jsonPointer()) | ||
.title(ex.getMessage()); | ||
} | ||
|
||
public static Builder runtime(int status, TaskContext<?> context, Exception ex) { | ||
return new Builder(RUNTIME_TYPE, status) | ||
.instance(context.position().jsonPointer()) | ||
.title(ex.getMessage()); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private final String type; | ||
private int status; | ||
private String instance; | ||
private String title; | ||
private String details; | ||
|
||
private Builder(String type, int status) { | ||
this.type = type; | ||
this.status = status; | ||
} | ||
|
||
public Builder instance(String instance) { | ||
this.instance = instance; | ||
return this; | ||
} | ||
|
||
public Builder title(String title) { | ||
this.title = title; | ||
return this; | ||
} | ||
|
||
public Builder details(String details) { | ||
this.details = details; | ||
return this; | ||
} | ||
|
||
public WorkflowError build() { | ||
return new WorkflowError(type, status, instance, title, details); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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.impl; | ||
|
||
public class WorkflowException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private final WorkflowError worflowError; | ||
|
||
public WorkflowException(WorkflowError error) { | ||
this(error, null); | ||
} | ||
|
||
public WorkflowException(WorkflowError error, Throwable cause) { | ||
super(error.toString(), cause); | ||
this.worflowError = error; | ||
} | ||
|
||
public WorkflowError getWorflowError() { | ||
return worflowError; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
impl/core/src/main/java/io/serverlessworkflow/impl/executors/RaiseExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.impl.executors; | ||
|
||
import io.serverlessworkflow.api.types.Error; | ||
import io.serverlessworkflow.api.types.ErrorInstance; | ||
import io.serverlessworkflow.api.types.ErrorType; | ||
import io.serverlessworkflow.api.types.RaiseTask; | ||
import io.serverlessworkflow.api.types.RaiseTaskError; | ||
import io.serverlessworkflow.impl.StringFilter; | ||
import io.serverlessworkflow.impl.TaskContext; | ||
import io.serverlessworkflow.impl.WorkflowContext; | ||
import io.serverlessworkflow.impl.WorkflowDefinition; | ||
import io.serverlessworkflow.impl.WorkflowError; | ||
import io.serverlessworkflow.impl.WorkflowException; | ||
import io.serverlessworkflow.impl.WorkflowUtils; | ||
import io.serverlessworkflow.impl.expressions.ExpressionFactory; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.BiFunction; | ||
|
||
public class RaiseExecutor extends AbstractTaskExecutor<RaiseTask> { | ||
|
||
private final BiFunction<WorkflowContext, TaskContext<RaiseTask>, WorkflowError> errorBuilder; | ||
|
||
private final StringFilter typeFilter; | ||
private final Optional<StringFilter> instanceFilter; | ||
private final StringFilter titleFilter; | ||
private final StringFilter detailFilter; | ||
|
||
protected RaiseExecutor(RaiseTask task, WorkflowDefinition definition) { | ||
super(task, definition); | ||
RaiseTaskError raiseError = task.getRaise().getError(); | ||
Error error = | ||
raiseError.getRaiseErrorDefinition() != null | ||
? raiseError.getRaiseErrorDefinition() | ||
: findError(definition, raiseError.getRaiseErrorReference()); | ||
this.typeFilter = getTypeFunction(definition.expressionFactory(), error.getType()); | ||
this.instanceFilter = getInstanceFunction(definition.expressionFactory(), error.getInstance()); | ||
this.titleFilter = | ||
WorkflowUtils.buildStringFilter(definition.expressionFactory(), error.getTitle()); | ||
this.detailFilter = | ||
WorkflowUtils.buildStringFilter(definition.expressionFactory(), error.getDetail()); | ||
this.errorBuilder = (w, t) -> buildError(error, w, t); | ||
} | ||
|
||
private static Error findError(WorkflowDefinition definition, String raiseErrorReference) { | ||
Map<String, Error> errorsMap = | ||
definition.workflow().getUse().getErrors().getAdditionalProperties(); | ||
Error error = errorsMap.get(raiseErrorReference); | ||
if (error == null) { | ||
throw new IllegalArgumentException("Error " + error + "is not defined in " + errorsMap); | ||
} | ||
return error; | ||
} | ||
|
||
private WorkflowError buildError( | ||
Error error, WorkflowContext context, TaskContext<RaiseTask> taskContext) { | ||
return WorkflowError.error(typeFilter.apply(context, taskContext), error.getStatus()) | ||
.instance( | ||
instanceFilter | ||
.map(f -> f.apply(context, taskContext)) | ||
.orElseGet(() -> taskContext.position().jsonPointer())) | ||
.title(titleFilter.apply(context, taskContext)) | ||
.details(detailFilter.apply(context, taskContext)) | ||
.build(); | ||
} | ||
|
||
private Optional<StringFilter> getInstanceFunction( | ||
ExpressionFactory expressionFactory, ErrorInstance errorInstance) { | ||
return errorInstance != null | ||
? Optional.of( | ||
WorkflowUtils.buildStringFilter( | ||
expressionFactory, | ||
errorInstance.getExpressionErrorInstance(), | ||
errorInstance.getLiteralErrorInstance())) | ||
: Optional.empty(); | ||
} | ||
|
||
private StringFilter getTypeFunction(ExpressionFactory expressionFactory, ErrorType type) { | ||
return WorkflowUtils.buildStringFilter( | ||
expressionFactory, | ||
type.getExpressionErrorType(), | ||
type.getLiteralErrorType().get().toString()); | ||
} | ||
|
||
@Override | ||
protected void internalExecute(WorkflowContext workflow, TaskContext<RaiseTask> taskContext) { | ||
throw new WorkflowException(errorBuilder.apply(workflow, taskContext)); | ||
} | ||
} |
Oops, something went wrong.