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

Information
Class: ZhEaIsNsAaBn.Utilities.APIExceptionHandlers.EntityFrameworkCore.DatabaseExceptionHandler
Assembly: ZhEaIsNsAaBn.Utilities.APIExceptionHandlers.EntityFrameworkCore
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.APIExceptionHandlers.EntityFrameworkCore/DatabaseExceptionHandler.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
TryHandleAsync()100%22100%
Classify(...)100%66100%
ClassifyWithContext(...)100%44100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.APIExceptionHandlers.EntityFrameworkCore/DatabaseExceptionHandler.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.Utilities.APIExceptionHandlers.EntityFrameworkCore;
 2
 3/// <summary>
 4/// Handles EF Core save failures with the same <see cref="Error"/> a repository returns, so the status is the same too 
 5/// unique / concurrency 409, other <c>DatabaseError</c> 500):
 6/// <list type="bullet">
 7/// <item><see cref="DbUpdateConcurrencyException"/> → Core's <c>Classify()</c> (<c>ConcurrencyException</c>).</item>
 8/// <item><see cref="DbUpdateException"/> → the failing context's <c>IDbErrorClassifier</c> (from <c>Entries[0].Context<
 9/// by <c>WithSqlServer()</c>); <see cref="Error.Unknown"/> when there is no entry, no classifier, the classifier doesn'
 10/// the context is already disposed.</item>
 11/// </list>
 12/// Other exceptions — a bare provider exception such as <c>SqlException</c> included — are left to the next handler (th
 13/// </summary>
 14public sealed class DatabaseExceptionHandler : BaseExceptionHandler<DatabaseExceptionHandler>, IExceptionHandler
 15{
 16    /// <summary>Creates the handler.</summary>
 17    /// <param name="logger">The logger.</param>
 18    /// <param name="environment">The host environment.</param>
 19    /// <param name="settings">The status mapping.</param>
 20    public DatabaseExceptionHandler( ILogger<DatabaseExceptionHandler> logger, IWebHostEnvironment environment, IOptions
 6021        : base( logger, environment, settings ) { }
 22
 23
 24    /// <inheritdoc />
 25    public async ValueTask<bool> TryHandleAsync( HttpContext httpContext, Exception exception, CancellationToken cancell
 26    {
 3027        ArgumentNullException.ThrowIfNull( httpContext );
 28
 3029        var error = Classify( exception );
 30
 3931        if ( error is null ) { return false; }
 32
 33
 2134        Log.DatabaseError( Logger, exception, error.Code );
 35
 36
 2137        await GenerateError( httpContext, error, cancellationToken ).ConfigureAwait( false );
 38
 2139        return true;
 3040    }
 41
 42
 43    // The error for an EF Core save failure, or null when the exception isn't one.
 44    private static Error? Classify( Exception exception )
 3045        => exception switch
 3046               {
 647                   DbUpdateConcurrencyException concurrency => concurrency.Classify(),
 1548                   DbUpdateException update                 => ClassifyWithContext( update ) ?? Error.Unknown( update ),
 949                   _                                        => null
 3050               };
 51
 52    private static Error? ClassifyWithContext( DbUpdateException exception )
 53    {
 1854        if ( exception.Entries.Count == 0 ) { return null; }
 55
 56
 1257        try { return exception.Entries[ 0 ].Context.GetDbErrorClassifier()?.Classify( exception ); }
 358        catch ( ObjectDisposedException )
 59        {
 60            // e.g. `using var context = factory.CreateDbContext()` already left: its services are gone.
 361            return null;
 62        }
 1263    }
 64}