UseExceptionHandler throws if ExceptionHandlingPath not set (#417)

This commit is contained in:
Jass Bagga 2017-10-27 15:34:34 -07:00 committed by GitHub
parent a30befae0f
commit 88db534e42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 4 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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>

View File

@ -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);
}
}
}