|
7 | 7 | using System.IO;
|
8 | 8 | using System.Linq;
|
9 | 9 | using System.Net;
|
10 |
| -using System.Text; |
11 | 10 | using System.Threading;
|
12 | 11 | using System.Threading.Tasks;
|
13 | 12 | using Microsoft.AspNetCore.Builder;
|
14 | 13 | using Microsoft.AspNetCore.Hosting;
|
15 | 14 | using Microsoft.AspNetCore.Http;
|
16 |
| -using Microsoft.AspNetCore.Http.Features; |
17 | 15 | using Microsoft.AspNetCore.TestHost;
|
18 | 16 | using Microsoft.AspNetCore.Testing;
|
19 | 17 | using Microsoft.Extensions.DependencyInjection;
|
20 | 18 | using Microsoft.Extensions.Hosting;
|
| 19 | +using Microsoft.Extensions.Logging; |
| 20 | +using Microsoft.Extensions.Logging.Testing; |
21 | 21 | using Xunit;
|
22 | 22 |
|
23 | 23 | namespace Microsoft.AspNetCore.Diagnostics
|
@@ -469,5 +469,79 @@ public void UsingExceptionHandler_ThrowsAnException_WhenExceptionHandlingPathNot
|
469 | 469 | "Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.AddExceptionHandler(options => { ... });'.",
|
470 | 470 | exception.Message);
|
471 | 471 | }
|
| 472 | + |
| 473 | + [Fact] |
| 474 | + public async Task ExceptionHandlerNotFound_RethrowsOriginalError() |
| 475 | + { |
| 476 | + var sink = new TestSink(TestSink.EnableWithTypeName<ExceptionHandlerMiddleware>); |
| 477 | + var loggerFactory = new TestLoggerFactory(sink, enabled: true); |
| 478 | + |
| 479 | + using var host = new HostBuilder() |
| 480 | + .ConfigureWebHost(webHostBuilder => |
| 481 | + { |
| 482 | + webHostBuilder |
| 483 | + .UseTestServer() |
| 484 | + .ConfigureServices(services => |
| 485 | + { |
| 486 | + services.AddSingleton<ILoggerFactory>(loggerFactory); |
| 487 | + }) |
| 488 | + .Configure(app => |
| 489 | + { |
| 490 | + app.Use(async (httpContext, next) => |
| 491 | + { |
| 492 | + Exception exception = null; |
| 493 | + try |
| 494 | + { |
| 495 | + await next(); |
| 496 | + } |
| 497 | + catch (InvalidOperationException ex) |
| 498 | + { |
| 499 | + exception = ex; |
| 500 | + |
| 501 | + // This mimics what the server would do when an exception occurs |
| 502 | + httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; |
| 503 | + } |
| 504 | + |
| 505 | + // The original exception is thrown |
| 506 | + Assert.NotNull(exception); |
| 507 | + Assert.Equal("Something bad happened.", exception.Message); |
| 508 | + |
| 509 | + }); |
| 510 | + |
| 511 | + app.UseExceptionHandler("/non-existent-hander"); |
| 512 | + |
| 513 | + app.Map("/handle-errors", (innerAppBuilder) => |
| 514 | + { |
| 515 | + innerAppBuilder.Run(async (httpContext) => |
| 516 | + { |
| 517 | + await httpContext.Response.WriteAsync("Handled error in a custom way."); |
| 518 | + }); |
| 519 | + }); |
| 520 | + |
| 521 | + app.Map("/throw", (innerAppBuilder) => |
| 522 | + { |
| 523 | + innerAppBuilder.Run(httpContext => |
| 524 | + { |
| 525 | + throw new InvalidOperationException("Something bad happened."); |
| 526 | + }); |
| 527 | + }); |
| 528 | + }); |
| 529 | + }).Build(); |
| 530 | + |
| 531 | + await host.StartAsync(); |
| 532 | + |
| 533 | + using (var server = host.GetTestServer()) |
| 534 | + { |
| 535 | + var client = server.CreateClient(); |
| 536 | + var response = await client.GetAsync("throw"); |
| 537 | + Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); |
| 538 | + Assert.Equal(string.Empty, await response.Content.ReadAsStringAsync()); |
| 539 | + } |
| 540 | + |
| 541 | + Assert.Contains(sink.Writes, w => |
| 542 | + w.LogLevel == LogLevel.Warning |
| 543 | + && w.EventId == 4 |
| 544 | + && w.Message == "No exception handler was found, rethrowing original exception."); |
| 545 | + } |
472 | 546 | }
|
473 | 547 | }
|
0 commit comments