< Summary

Information
Class: Elsa.Http.Handlers.DefaultHttpEndpointFaultHandler
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Handlers/DefaultHttpEndpointFaultHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 44
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
HandleAsync(...)0%2040%
GetIsTimeoutFault(...)100%210%
GetIsBadRequestFault(...)100%210%
ContainsException(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Handlers/DefaultHttpEndpointFaultHandler.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Http;
 2using Elsa.Http.Exceptions;
 3
 4namespace Elsa.Http.Handlers;
 5
 6/// <summary>
 7/// A default fault handler that writes information about the fault to the <see cref="HttpResponse"/>.
 8/// </summary>
 9public sealed class DefaultHttpEndpointFaultHandler : IHttpEndpointFaultHandler
 10{
 11    /// <inheritdoc />
 12    public ValueTask HandleAsync(HttpEndpointFaultContext context)
 13    {
 014        var httpContext = context.HttpContext;
 015        var isTimeoutIncident = GetIsTimeoutFault(context);
 016        var isBadRequest = GetIsBadRequestFault(context);
 017        var statusCode = isTimeoutIncident
 018            ? StatusCodes.Status408RequestTimeout
 019            : isBadRequest
 020                ? StatusCodes.Status400BadRequest
 021                : StatusCodes.Status500InternalServerError;
 22
 023        httpContext.Response.StatusCode = statusCode;
 024        return ValueTask.CompletedTask;
 25    }
 26
 27    private bool GetIsTimeoutFault(HttpEndpointFaultContext context)
 28    {
 029        return ContainsException(context, typeof(OperationCanceledException), typeof(TaskCanceledException), typeof(Time
 30    }
 31
 32    private bool GetIsBadRequestFault(HttpEndpointFaultContext context)
 33    {
 034        return ContainsException(context, typeof(HttpBadRequestException));
 35    }
 36
 37    private bool ContainsException(HttpEndpointFaultContext context, params Type[] exceptionTypes)
 38    {
 039        var workflowState = context.WorkflowState;
 040        var timeoutIncident = workflowState.Incidents.FirstOrDefault(x => exceptionTypes.Contains(x.Exception?.Type));
 41
 042        return timeoutIncident != null;
 43    }
 44}