diff --git a/src/AStar.Dev.Functional.Extensions/AStar.Dev.Functional.Extensions.csproj b/src/AStar.Dev.Functional.Extensions/AStar.Dev.Functional.Extensions.csproj index 14ae997..f54be2d 100644 --- a/src/AStar.Dev.Functional.Extensions/AStar.Dev.Functional.Extensions.csproj +++ b/src/AStar.Dev.Functional.Extensions/AStar.Dev.Functional.Extensions.csproj @@ -9,7 +9,7 @@ true snupkg AStar.Dev.Functional.Extensions - 0.3.1-alpha + 0.3.2-alpha Readme.md AStar Development, Jason Barden AStar Development @@ -26,7 +26,7 @@ Readme.md AStar.Dev.Functional.Extensions AStar Development 2025 - A complete rewrite of Result etc., and additional extensions / objects. + Add an extension method to convert a failures exception message to an instance of the ErrorResponse class. astar.png diff --git a/src/AStar.Dev.Functional.Extensions/TryExtensions.cs b/src/AStar.Dev.Functional.Extensions/TryExtensions.cs new file mode 100644 index 0000000..822c71d --- /dev/null +++ b/src/AStar.Dev.Functional.Extensions/TryExtensions.cs @@ -0,0 +1,21 @@ +using System; + +namespace AStar.Dev.Functional.Extensions; + +/// +/// Extensions for the Try class to convert Result types. +/// These extensions allow for converting Result types with exceptions to Result types with ErrorResponse. +/// +public static class TryExtensions +{ + + /// + /// Converts a Result with an Exception to a Result with an ErrorResponse (specifically, the base exception message is mapped to the ErrorResponse - please note: NO translation happens...). + /// + /// The type of the successful result + /// The Result object being extended. + /// A success result without change if applicable, otherwise, the exception will be mapped to an ErrorResponse + public static Result ToErrorResponse(this Result result) + => result.MapFailure( + ex => new ErrorResponse(ex.GetBaseException().Message)); +}