diff --git a/src/Microsoft.AspNetCore.Diagnostics/ExceptionHandler/ExceptionHandlerMiddleware.cs b/src/Microsoft.AspNetCore.Diagnostics/ExceptionHandler/ExceptionHandlerMiddleware.cs index 49e8b0a981..814c3991e8 100644 --- a/src/Microsoft.AspNetCore.Diagnostics/ExceptionHandler/ExceptionHandlerMiddleware.cs +++ b/src/Microsoft.AspNetCore.Diagnostics/ExceptionHandler/ExceptionHandlerMiddleware.cs @@ -30,12 +30,19 @@ namespace Microsoft.AspNetCore.Diagnostics _next = next; _options = options.Value; _logger = loggerFactory.CreateLogger(); - if (_options.ExceptionHandler == null) - { - _options.ExceptionHandler = _next; - } _clearCacheHeadersDelegate = ClearCacheHeaders; _diagnosticSource = diagnosticSource; + if (_options.ExceptionHandler == null) + { + if (_options.ExceptionHandlingPath == null) + { + throw new InvalidOperationException(Resources.FormatExceptionHandlerOptions_NotConfiguredCorrectly()); + } + else + { + _options.ExceptionHandler = _next; + } + } } public async Task Invoke(HttpContext context) diff --git a/src/Microsoft.AspNetCore.Diagnostics/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Diagnostics/Properties/Resources.Designer.cs index ae107bc3cf..a9ca25fef3 100644 --- a/src/Microsoft.AspNetCore.Diagnostics/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNetCore.Diagnostics/Properties/Resources.Designer.cs @@ -584,6 +584,20 @@ namespace Microsoft.AspNetCore.Diagnostics internal static string FormatRuntimeInfoPage_Environment() => GetString("RuntimeInfoPage_Environment"); + /// + /// An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' option must be set in 'UseExceptionHandler()'. + /// + internal static string ExceptionHandlerOptions_NotConfiguredCorrectly + { + get => GetString("ExceptionHandlerOptions_NotConfiguredCorrectly"); + } + + /// + /// An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' option must be set in 'UseExceptionHandler()'. + /// + internal static string FormatExceptionHandlerOptions_NotConfiguredCorrectly() + => GetString("ExceptionHandlerOptions_NotConfiguredCorrectly"); + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name); diff --git a/src/Microsoft.AspNetCore.Diagnostics/Resources.resx b/src/Microsoft.AspNetCore.Diagnostics/Resources.resx index 4c430b2b36..93a94abe1a 100644 --- a/src/Microsoft.AspNetCore.Diagnostics/Resources.resx +++ b/src/Microsoft.AspNetCore.Diagnostics/Resources.resx @@ -247,4 +247,7 @@ Environment: + + An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' option must be set in 'UseExceptionHandler()'. + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Diagnostics.Tests/ExceptionHandlerTest.cs b/test/Microsoft.AspNetCore.Diagnostics.Tests/ExceptionHandlerTest.cs index 87f7c87f8a..0ee748add7 100644 --- a/test/Microsoft.AspNetCore.Diagnostics.Tests/ExceptionHandlerTest.cs +++ b/test/Microsoft.AspNetCore.Diagnostics.Tests/ExceptionHandlerTest.cs @@ -494,5 +494,27 @@ namespace Microsoft.AspNetCore.Diagnostics Assert.NotNull(listener.DiagnosticHandledException?.HttpContext); Assert.NotNull(listener.DiagnosticHandledException?.Exception); } + + [Fact] + public void UsingExceptionHandler_ThrowsAnException_WhenExceptionHandlingPathNotSet() + { + // Arrange + DiagnosticListener diagnosticListener = null; + + var builder = new WebHostBuilder() + .Configure(app => + { + diagnosticListener = app.ApplicationServices.GetRequiredService(); + app.UseExceptionHandler(); + }); + + // Act + var exception = Assert.Throws(() => new TestServer(builder)); + + // Assert + Assert.Equal($"An error occurred when configuring the exception handler middleware. " + + $"Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' option must be set in 'UseExceptionHandler()'.", + exception.Message); + } } }