< Summary

Information
Class: Api.Middleware.GlobalExceptionHandler
Assembly: Api
File(s): /home/runner/work/Northwind-Api/Northwind-Api/src/Api/Middleware/GlobalExceptionHandler.cs
Line coverage
26%
Covered lines: 6
Uncovered lines: 17
Coverable lines: 23
Total lines: 42
Line coverage: 26%
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%11100%
TryHandleAsync()100%210%

File(s)

/home/runner/work/Northwind-Api/Northwind-Api/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
 210  public GlobalExceptionHandler(
 211      ILogger<GlobalExceptionHandler> logger,
 412      IProblemDetailsService problemDetailsService) {
 213    _logger = logger;
 214    _problemDetailsService = problemDetailsService;
 215  }
 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    var handled = await _problemDetailsService.TryWriteAsync(
 033        new ProblemDetailsContext {
 034          HttpContext = httpContext,
 035          ProblemDetails = pd
 036        });
 37
 038    _logger.LogError(exception, "Unhandled exception.");
 39
 040    return handled;
 041  }
 42}