< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

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

#LineLine coverage
 1using System.Net.Mime;
 2using Elsa.Workflows;
 3using Microsoft.AspNetCore.Http;
 4
 5namespace Elsa.Http.Handlers;
 6
 7/// <summary>
 8/// A fault handler that writes detailed information about the fault to the <see cref="HttpResponse"/>.
 9/// </summary>
 10public sealed class DetailedHttpEndpointFaultHandler : IHttpEndpointFaultHandler
 11{
 12    private readonly IApiSerializer _apiSerializer;
 13
 14    /// <summary>
 15    /// Initializes a new instance of the <see cref="DefaultHttpEndpointFaultHandler"/> class.
 16    /// </summary>
 017    public DetailedHttpEndpointFaultHandler(IApiSerializer apiSerializer)
 18    {
 019        _apiSerializer = apiSerializer;
 020    }
 21
 22    /// <inheritdoc />
 23    public async ValueTask HandleAsync(HttpEndpointFaultContext context)
 24    {
 025        var httpContext = context.HttpContext;
 026        var workflowState = context.WorkflowState;
 027        var isTimeoutIncident = GetIsTimeoutFault(context);
 028        var statusCode = isTimeoutIncident ? StatusCodes.Status408RequestTimeout : StatusCodes.Status500InternalServerEr
 29
 030        httpContext.Response.ContentType = MediaTypeNames.Application.Json;
 031        httpContext.Response.StatusCode = statusCode;
 32
 033        var faultedResponse = _apiSerializer.Serialize(workflowState);
 034        await httpContext.Response.WriteAsync(faultedResponse, context.CancellationToken);
 035    }
 36
 37    private bool GetIsTimeoutFault(HttpEndpointFaultContext context)
 38    {
 039        var workflowState = context.WorkflowState;
 040        var exceptionTypes = new[] { typeof(OperationCanceledException), typeof(TaskCanceledException), typeof(TimeoutEx
 041        var timeoutIncident = workflowState.Incidents.FirstOrDefault(x => exceptionTypes.Contains(x.Exception?.Type));
 42
 043        return timeoutIncident != null;
 44    }
 45}