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

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%22100%
WithHandler()100%44100%
Build()100%44100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.APIExceptionHandlers/Registrations/ExceptionHandlersBuilder.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.DependencyInjection.Extensions;
 3
 4namespace ZhEaIsNsAaBn.Utilities.APIExceptionHandlers;
 5
 6/// <summary>Records the extra handlers; <see cref="Build"/> registers everything in the fixed order.</summary>
 7internal sealed class ExceptionHandlersBuilder : IExceptionHandlersBuilder
 8{
 9    // Registered first, in this order, by every Build(); the fallback always goes last.
 710    private static readonly Type[] BuiltIns =
 711        [ typeof( JsonExceptionHandler ), typeof( ValidationExceptionHandler ), typeof( BusinessExceptionHandler ), type
 12
 11613    private readonly List<Action<IServiceCollection>> _extras = [];
 11614    private readonly HashSet<Type>                    _types  = [ ..BuiltIns, typeof( FallbackExceptionHandler ) ];
 15    private readonly IServiceCollection               _services;
 16    private          bool                             _built;
 17
 18
 11619    internal ExceptionHandlersBuilder( IServiceCollection services )
 20    {
 11621        ArgumentNullException.ThrowIfNull( services );
 22
 11623        _services = services;
 11624    }
 25
 26
 27    public IExceptionHandlersBuilder WithHandler< THandler >()
 28        where THandler : class, IExceptionHandler
 29    {
 30        // After Build() the fallback is already registered; a late handler would run after it, i.e. never.
 9831        if ( _built ) { throw new InvalidOperationException( $"{typeof( THandler ).Name} was recorded after Build(); rec
 32
 16833        if ( _types.Add( typeof( THandler ) ) ) { _extras.Add( services => services.AddExceptionHandler<THandler>() ); }
 34
 9035        return this;
 36    }
 37
 38    public IServiceCollection Build()
 39    {
 12040        if ( _built ) { return _services; }
 41
 11242        _built = true;
 43
 44
 11245        _services.AddAPIResponses();
 46
 11247        _services.AddExceptionHandler<JsonExceptionHandler>();
 11248        _services.AddExceptionHandler<ValidationExceptionHandler>();
 11249        _services.AddExceptionHandler<BusinessExceptionHandler>();
 11250        _services.AddExceptionHandler<ExternalServiceExceptionHandler>();
 51
 43452        foreach ( var extra in _extras ) { extra( _services ); }
 53
 11254        _services.AddExceptionHandler<FallbackExceptionHandler>();
 55
 56
 57        // Before AddProblemDetails, so it is asked ahead of the default writer.
 11258        _services.TryAddEnumerable( ServiceDescriptor.Singleton<IProblemDetailsWriter, BaseResponseProblemDetailsWriter>
 11259        _services.AddProblemDetails();
 60
 61
 11262        return _services;
 63    }
 64}