|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.executors; |
| 17 | + |
| 18 | +import io.serverlessworkflow.api.types.Error; |
| 19 | +import io.serverlessworkflow.api.types.ErrorInstance; |
| 20 | +import io.serverlessworkflow.api.types.ErrorType; |
| 21 | +import io.serverlessworkflow.api.types.RaiseTask; |
| 22 | +import io.serverlessworkflow.api.types.RaiseTaskError; |
| 23 | +import io.serverlessworkflow.impl.StringFilter; |
| 24 | +import io.serverlessworkflow.impl.TaskContext; |
| 25 | +import io.serverlessworkflow.impl.WorkflowContext; |
| 26 | +import io.serverlessworkflow.impl.WorkflowDefinition; |
| 27 | +import io.serverlessworkflow.impl.WorkflowError; |
| 28 | +import io.serverlessworkflow.impl.WorkflowException; |
| 29 | +import io.serverlessworkflow.impl.WorkflowUtils; |
| 30 | +import io.serverlessworkflow.impl.expressions.ExpressionFactory; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Optional; |
| 33 | +import java.util.function.BiFunction; |
| 34 | + |
| 35 | +public class RaiseExecutor extends AbstractTaskExecutor<RaiseTask> { |
| 36 | + |
| 37 | + private final BiFunction<WorkflowContext, TaskContext<RaiseTask>, WorkflowError> errorBuilder; |
| 38 | + |
| 39 | + private final StringFilter typeFilter; |
| 40 | + private final Optional<StringFilter> instanceFilter; |
| 41 | + private final StringFilter titleFilter; |
| 42 | + private final StringFilter detailFilter; |
| 43 | + |
| 44 | + protected RaiseExecutor(RaiseTask task, WorkflowDefinition definition) { |
| 45 | + super(task, definition); |
| 46 | + RaiseTaskError raiseError = task.getRaise().getError(); |
| 47 | + Error error = |
| 48 | + raiseError.getRaiseErrorDefinition() != null |
| 49 | + ? raiseError.getRaiseErrorDefinition() |
| 50 | + : findError(definition, raiseError.getRaiseErrorReference()); |
| 51 | + this.typeFilter = getTypeFunction(definition.expressionFactory(), error.getType()); |
| 52 | + this.instanceFilter = getInstanceFunction(definition.expressionFactory(), error.getInstance()); |
| 53 | + this.titleFilter = |
| 54 | + WorkflowUtils.buildStringFilter(definition.expressionFactory(), error.getTitle()); |
| 55 | + this.detailFilter = |
| 56 | + WorkflowUtils.buildStringFilter(definition.expressionFactory(), error.getDetail()); |
| 57 | + this.errorBuilder = (w, t) -> buildError(error, w, t); |
| 58 | + } |
| 59 | + |
| 60 | + private static Error findError(WorkflowDefinition definition, String raiseErrorReference) { |
| 61 | + Map<String, Error> errorsMap = |
| 62 | + definition.workflow().getUse().getErrors().getAdditionalProperties(); |
| 63 | + Error error = errorsMap.get(raiseErrorReference); |
| 64 | + if (error == null) { |
| 65 | + throw new IllegalArgumentException("Error " + error + "is not defined in " + errorsMap); |
| 66 | + } |
| 67 | + return error; |
| 68 | + } |
| 69 | + |
| 70 | + private WorkflowError buildError( |
| 71 | + Error error, WorkflowContext context, TaskContext<RaiseTask> taskContext) { |
| 72 | + return WorkflowError.error(typeFilter.apply(context, taskContext), error.getStatus()) |
| 73 | + .instance( |
| 74 | + instanceFilter |
| 75 | + .map(f -> f.apply(context, taskContext)) |
| 76 | + .orElseGet(() -> taskContext.position().jsonPointer())) |
| 77 | + .title(titleFilter.apply(context, taskContext)) |
| 78 | + .details(detailFilter.apply(context, taskContext)) |
| 79 | + .build(); |
| 80 | + } |
| 81 | + |
| 82 | + private Optional<StringFilter> getInstanceFunction( |
| 83 | + ExpressionFactory expressionFactory, ErrorInstance errorInstance) { |
| 84 | + return errorInstance != null |
| 85 | + ? Optional.of( |
| 86 | + WorkflowUtils.buildStringFilter( |
| 87 | + expressionFactory, |
| 88 | + errorInstance.getExpressionErrorInstance(), |
| 89 | + errorInstance.getLiteralErrorInstance())) |
| 90 | + : Optional.empty(); |
| 91 | + } |
| 92 | + |
| 93 | + private StringFilter getTypeFunction(ExpressionFactory expressionFactory, ErrorType type) { |
| 94 | + return WorkflowUtils.buildStringFilter( |
| 95 | + expressionFactory, |
| 96 | + type.getExpressionErrorType(), |
| 97 | + type.getLiteralErrorType().get().toString()); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected void internalExecute(WorkflowContext workflow, TaskContext<RaiseTask> taskContext) { |
| 102 | + throw new WorkflowException(errorBuilder.apply(workflow, taskContext)); |
| 103 | + } |
| 104 | +} |
0 commit comments