Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageId>AStar.Dev.Functional.Extensions</PackageId>
<Version>0.3.1-alpha</Version>
<Version>0.3.2-alpha</Version>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Authors>AStar Development, Jason Barden</Authors>
<Company>AStar Development</Company>
Expand All @@ -26,7 +26,7 @@
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Title>AStar.Dev.Functional.Extensions</Title>
<Copyright>AStar Development 2025</Copyright>
<PackageReleaseNotes>A complete rewrite of Result etc., and additional extensions / objects.</PackageReleaseNotes>
<PackageReleaseNotes>Add an extension method to convert a failures exception message to an instance of the ErrorResponse class.</PackageReleaseNotes>
<PackageIcon>astar.png</PackageIcon>
</PropertyGroup>

Expand Down
21 changes: 21 additions & 0 deletions src/AStar.Dev.Functional.Extensions/TryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace AStar.Dev.Functional.Extensions;

/// <summary>
/// Extensions for the Try class to convert Result types.
/// These extensions allow for converting Result types with exceptions to Result types with ErrorResponse.
/// </summary>
public static class TryExtensions
{

/// <summary>
/// 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...).
/// </summary>
/// <typeparam name="T">The type of the successful result</typeparam>
/// <param name="result">The Result object being extended.</param>
/// <returns>A success result without change if applicable, otherwise, the exception will be mapped to an ErrorResponse</returns>
public static Result<T, ErrorResponse> ToErrorResponse<T>(this Result<T, Exception> result)
=> result.MapFailure(
ex => new ErrorResponse(ex.GetBaseException().Message));
}
Loading