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

Information
Class: ZhEaIsNsAaBn.Utilities.APIExceptionHandlers.BaseResponseProblemDetailsWriter
Assembly: ZhEaIsNsAaBn.Utilities.APIExceptionHandlers
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.APIExceptionHandlers/GlobalHandlers/BaseResponseProblemDetailsWriter.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 45
Line coverage: 100%
Branch coverage
70%
Covered branches: 14
Total branches: 20
Branch coverage: 70%
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%11100%
CanWrite(...)100%11100%
WriteAsync()70%2020100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.APIExceptionHandlers/GlobalHandlers/BaseResponseProblemDetailsWriter.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.Utilities.APIExceptionHandlers;
 2
 3/// <summary>
 4/// Writes problem-details responses (status-code pages, <c>Results.Problem</c>, …) as the <see cref="BaseResponse"/> en
 5/// response's own status, one error whose code is the status name (e.g. <c>NotFound</c>), and the trace and transaction
 6/// Registered ahead of the default writer by <c>AddCustomExceptionHandlers().Build()</c>.
 7/// </summary>
 8internal sealed class BaseResponseProblemDetailsWriter : IProblemDetailsWriter
 9{
 410    private static readonly JsonSerializerOptions Compact  = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, 
 411    private static readonly JsonSerializerOptions Indented = new( Compact ) { WriteIndented = true };
 12
 13    private readonly IWebHostEnvironment _environment;
 14
 15
 19216    public BaseResponseProblemDetailsWriter( IWebHostEnvironment environment ) => _environment = environment;
 17
 18
 419    public bool CanWrite( ProblemDetailsContext context ) => true;
 20
 21    public async ValueTask WriteAsync( ProblemDetailsContext context )
 22    {
 423        ArgumentNullException.ThrowIfNull( context );
 24
 425        var httpContext = context.HttpContext;
 426        var status      = context.ProblemDetails.Status ?? httpContext.Response.StatusCode;
 427        var code        = Enum.IsDefined( typeof( HttpStatusCode ), status ) ? ( (HttpStatusCode) status ).ToString() : 
 428        var message     = context.ProblemDetails.Title ?? context.ProblemDetails.Detail ?? $"The request failed with HTT
 29
 430        var response = new BaseResponse
 431                           {
 432                               Status  = status,
 433                               Errors  = [ new Error( code, "Http", $"فشل الطلب (رمز الحالة {status}).", message ) ],
 434                               TraceId = System.Diagnostics.Activity.Current?.TraceId.ToString() ?? httpContext.TraceIde
 435                               TransId = httpContext.Items[ "transId" ]?.ToString() ?? httpContext.Request.Headers[ "Tra
 436                           };
 37
 38
 439        httpContext.Response.StatusCode  = status;
 440        httpContext.Response.ContentType = "application/json";
 41
 42
 443        await httpContext.Response.WriteAsync( JsonSerializer.Serialize( response, _environment.IsDevelopment() ? Indent
 444    }
 45}