< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 42
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
TryHandleAsync()100%210%

File(s)

/home/runner/work/CleanArchitectureTemplate/CleanArchitectureTemplate/src/Api/middleware/GlobalExceptionHandler.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Diagnostics;
 2using Microsoft.AspNetCore.Mvc;
 3
 4namespace Api.Middleware;
 5
 6public sealed class GlobalExceptionHandler : IExceptionHandler {
 7  private readonly ILogger<GlobalExceptionHandler> _logger;
 8  private readonly IProblemDetailsService _problemDetailsService;
 9
 010  public GlobalExceptionHandler(
 011      ILogger<GlobalExceptionHandler> logger,
 012      IProblemDetailsService problemDetailsService) {
 013    _logger = logger;
 014    _problemDetailsService = problemDetailsService;
 015  }
 16
 17  public async ValueTask<bool> TryHandleAsync(
 18      HttpContext httpContext,
 19      Exception exception,
 020      CancellationToken ct) {
 21
 022    var pd = new ProblemDetails {
 023      Status = StatusCodes.Status500InternalServerError,
 024      Title = "An unexpected error occurred.",
 025      Detail = exception.Message,
 026      Type = "server_error",
 027      Instance = httpContext.Request.Path
 028    };
 29
 030    httpContext.Response.StatusCode = pd.Status!.Value;
 31
 032    bool handled = await _problemDetailsService.TryWriteAsync(
 033        new ProblemDetailsContext {
 034          HttpContext = httpContext,
 035          ProblemDetails = pd
 036        });
 37
 038    _logger.LogError(exception, "Unhandled exception translated to ProblemDetails");
 39
 040    return handled;
 041  }
 42}