Skip to content

Commit dd2564b

Browse files
authored
Include Exception Properties at FailureDetails (#263)
* initial commit * udpate * udpate * update * disable new test * check test * check test * udpate test * udpate test * fix * update comment * update comment * update
1 parent b4075c0 commit dd2564b

9 files changed

Lines changed: 948 additions & 19 deletions

File tree

client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcWorker.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public final class DurableTaskGrpcWorker implements AutoCloseable {
4646
private final DataConverter dataConverter;
4747
private final Duration maximumTimerInterval;
4848
private final DurableTaskGrpcWorkerVersioningOptions versioningOptions;
49+
private final ExceptionPropertiesProvider exceptionPropertiesProvider;
4950
private final int maxConcurrentEntityWorkItems;
5051
private final ExecutorService workItemExecutor;
5152

@@ -84,6 +85,7 @@ public final class DurableTaskGrpcWorker implements AutoCloseable {
8485
this.dataConverter = builder.dataConverter != null ? builder.dataConverter : new JacksonDataConverter();
8586
this.maximumTimerInterval = builder.maximumTimerInterval != null ? builder.maximumTimerInterval : DEFAULT_MAXIMUM_TIMER_INTERVAL;
8687
this.versioningOptions = builder.versioningOptions;
88+
this.exceptionPropertiesProvider = builder.exceptionPropertiesProvider;
8789
int maxThreads = builder.maxWorkItemThreads > 0 ? builder.maxWorkItemThreads : DEFAULT_MAX_WORK_ITEM_THREADS;
8890
this.workItemExecutor = new ThreadPoolExecutor(
8991
0, maxThreads,
@@ -159,7 +161,8 @@ public void startAndBlock() {
159161
this.maximumTimerInterval,
160162
logger,
161163
this.versioningOptions,
162-
true);
164+
true,
165+
this.exceptionPropertiesProvider);
163166
TaskActivityExecutor taskActivityExecutor = new TaskActivityExecutor(
164167
this.activityFactories,
165168
this.dataConverter,
@@ -389,11 +392,8 @@ public void startAndBlock() {
389392
activityRequest.getTaskId());
390393
} catch (Throwable e) {
391394
activityError = e;
392-
failureDetails = TaskFailureDetails.newBuilder()
393-
.setErrorType(e.getClass().getName())
394-
.setErrorMessage(e.getMessage())
395-
.setStackTrace(StringValue.of(FailureDetails.getFullStackTrace(e)))
396-
.build();
395+
failureDetails = FailureDetails.fromException(
396+
e, this.exceptionPropertiesProvider).toProto();
397397
} finally {
398398
activityScope.close();
399399
TracingHelper.endSpan(activitySpan, activityError);

client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcWorkerBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public final class DurableTaskGrpcWorkerBuilder {
2424
DataConverter dataConverter;
2525
Duration maximumTimerInterval;
2626
DurableTaskGrpcWorkerVersioningOptions versioningOptions;
27+
ExceptionPropertiesProvider exceptionPropertiesProvider;
2728
int maxConcurrentEntityWorkItems = 1;
2829
int maxWorkItemThreads;
2930
private WorkItemFilter workItemFilter;
@@ -292,6 +293,21 @@ public DurableTaskGrpcWorkerBuilder useVersioning(DurableTaskGrpcWorkerVersionin
292293
return this;
293294
}
294295

296+
/**
297+
* Sets the {@link ExceptionPropertiesProvider} to use for extracting custom properties from exceptions.
298+
* <p>
299+
* When set, the provider is invoked whenever an activity or orchestration fails with an exception. The returned
300+
* properties are included in the {@link FailureDetails} and can be retrieved via
301+
* {@link FailureDetails#getProperties()}.
302+
*
303+
* @param provider the exception properties provider
304+
* @return this builder object
305+
*/
306+
public DurableTaskGrpcWorkerBuilder exceptionPropertiesProvider(ExceptionPropertiesProvider provider) {
307+
this.exceptionPropertiesProvider = provider;
308+
return this;
309+
}
310+
295311
/**
296312
* Sets explicit work item filters for this worker. When set, only work items matching the filters
297313
* will be dispatched to this worker by the backend.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.microsoft.durabletask;
4+
5+
import javax.annotation.Nullable;
6+
import java.util.Map;
7+
8+
/**
9+
* Provider interface for extracting custom properties from exceptions.
10+
* <p>
11+
* Implementations of this interface can be registered with a {@link DurableTaskGrpcWorkerBuilder} to include
12+
* custom exception properties in {@link FailureDetails} when activities or orchestrations fail.
13+
* These properties are then available via {@link FailureDetails#getProperties()}.
14+
* <p>
15+
* Example usage:
16+
* <pre>{@code
17+
* DurableTaskGrpcWorker worker = new DurableTaskGrpcWorkerBuilder()
18+
* .exceptionPropertiesProvider(exception -> {
19+
* if (exception instanceof MyCustomException) {
20+
* MyCustomException custom = (MyCustomException) exception;
21+
* Map<String, Object> props = new HashMap<>();
22+
* props.put("errorCode", custom.getErrorCode());
23+
* props.put("retryable", custom.isRetryable());
24+
* return props;
25+
* }
26+
* return null;
27+
* })
28+
* .addOrchestration(...)
29+
* .build();
30+
* }</pre>
31+
*/
32+
@FunctionalInterface
33+
public interface ExceptionPropertiesProvider {
34+
35+
/**
36+
* Extracts custom properties from the given exception.
37+
* <p>
38+
* Return {@code null} or an empty map if no custom properties should be included for this exception.
39+
*
40+
* @param exception the exception to extract properties from
41+
* @return a map of property names to values, or {@code null}
42+
*/
43+
@Nullable
44+
Map<String, Object> getExceptionProperties(Exception exception);
45+
}

0 commit comments

Comments
 (0)