| | | 1 | | using Microsoft.AspNetCore.Http; |
| | | 2 | | using Elsa.Http.Exceptions; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Http.Handlers; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A default fault handler that writes information about the fault to the <see cref="HttpResponse"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class DefaultHttpEndpointFaultHandler : IHttpEndpointFaultHandler |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | | 12 | | public ValueTask HandleAsync(HttpEndpointFaultContext context) |
| | | 13 | | { |
| | 0 | 14 | | var httpContext = context.HttpContext; |
| | 0 | 15 | | var isTimeoutIncident = GetIsTimeoutFault(context); |
| | 0 | 16 | | var isBadRequest = GetIsBadRequestFault(context); |
| | 0 | 17 | | var statusCode = isTimeoutIncident |
| | 0 | 18 | | ? StatusCodes.Status408RequestTimeout |
| | 0 | 19 | | : isBadRequest |
| | 0 | 20 | | ? StatusCodes.Status400BadRequest |
| | 0 | 21 | | : StatusCodes.Status500InternalServerError; |
| | | 22 | | |
| | 0 | 23 | | httpContext.Response.StatusCode = statusCode; |
| | 0 | 24 | | return ValueTask.CompletedTask; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private bool GetIsTimeoutFault(HttpEndpointFaultContext context) |
| | | 28 | | { |
| | 0 | 29 | | return ContainsException(context, typeof(OperationCanceledException), typeof(TaskCanceledException), typeof(Time |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private bool GetIsBadRequestFault(HttpEndpointFaultContext context) |
| | | 33 | | { |
| | 0 | 34 | | return ContainsException(context, typeof(HttpBadRequestException)); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | private bool ContainsException(HttpEndpointFaultContext context, params Type[] exceptionTypes) |
| | | 38 | | { |
| | 0 | 39 | | var workflowState = context.WorkflowState; |
| | 0 | 40 | | var timeoutIncident = workflowState.Incidents.FirstOrDefault(x => exceptionTypes.Contains(x.Exception?.Type)); |
| | | 41 | | |
| | 0 | 42 | | return timeoutIncident != null; |
| | | 43 | | } |
| | | 44 | | } |