elsa-core/src/modules/Elsa.Http/Handlers/DefaultHttpEndpointFaultHandler.cs
Sipke Schoorstra a5d13e4e9b
Enhance HTTP Endpoint + Flowchart fix (#4469)
* Fix flowchart next activity scheduling bug

This fixes a bug where the Flowchart activity would schedule connected activities regardless of the outcome of the completed activity.

* Cancel activity execution when cancellation token is triggered.

* Enable user to handle HTTP endpoint validation failures as outcomes

* Don't respond with full details in case of HTTP Endpoint faults for security
2023-09-22 18:45:18 +02:00

33 lines
1.2 KiB
C#

using Elsa.Http.Models;
using Microsoft.AspNetCore.Http;
using System.Net.Mime;
using Elsa.Http.Contracts;
using Elsa.Workflows.Core.Contracts;
namespace Elsa.Http.Handlers;
/// <summary>
/// A default fault handler that writes information about the fault to the <see cref="HttpResponse"/>.
/// </summary>
public sealed class DefaultHttpEndpointFaultHandler : IHttpEndpointFaultHandler
{
/// <inheritdoc />
public ValueTask HandleAsync(HttpEndpointFaultContext context)
{
var httpContext = context.HttpContext;
var isTimeoutIncident = GetIsTimeoutFault(context);
var statusCode = isTimeoutIncident ? StatusCodes.Status408RequestTimeout : StatusCodes.Status500InternalServerError;
httpContext.Response.StatusCode = statusCode;
return ValueTask.CompletedTask;
}
private bool GetIsTimeoutFault(HttpEndpointFaultContext context)
{
var workflowState = context.WorkflowState;
var exceptionTypes = new[] { typeof(OperationCanceledException), typeof(TaskCanceledException), typeof(TimeoutException) };
var timeoutIncident = workflowState.Incidents.FirstOrDefault(x => exceptionTypes.Contains(x.Exception?.Type));
return timeoutIncident != null;
}
}