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>
|
2022-03-09 23:19:00 +00:00
|
|
|
public class DefaultHttpEndpointWorkflowFaultHandler : IHttpEndpointWorkflowFaultHandler
|
|
|
|
|
{
|
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>
|
|
|
|
|
/// Initializes a new instance of the <see cref="DefaultHttpEndpointWorkflowFaultHandler"/> class.
|
|
|
|
|
/// </summary>
|
2023-04-16 22:11:58 +00:00
|
|
|
public DefaultHttpEndpointWorkflowFaultHandler(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 />
|
2022-03-09 23:19:00 +00:00
|
|
|
public virtual async ValueTask HandleAsync(HttpEndpointFaultedWorkflowContext context)
|
|
|
|
|
{
|
|
|
|
|
var httpContext = context.HttpContext;
|
|
|
|
|
var workflowInstance = context.WorkflowInstance;
|
2023-01-03 18:15:05 +00:00
|
|
|
var fault = workflowInstance.WorkflowState.Fault!;
|
2022-03-09 23:19:00 +00:00
|
|
|
|
|
|
|
|
httpContext.Response.ContentType = MediaTypeNames.Application.Json;
|
|
|
|
|
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
|
|
|
|
|
2023-04-16 22:11:58 +00:00
|
|
|
var faultedResponse = _apiSerializer.Serialize(new
|
2022-03-09 23:19:00 +00:00
|
|
|
{
|
2023-01-03 18:15:05 +00:00
|
|
|
errorMessage = $"Workflow faulted at {workflowInstance.FaultedAt!} with error: {fault.Message}",
|
2023-03-02 09:27:11 +00:00
|
|
|
exception = fault?.Exception,
|
2022-03-09 23:19:00 +00:00
|
|
|
workflow = new
|
|
|
|
|
{
|
|
|
|
|
name = workflowInstance.Name,
|
|
|
|
|
version = workflowInstance.Version,
|
|
|
|
|
instanceId = workflowInstance.Id
|
|
|
|
|
}
|
2023-04-16 22:11:58 +00:00
|
|
|
});
|
2022-03-09 23:19:00 +00:00
|
|
|
|
|
|
|
|
await httpContext.Response.WriteAsync(faultedResponse, context.CancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|