| | | 1 | | using System.Threading.Tasks; |
| | | 2 | | using Microsoft.AspNetCore.Http; |
| | | 3 | | using Microsoft.AspNetCore.Http.Features; |
| | | 4 | | using SampleDotNetCore2RestStub.Exceptions; |
| | | 5 | | |
| | | 6 | | namespace SampleDotNetCore2RestStub.Middleware |
| | | 7 | | { |
| | | 8 | | public class HttpExceptionMiddleware |
| | | 9 | | { |
| | | 10 | | private readonly RequestDelegate _next; |
| | | 11 | | |
| | 2 | 12 | | public HttpExceptionMiddleware(RequestDelegate next) |
| | 2 | 13 | | { |
| | 2 | 14 | | _next = next; |
| | 2 | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task Invoke(HttpContext context) |
| | 2 | 18 | | { |
| | | 19 | | try |
| | 2 | 20 | | { |
| | 2 | 21 | | await _next.Invoke(context); |
| | 2 | 22 | | } |
| | 0 | 23 | | catch (HttpException httpException) |
| | 0 | 24 | | { |
| | 0 | 25 | | context.Response.StatusCode = httpException.StatusCode; |
| | 0 | 26 | | var feature = context.Features.Get<IHttpResponseFeature>(); |
| | 0 | 27 | | feature.ReasonPhrase = httpException.Message; |
| | | 28 | | } |
| | 2 | 29 | | } |
| | | 30 | | } |
| | | 31 | | } |