UseExceptionHandler throws if ExceptionHandlingPath not set (#417)
This commit is contained in:
parent
a30befae0f
commit
88db534e42
|
|
@ -30,12 +30,19 @@ namespace Microsoft.AspNetCore.Diagnostics
|
|||
_next = next;
|
||||
_options = options.Value;
|
||||
_logger = loggerFactory.CreateLogger<ExceptionHandlerMiddleware>();
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -584,6 +584,20 @@ namespace Microsoft.AspNetCore.Diagnostics
|
|||
internal static string FormatRuntimeInfoPage_Environment()
|
||||
=> GetString("RuntimeInfoPage_Environment");
|
||||
|
||||
/// <summary>
|
||||
/// An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' option must be set in 'UseExceptionHandler()'.
|
||||
/// </summary>
|
||||
internal static string ExceptionHandlerOptions_NotConfiguredCorrectly
|
||||
{
|
||||
get => GetString("ExceptionHandlerOptions_NotConfiguredCorrectly");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' option must be set in 'UseExceptionHandler()'.
|
||||
/// </summary>
|
||||
internal static string FormatExceptionHandlerOptions_NotConfiguredCorrectly()
|
||||
=> GetString("ExceptionHandlerOptions_NotConfiguredCorrectly");
|
||||
|
||||
private static string GetString(string name, params string[] formatterNames)
|
||||
{
|
||||
var value = _resourceManager.GetString(name);
|
||||
|
|
|
|||
|
|
@ -247,4 +247,7 @@
|
|||
<data name="RuntimeInfoPage_Environment" xml:space="preserve">
|
||||
<value>Environment:</value>
|
||||
</data>
|
||||
<data name="ExceptionHandlerOptions_NotConfiguredCorrectly" xml:space="preserve">
|
||||
<value>An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' option must be set in 'UseExceptionHandler()'.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
@ -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<DiagnosticListener>();
|
||||
app.UseExceptionHandler();
|
||||
});
|
||||
|
||||
// Act
|
||||
var exception = Assert.Throws<InvalidOperationException>(() => 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue