| | | 1 | | using System; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | #if !USE_ASPNETCORE |
| | | 5 | | using Microsoft.Owin; |
| | | 6 | | using IContext = Microsoft.Owin.IOwinContext; |
| | | 7 | | using OwinMiddleware = Microsoft.Owin.OwinMiddleware; |
| | | 8 | | using Next = Microsoft.Owin.OwinMiddleware; |
| | | 9 | | #else |
| | | 10 | | using OwinMiddleware = System.Object; |
| | | 11 | | using IContext = Microsoft.AspNetCore.Http.HttpContext; |
| | | 12 | | using Next = Microsoft.AspNetCore.Http.RequestDelegate; |
| | | 13 | | #endif |
| | | 14 | | using WireMock.Owin.Mappers; |
| | | 15 | | using WireMock.Validation; |
| | | 16 | | |
| | | 17 | | namespace WireMock.Owin |
| | | 18 | | { |
| | | 19 | | internal class GlobalExceptionMiddleware : OwinMiddleware |
| | | 20 | | { |
| | | 21 | | private readonly IWireMockMiddlewareOptions _options; |
| | | 22 | | private readonly IOwinResponseMapper _responseMapper; |
| | | 23 | | |
| | | 24 | | #if !USE_ASPNETCORE |
| | | 25 | | public GlobalExceptionMiddleware(Next next, IWireMockMiddlewareOptions options, IOwinResponseMapper responseMapp |
| | | 26 | | { |
| | | 27 | | Check.NotNull(options, nameof(options)); |
| | | 28 | | Check.NotNull(responseMapper, nameof(responseMapper)); |
| | | 29 | | |
| | | 30 | | _options = options; |
| | | 31 | | _responseMapper = responseMapper; |
| | | 32 | | } |
| | | 33 | | #else |
| | 48 | 34 | | public GlobalExceptionMiddleware(Next next, IWireMockMiddlewareOptions options, IOwinResponseMapper responseMapp |
| | 48 | 35 | | { |
| | 48 | 36 | | Check.NotNull(options, nameof(options)); |
| | 48 | 37 | | Check.NotNull(responseMapper, nameof(responseMapper)); |
| | | 38 | | |
| | 48 | 39 | | Next = next; |
| | 48 | 40 | | _options = options; |
| | 48 | 41 | | _responseMapper = responseMapper; |
| | 48 | 42 | | } |
| | | 43 | | #endif |
| | | 44 | | |
| | | 45 | | #if USE_ASPNETCORE |
| | 46 | 46 | | public Next Next { get; } |
| | | 47 | | #endif |
| | | 48 | | |
| | | 49 | | #if !USE_ASPNETCORE |
| | | 50 | | public override Task Invoke(IContext ctx) |
| | | 51 | | #else |
| | | 52 | | public Task Invoke(IContext ctx) |
| | | 53 | | #endif |
| | 46 | 54 | | { |
| | 46 | 55 | | return InvokeInternal(ctx); |
| | 45 | 56 | | } |
| | | 57 | | |
| | | 58 | | private async Task InvokeInternal(IContext ctx) |
| | 46 | 59 | | { |
| | | 60 | | try |
| | 46 | 61 | | { |
| | 46 | 62 | | await Next?.Invoke(ctx); |
| | 46 | 63 | | } |
| | 0 | 64 | | catch (Exception ex) |
| | 0 | 65 | | { |
| | 0 | 66 | | _options.Logger.Error("HttpStatusCode set to 500 {0}", ex); |
| | 0 | 67 | | await _responseMapper.MapAsync(ResponseMessageBuilder.Create(JsonConvert.SerializeObject(ex), 500), ctx. |
| | 0 | 68 | | } |
| | 46 | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |