< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.Infrastructure.Core.SafeExecution
Assembly: ZhEaIsNsAaBn.Infrastructure.Core
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.Infrastructure.Core/Errors/SafeExecution.cs
Line coverage
83%
Covered lines: 10
Uncovered lines: 2
Coverable lines: 12
Total lines: 47
Line coverage: 83.3%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ExecuteSafe(...)100%4483.33%
ExecuteSafeAsync()100%4483.33%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.Infrastructure.Core/Errors/SafeExecution.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.Infrastructure.Core;
 2
 3/// <summary>
 4/// The shared error ladder (STYLE_GUIDE §6): runs a database call and turns its exception into an <see cref="Error"/>.
 5/// Cancellation always propagates; a concurrency conflict is classified here (provider-neutral); everything else goes t
 6/// context's <see cref="IDbErrorClassifier"/> (from its provider package), else <see cref="Error.Unknown"/>.
 7/// </summary>
 8public static class SafeExecution
 9{
 10    /// <summary>Runs <paramref name="action"/> and returns its result, or the classified error.</summary>
 11    /// <typeparam name="T">The result type.</typeparam>
 12    /// <param name="action">The database call, e.g. <c>() =&gt; dbContext.SaveChanges()</c>.</param>
 13    /// <param name="classifier">The provider classifier (<c>dbContext.GetDbErrorClassifier()</c>); <see langword="null"
 14    /// <returns>
 15    /// The result on success; otherwise <c>ConcurrencyException</c>, the classifier's error, or <see cref="Error.Unknow
 16    /// </returns>
 17    /// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception>
 18    /// <exception cref="OperationCanceledException">The call was cancelled.</exception>
 19    public static LayerResponse<T> ExecuteSafe< T >( Func<T> action, IDbErrorClassifier? classifier = null )
 20    {
 34021        ArgumentNullException.ThrowIfNull( action );
 22
 34023        try { return action(); }
 024        catch ( OperationCanceledException ) { throw; }
 425        catch ( DbUpdateConcurrencyException ex ) { return ex.Classify(); }
 7226        catch ( Exception ex ) { return classifier?.Classify( ex ) ?? Error.Unknown( ex ); }
 34027    }
 28
 29    /// <summary>Async version of <see cref="ExecuteSafe{T}(Func{T}, IDbErrorClassifier)"/>.</summary>
 30    /// <typeparam name="T">The result type.</typeparam>
 31    /// <param name="action">The database call, e.g. <c>() =&gt; dbContext.SaveChangesAsync( cancellationToken )</c>.</p
 32    /// <param name="classifier">The provider classifier (<c>dbContext.GetDbErrorClassifier()</c>); <see langword="null"
 33    /// <returns>
 34    /// The result on success; otherwise <c>ConcurrencyException</c>, the classifier's error, or <see cref="Error.Unknow
 35    /// </returns>
 36    /// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception>
 37    /// <exception cref="OperationCanceledException">The call was cancelled.</exception>
 38    public static async Task<LayerResponse<T>> ExecuteSafeAsync< T >( Func<Task<T>> action, IDbErrorClassifier? classifi
 39    {
 25240        ArgumentNullException.ThrowIfNull( action );
 41
 25242        try { return await action().ConfigureAwait( false ); }
 843        catch ( OperationCanceledException ) { throw; }
 044        catch ( DbUpdateConcurrencyException ex ) { return ex.Classify(); }
 13645        catch ( Exception ex ) { return classifier?.Classify( ex ) ?? Error.Unknown( ex ); }
 24846    }
 47}