|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.DurableTask.Worker; |
| 5 | + |
| 6 | +namespace ExceptionPropertiesSample; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Custom exception properties provider that extracts additional properties from exceptions |
| 10 | +/// to include in TaskFailureDetails for better diagnostics and error handling. |
| 11 | +/// </summary> |
| 12 | +public class CustomExceptionPropertiesProvider : IExceptionPropertiesProvider |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Extracts custom properties from exceptions to enrich failure details. |
| 16 | + /// </summary> |
| 17 | + /// <param name="exception">The exception to extract properties from.</param> |
| 18 | + /// <returns> |
| 19 | + /// A dictionary of custom properties to include in the FailureDetails, |
| 20 | + /// or null if no properties should be added for this exception type. |
| 21 | + /// </returns> |
| 22 | + public IDictionary<string, object?>? GetExceptionProperties(Exception exception) |
| 23 | + { |
| 24 | + return exception switch |
| 25 | + { |
| 26 | + BusinessValidationException businessEx => new Dictionary<string, object?> |
| 27 | + { |
| 28 | + ["ErrorCode"] = businessEx.ErrorCode, |
| 29 | + ["StatusCode"] = businessEx.StatusCode, |
| 30 | + ["Metadata"] = businessEx.Metadata, |
| 31 | + }, |
| 32 | + ArgumentOutOfRangeException argEx => new Dictionary<string, object?> |
| 33 | + { |
| 34 | + ["ParameterName"] = argEx.ParamName ?? string.Empty, |
| 35 | + ["ActualValue"] = argEx.ActualValue?.ToString() ?? string.Empty, |
| 36 | + }, |
| 37 | + ArgumentNullException argNullEx => new Dictionary<string, object?> |
| 38 | + { |
| 39 | + ["ParameterName"] = argNullEx.ParamName ?? string.Empty, |
| 40 | + }, |
| 41 | + _ => null // No custom properties for other exception types |
| 42 | + }; |
| 43 | + } |
| 44 | +} |
| 45 | + |
0 commit comments