< Summary

Information
Class: Elsa.Bpmn.Interchange.Endpoints.Bpmn.BpmnExportErrorResponses
Assembly: Elsa.Bpmn.Interchange
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Bpmn.Interchange/Endpoints/Bpmn/BpmnExportErrorResponses.cs
Line coverage
76%
Covered lines: 13
Uncovered lines: 4
Coverable lines: 17
Total lines: 63
Line coverage: 76.4%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RunAsync()100%1166.66%
ResponseFor(...)75%4487.5%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Bpmn.Interchange/Endpoints/Bpmn/BpmnExportErrorResponses.cs

#LineLine coverage
 1using Bpmn.Interchange;
 2using Elsa.Bpmn.Interchange.Exceptions;
 3using Microsoft.AspNetCore.Http;
 4
 5namespace Elsa.Bpmn.Interchange.Endpoints.Bpmn;
 6
 7/// <summary>
 8/// The exception-to-response mapping shared by every endpoint that reads a workflow definition's stored BPMN source
 9/// back out through <c>BpmnInterchangeDocumentService</c> — <c>Export</c> and the document <c>Get</c> endpoint —
 10/// refusing the same "missing" and "stale" cases the same way.
 11/// </summary>
 12internal static class BpmnExportErrorResponses
 13{
 14    /// <summary>
 15    /// Runs <paramref name="sendResponse"/>, reporting the shared error response for whichever exception it throws.
 16    /// A <see cref="BpmnExportUnavailableException"/> goes out through <paramref name="httpResponse"/> as a
 17    /// <see cref="BpmnErrorResponse"/> coded from its <see cref="BpmnExportUnavailableException.Reason"/>; the
 18    /// remaining, uncoded <see cref="BpmnInterchangeException"/> case still goes through <paramref name="addError"/>
 19    /// and <paramref name="sendErrorsAsync"/>, exactly as before.
 20    /// </summary>
 21    public static async Task RunAsync(
 22        Func<Task> sendResponse,
 23        HttpResponse httpResponse,
 24        Action<string> addError,
 25        Func<int, CancellationToken, Task> sendErrorsAsync,
 26        CancellationToken cancellationToken)
 27    {
 28        try
 29        {
 3830            await sendResponse();
 3431        }
 432        catch (BpmnExportUnavailableException exception)
 33        {
 434            await BpmnErrorResponse.SendAsync(httpResponse, ResponseFor(exception), cancellationToken);
 435        }
 036        catch (BpmnInterchangeException exception)
 37        {
 038            addError(exception.Message);
 039            await sendErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken);
 40        }
 3841    }
 42
 43    /// <summary>
 44    /// The response for <paramref name="exception"/>, coded from its <see cref="BpmnExportUnavailableException.Reason"/
 45    /// as its own pure, synchronous step so it can be asserted on directly.
 46    /// <see cref="BpmnExportUnavailableReason.SourceVersionUnknown"/> in particular is not reachable through
 47    /// <c>Import</c> or the document <c>PUT</c> themselves — both always record a source version alongside the
 48    /// source text — only through custom properties edited or migrated some other way, so an HTTP-level test cannot
 49    /// reach it either.
 50    /// </summary>
 51    internal static BpmnErrorResponse ResponseFor(BpmnExportUnavailableException exception)
 52    {
 753        var code = exception.Reason switch
 754        {
 355            BpmnExportUnavailableReason.NotImported => BpmnErrorCodes.ExportNotImported,
 156            BpmnExportUnavailableReason.SourceVersionUnknown => BpmnErrorCodes.ExportSourceVersionUnknown,
 357            BpmnExportUnavailableReason.SourceStale => BpmnErrorCodes.ExportSourceStale,
 058            _ => throw new ArgumentOutOfRangeException(nameof(exception), exception.Reason, "Unknown BpmnExportUnavailab
 759        };
 60
 761        return BpmnErrorResponse.Create(exception.Message, code, StatusCodes.Status422UnprocessableEntity);
 62    }
 63}