2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Http.Models;
|
2022-03-09 23:19:00 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-03-02 09:27:11 +00:00
|
|
|
using System.Net.Mime;
|
2023-03-03 21:47:23 +00:00
|
|
|
using Elsa.Http.Contracts;
|
2023-04-16 22:11:58 +00:00
|
|
|
using Elsa.Workflows.Core.Contracts;
|
2022-03-09 23:19:00 +00:00
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
namespace Elsa.Http.Handlers;
|
2022-03-09 23:19:00 +00:00
|
|
|
|
2022-04-27 20:32:40 +00:00
|
|
|
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
|
2023-01-03 18:15:05 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A default fault handler that writes information about the fault to the <see cref="HttpResponse"/>.
|
|
|
|
|
/// </summary>
|
2023-09-19 20:42:53 +00:00
|
|
|
public class DefaultHttpEndpointFaultHandler : IHttpEndpointFaultHandler
|
2022-03-09 23:19:00 +00:00
|
|
|
{
|
2023-04-16 22:11:58 +00:00
|
|
|
private readonly IApiSerializer _apiSerializer;
|
2023-03-02 09:27:11 +00:00
|
|
|
|
2023-03-07 12:45:58 +00:00
|
|
|
/// <summary>
|
2023-09-19 20:42:53 +00:00
|
|
|
/// Initializes a new instance of the <see cref="DefaultHttpEndpointFaultHandler"/> class.
|
2023-03-07 12:45:58 +00:00
|
|
|
/// </summary>
|
2023-09-19 20:42:53 +00:00
|
|
|
public DefaultHttpEndpointFaultHandler(IApiSerializer apiSerializer)
|
2023-03-02 09:27:11 +00:00
|
|
|
{
|
2023-04-16 22:11:58 +00:00
|
|
|
_apiSerializer = apiSerializer;
|
2023-03-02 09:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-03 18:15:05 +00:00
|
|
|
/// <inheritdoc />
|
2023-09-19 20:42:53 +00:00
|
|
|
public virtual async ValueTask HandleAsync(HttpEndpointFaultContext context)
|
2022-03-09 23:19:00 +00:00
|
|
|
{
|
|
|
|
|
var httpContext = context.HttpContext;
|
2023-04-20 18:45:27 +00:00
|
|
|
var workflowState = context.WorkflowState;
|
2023-09-19 20:42:53 +00:00
|
|
|
var isTimeoutIncident = GetIsTimeoutFault(context);
|
|
|
|
|
var statusCode = isTimeoutIncident ? StatusCodes.Status408RequestTimeout : StatusCodes.Status500InternalServerError;
|
2022-03-09 23:19:00 +00:00
|
|
|
|
|
|
|
|
httpContext.Response.ContentType = MediaTypeNames.Application.Json;
|
2023-09-19 20:42:53 +00:00
|
|
|
httpContext.Response.StatusCode = statusCode;
|
2022-03-09 23:19:00 +00:00
|
|
|
|
2023-09-14 08:50:02 +00:00
|
|
|
var faultedResponse = _apiSerializer.Serialize(workflowState);
|
2022-03-09 23:19:00 +00:00
|
|
|
await httpContext.Response.WriteAsync(faultedResponse, context.CancellationToken);
|
|
|
|
|
}
|
2023-09-19 20:42:53 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2022-03-09 23:19:00 +00:00
|
|
|
}
|