Skip to content

Enable Custom Exception Provider with Durable Functions Java#294

Open
nytian wants to merge 2 commits into
mainfrom
nytian/exception-provider-middleware
Open

Enable Custom Exception Provider with Durable Functions Java#294
nytian wants to merge 2 commits into
mainfrom
nytian/exception-provider-middleware

Conversation

@nytian

@nytian nytian commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Today when a Java activity fails, only the exception type and message reach the Durable extension. The TaskFailureDetails from the worker cannot carry user context like error codes or correlation IDs, so that data is dropped at the worker to host boundary. Users have no supported way to surface it in their error handling.

This PR adds an ActivityMiddleware that fixes this. When an activity throws, it runs a registered ExceptionPropertiesProvider and serializes the exception into structured TaskFailureDetails JSON, including error type, message, stack trace, nested causes, and custom properties. It runs worker side while the live Exception still exists, which is the only place this data is available before serialization.

Nested causes are emitted as innerFailure, bounded by MAX_INNER_FAILURE_DEPTH (10). Provider discovery uses cross class loader SPI lookup so it works from worker threads. When no provider is registered, the original exception is rethrown unchanged, so the change is additive. Also registers the middleware via SPI and adds the properties field to TaskFailureDetails. Adds ActivityMiddlewareTest, and the e2e test at durable extension repo accordingy

@nytian
nytian requested a review from a team as a code owner July 17, 2026 03:38
Copilot AI review requested due to automatic review settings July 17, 2026 03:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Azure Functions activity middleware that enables opt-in structured exception properties (via ExceptionPropertiesProvider) to be surfaced as a TaskFailureDetails-shaped JSON payload when an activity fails, aligning behavior with Durable Task failure details handling.

Changes:

  • Introduces ActivityMiddleware to reshape activity failures into TaskFailureDetails JSON when a provider returns custom properties, with SPI-based provider discovery across class loaders.
  • Registers the new middleware via META-INF/services and adds unit tests (including cross-class-loader SPI discovery regression coverage).
  • Updates protobuf source tracking and fixes a minor syntax typo in the proto file.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/durabletask-protobuf/protos/orchestrator_service.proto Fixes a proto syntax typo (extra semicolon) in a deprecated field.
internal/durabletask-protobuf/PROTO_SOURCE_COMMIT_HASH Updates the recorded upstream protobuf source commit hash.
azurefunctions/src/main/java/com/microsoft/durabletask/azurefunctions/internal/middleware/ActivityMiddleware.java Adds new activity middleware to reshape failures into TaskFailureDetails JSON and discover providers via SPI.
azurefunctions/src/main/resources/META-INF/services/com.microsoft.azure.functions.internal.spi.middleware.Middleware Registers ActivityMiddleware for Azure Functions SPI middleware discovery.
azurefunctions/src/test/java/com/microsoft/durabletask/azurefunctions/internal/middleware/ActivityMiddlewareTest.java Adds unit coverage for reshaping behavior and SPI discovery across class loaders.
azurefunctions/src/test/java/com/microsoft/durabletask/azurefunctions/internal/middleware/TestExceptionPropertiesProvider.java Adds a test-only provider for verifying SPI discovery behavior.
azurefunctions/build.gradle Adds azure-functions-java-spi as a test dependency so middleware tests compile.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +128 to +132
} catch (Throwable t) {
// Discovery failures must not break activity execution; the feature is opt-in.
LOGGER.warning("Failed to load ExceptionPropertiesProvider via ServiceLoader using "
+ classLoader + ": " + t);
}
Comment on lines +173 to +177
} catch (Exception providerException) {
// Don't let a misbehaving provider mask the original failure.
LOGGER.warning("ExceptionPropertiesProvider threw while extracting properties: " + providerException);
return null;
}
Comment on lines +70 to +78
Throwable userException = unwrap(e);
Map<String, Object> properties = safeGetProperties(provider, userException);
if (properties == null || properties.isEmpty()) {
// No custom properties for this failure - preserve the original behavior.
throw e;
}

throw new StructuredActivityFailure(buildFailureDetailsJson(userException, provider));
}
Comment on lines +266 to +277
Path root = Files.createTempDirectory("amw-spi-");
root.toFile().deleteOnExit();
Path servicesDir = root.resolve("META-INF").resolve("services");
Files.createDirectories(servicesDir);
Path serviceFile = servicesDir.resolve(ExceptionPropertiesProvider.class.getName());
Files.write(serviceFile,
(TestExceptionPropertiesProvider.class.getName() + System.lineSeparator())
.getBytes(StandardCharsets.UTF_8));
serviceFile.toFile().deleteOnExit();
servicesDir.toFile().deleteOnExit();
root.resolve("META-INF").toFile().deleteOnExit();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants