Skip to content

Commit

Permalink
fix: Add non-async unwrapped status invoker
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasg14b committed Feb 5, 2024
1 parent cacb061 commit 736a920
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Clawfoot.Status/Clawfoot.Status.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RepositoryUrl>https://github.com/clawfoot-software/Clawfoot/tree/master/Clawfoot.Status</RepositoryUrl>
<Copyright>2023 Douglas Gaskell</Copyright>
<Version>1.1.4</Version>
<Version>1.1.5</Version>
<LangVersion>8.0</LangVersion>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<AssemblyVersion>0.1.1.4</AssemblyVersion>
<FileVersion>0.1.1.4</FileVersion>
<AssemblyVersion>0.1.1.5</AssemblyVersion>
<FileVersion>0.1.1.5</FileVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>Adds additional Status constructors to simplify caller needs to create a status from an Irror that is created from an error enum</PackageReleaseNotes>
</PropertyGroup>
Expand Down
32 changes: 32 additions & 0 deletions Clawfoot.Status/Extensions/GenericInvokeExtenions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ public static T InvokeResult<T>(this IStatus<T> status, Func<T> func, bool keepE

return default(T);
}

/// <summary>
/// Invokes the delegate, and if it throws an exception, records it in the current status and returns null.
/// If success, unwraps the nested status, sets the status result, and returns the result of the delegate
/// </summary>
/// <param name="func"></param>
/// <param name="keepException"></param>
/// <returns></returns>
public static T InvokeResult<T>(this IStatus<T> status, Func<IStatus<T>> func, bool keepException = false)
{
try
{
IStatus<T> result = func.Invoke();
status.SetResult(result);
status.MergeStatuses(result);

return result.Result;
}
catch (Exception ex)
{
if (!keepException)
{
status.AddError(ex.Message);
}
else
{
status.AddException(ex);
}
}

return default(T);
}

/// <summary>
/// Invokes the delegate, and if it throws an exception, records it in the current status and returns null.
Expand Down

0 comments on commit 736a920

Please sign in to comment.