-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net Processes - Add Process-Level Error Handler (#9477)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Enabled support for function-specific error-handler step in this PR: #9187 Fixes: #9291 This change provides the ability to define a _process scoped_ error handler (as opposed to function specific). When a function-scoped error-handler is defined, it will take precedence. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ```c# ProcessBuilder process = new(nameof(ProcessFunctionErrorHandledAsync)); ProcessStepBuilder errorStep = process.AddStepFromType<ErrorStep>(); process.OnError().SendEventTo(new ProcessFunctionTargetBuilder(errorStep)); class ErrorStep : KernelProcessStep { [KernelFunction] public void GlobalErrorHandler(Exception exception) { } } ``` **Notes:** - Switch error handler from passing `Exception` object to a `KernelProcessError` to satisfy serialization expectations - Normalized namespaces for `Internal` shared code - Introduced shared `ProcessConstants` file - Opportunistically converted some `List` creation to `Array` - Opportunistically included parameter name in some `Verify` assertions. - Opportunistically removed a extraneous _not-null_ directives (`!`) - Verified DAPR error handling in demo app (`True` means the expected error handler was invoked): <img width="449" alt="image" src="https://github.com/user-attachments/assets/2d987378-edd2-4c9f-92dd-cf112888b8b0"> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
- Loading branch information
Showing
30 changed files
with
395 additions
and
105 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
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
39 changes: 39 additions & 0 deletions
39
dotnet/src/Experimental/Process.Abstractions/KernelProcessError.cs
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,39 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel; | ||
|
||
/// <summary> | ||
/// Represents an failure that occurred during the execution of a process. | ||
/// </summary> | ||
/// <remarks> | ||
/// Initializes a new instance of the <see cref="KernelProcessError"/> class. | ||
/// </remarks> | ||
/// <param name="Type">The exception type name</param> | ||
/// <param name="Message">The exception message (<see cref="Exception.Message"/></param> | ||
/// <param name="StackTrace">The exception stack-trace (<see cref="Exception.StackTrace"/></param> | ||
[DataContract] | ||
public sealed record KernelProcessError( | ||
[property:DataMember] | ||
string Type, | ||
[property:DataMember] | ||
string Message, | ||
[property:DataMember] | ||
string? StackTrace) | ||
{ | ||
/// <summary> | ||
/// The inner failure, when exists, as <see cref="KernelProcessError"/>. | ||
/// </summary> | ||
[DataMember] | ||
public KernelProcessError? InnerError { get; init; } | ||
|
||
/// <summary> | ||
/// Factory method to create a <see cref="KernelProcessError"/> from a source <see cref="Exception"/> object. | ||
/// </summary> | ||
public static KernelProcessError FromException(Exception ex) => | ||
new(ex.GetType().Name, ex.Message, ex.StackTrace) | ||
{ | ||
InnerError = ex.InnerException is not null ? FromException(ex.InnerException) : null | ||
}; | ||
} |
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
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
Oops, something went wrong.