|
19 | 19 |
|
20 | 20 | package io.temporal.internal.sync; |
21 | 21 |
|
22 | | -import static io.temporal.worker.WorkflowErrorPolicy.FailWorkflow; |
| 22 | +import static io.temporal.internal.common.CheckedExceptionWrapper.wrap; |
| 23 | +import static io.temporal.internal.sync.WorkflowInternal.unwrap; |
23 | 24 |
|
24 | 25 | import com.google.common.base.Preconditions; |
25 | 26 | import io.temporal.api.common.v1.Payloads; |
@@ -102,7 +103,9 @@ void addWorkflowImplementationTypes( |
102 | 103 |
|
103 | 104 | <R> void addWorkflowImplementationFactory(Class<R> clazz, Functions.Func<R> factory) { |
104 | 105 | WorkflowImplementationOptions unitTestingOptions = |
105 | | - WorkflowImplementationOptions.newBuilder().setWorkflowErrorPolicy(FailWorkflow).build(); |
| 106 | + WorkflowImplementationOptions.newBuilder() |
| 107 | + .setFailWorkflowExceptionTypes(Throwable.class) |
| 108 | + .build(); |
106 | 109 | addWorkflowImplementationFactory(unitTestingOptions, clazz, factory); |
107 | 110 | } |
108 | 111 |
|
@@ -287,29 +290,47 @@ public Object execute(Object[] arguments) { |
287 | 290 | } catch (IllegalAccessException e) { |
288 | 291 | throw new Error(mapToWorkflowExecutionException(e, dataConverter)); |
289 | 292 | } catch (InvocationTargetException e) { |
290 | | - Throwable targetException = e.getTargetException(); |
291 | | - if (targetException instanceof Error) { |
292 | | - throw (Error) targetException; |
| 293 | + Throwable target = e.getTargetException(); |
| 294 | + if (target instanceof DestroyWorkflowThreadError) { |
| 295 | + throw (DestroyWorkflowThreadError) target; |
293 | 296 | } |
294 | | - if (log.isErrorEnabled()) { |
295 | | - boolean cancelRequested = |
296 | | - WorkflowInternal.getRootWorkflowContext().getContext().isCancelRequested(); |
297 | | - if (!cancelRequested || !FailureConverter.isCanceledCause(targetException)) { |
298 | | - log.error( |
299 | | - "Workflow execution failure " |
300 | | - + "WorkflowId=" |
301 | | - + info.getWorkflowId() |
302 | | - + ", RunId=" |
303 | | - + info.getRunId() |
304 | | - + ", WorkflowType=" |
305 | | - + info.getWorkflowType(), |
306 | | - targetException); |
| 297 | + Throwable exception = unwrap(target); |
| 298 | + |
| 299 | + WorkflowImplementationOptions options = implementationOptions.get(info.getWorkflowType()); |
| 300 | + Class<? extends Throwable>[] failTypes = options.getFailWorkflowExceptionTypes(); |
| 301 | + if (exception instanceof TemporalFailure) { |
| 302 | + logWorkflowExecutionException(info, exception); |
| 303 | + throw mapToWorkflowExecutionException(exception, dataConverter); |
| 304 | + } |
| 305 | + for (Class<? extends Throwable> failType : failTypes) { |
| 306 | + if (failType.isAssignableFrom(exception.getClass())) { |
| 307 | + // fail workflow |
| 308 | + if (log.isErrorEnabled()) { |
| 309 | + boolean cancelRequested = |
| 310 | + WorkflowInternal.getRootWorkflowContext().getContext().isCancelRequested(); |
| 311 | + if (!cancelRequested || !FailureConverter.isCanceledCause(exception)) { |
| 312 | + logWorkflowExecutionException(info, exception); |
| 313 | + } |
| 314 | + } |
| 315 | + throw mapToWorkflowExecutionException(exception, dataConverter); |
307 | 316 | } |
308 | 317 | } |
309 | | - throw mapToWorkflowExecutionException(targetException, dataConverter); |
| 318 | + throw wrap(exception); |
310 | 319 | } |
311 | 320 | } |
312 | 321 |
|
| 322 | + private void logWorkflowExecutionException(WorkflowInfo info, Throwable exception) { |
| 323 | + log.error( |
| 324 | + "Workflow execution failure " |
| 325 | + + "WorkflowId=" |
| 326 | + + info.getWorkflowId() |
| 327 | + + ", RunId=" |
| 328 | + + info.getRunId() |
| 329 | + + ", WorkflowType=" |
| 330 | + + info.getWorkflowType(), |
| 331 | + exception); |
| 332 | + } |
| 333 | + |
313 | 334 | @Override |
314 | 335 | public void init(WorkflowOutboundCallsInterceptor outboundCalls) { |
315 | 336 | WorkflowInternal.getRootWorkflowContext().setHeadInterceptor(outboundCalls); |
@@ -365,11 +386,6 @@ static WorkflowExecutionException mapToWorkflowExecutionException( |
365 | 386 | return new WorkflowExecutionException(failure); |
366 | 387 | } |
367 | 388 |
|
368 | | - static WorkflowExecutionException mapError(Throwable error) { |
369 | | - Failure failure = FailureConverter.exceptionToFailureNoUnwrapping(error); |
370 | | - return new WorkflowExecutionException(failure); |
371 | | - } |
372 | | - |
373 | 389 | @Override |
374 | 390 | public String toString() { |
375 | 391 | return "POJOWorkflowImplementationFactory{" |
|
0 commit comments