| | | 1 | | using Bpmn.Interchange; |
| | | 2 | | using Elsa.Bpmn.Interchange.Exceptions; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | |
| | | 5 | | namespace 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> |
| | | 12 | | internal 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 | | { |
| | 38 | 30 | | await sendResponse(); |
| | 34 | 31 | | } |
| | 4 | 32 | | catch (BpmnExportUnavailableException exception) |
| | | 33 | | { |
| | 4 | 34 | | await BpmnErrorResponse.SendAsync(httpResponse, ResponseFor(exception), cancellationToken); |
| | 4 | 35 | | } |
| | 0 | 36 | | catch (BpmnInterchangeException exception) |
| | | 37 | | { |
| | 0 | 38 | | addError(exception.Message); |
| | 0 | 39 | | await sendErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | | 40 | | } |
| | 38 | 41 | | } |
| | | 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 | | { |
| | 7 | 53 | | var code = exception.Reason switch |
| | 7 | 54 | | { |
| | 3 | 55 | | BpmnExportUnavailableReason.NotImported => BpmnErrorCodes.ExportNotImported, |
| | 1 | 56 | | BpmnExportUnavailableReason.SourceVersionUnknown => BpmnErrorCodes.ExportSourceVersionUnknown, |
| | 3 | 57 | | BpmnExportUnavailableReason.SourceStale => BpmnErrorCodes.ExportSourceStale, |
| | 0 | 58 | | _ => throw new ArgumentOutOfRangeException(nameof(exception), exception.Reason, "Unknown BpmnExportUnavailab |
| | 7 | 59 | | }; |
| | | 60 | | |
| | 7 | 61 | | return BpmnErrorResponse.Create(exception.Message, code, StatusCodes.Status422UnprocessableEntity); |
| | | 62 | | } |
| | | 63 | | } |