Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpful log message and exception when exception handler returns 404 #31142

Merged
merged 8 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Diagnostics
Expand All @@ -20,7 +21,9 @@ internal static class DiagnosticsLoggerExtensions
LoggerMessage.Define(LogLevel.Error, new EventId(3, "Exception"), "An exception was thrown attempting to execute the error handler.");

private static readonly Action<ILogger, Exception?> _errorHandlerNotFound =
LoggerMessage.Define(LogLevel.Warning, new EventId(4, "HandlerNotFound"), "No exception handler was found, rethrowing original exception.");
LoggerMessage.Define(LogLevel.Warning, new EventId(4, "HandlerNotFound"), "The exception handler configured on ExceptionHandlerOptions produced a 404 status response. " +
"An InvalidOperationException containing the original exception will be thrown since this is often due to a misconfigured ExceptionHandlerOptions.ExceptionHandlingPath. " +
"If the exception handler is expected to return 404 status responses then set ExceptionHandlerOptions.AllowStatusCode404Response to true.");

// DeveloperExceptionPageMiddleware
private static readonly Action<ILogger, Exception?> _responseStartedErrorPageMiddleware =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ private async Task HandleException(HttpContext context, ExceptionDispatchInfo ed
}

_logger.ErrorHandlerNotFound();

edi = ExceptionDispatchInfo.Capture(new InvalidOperationException($"No exception handler was found, see inner exception for details of original exception. If an exception should not be thrown for 404 responses, set {nameof(ExceptionHandlerOptions.AllowStatusCode404Response)} to true.", edi.SourceException));
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception ex2)
{
Expand All @@ -154,7 +156,7 @@ private async Task HandleException(HttpContext context, ExceptionDispatchInfo ed
context.Request.Path = originalPath;
}

edi.Throw(); // Re-throw the original if we couldn't handle it
edi.Throw(); // Re-throw wrapped exception or the original if we couldn't handle it
}

private static void ClearHttpContext(HttpContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public void UsingExceptionHandler_ThrowsAnException_WhenExceptionHandlingPathNot
}

[Fact]
public async Task ExceptionHandlerNotFound_RethrowsOriginalError()
public async Task ExceptionHandlerNotFound_ThrowsIOEWithOriginalError()
{
var sink = new TestSink(TestSink.EnableWithTypeName<ExceptionHandlerMiddleware>);
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
Expand Down Expand Up @@ -500,9 +500,14 @@ public async Task ExceptionHandlerNotFound_RethrowsOriginalError()
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
}

// The original exception is thrown
// Invalid operation exception
Assert.NotNull(exception);
Assert.Equal("Something bad happened.", exception.Message);
Assert.Equal("No exception handler was found, see inner exception for details of original exception. If an exception should not be thrown for 404 responses, set AllowStatusCode404Response to true.", exception.Message);
JunTaoLuo marked this conversation as resolved.
Show resolved Hide resolved

// The original exception is inner exception
Assert.NotNull(exception.InnerException);
Assert.IsType<ApplicationException>(exception.InnerException);
Assert.Equal("Something bad happened.", exception.InnerException.Message);

});

Expand All @@ -520,7 +525,7 @@ public async Task ExceptionHandlerNotFound_RethrowsOriginalError()
{
innerAppBuilder.Run(httpContext =>
{
throw new InvalidOperationException("Something bad happened.");
throw new ApplicationException("Something bad happened.");
});
});
});
Expand All @@ -539,7 +544,9 @@ public async Task ExceptionHandlerNotFound_RethrowsOriginalError()
Assert.Contains(sink.Writes, w =>
w.LogLevel == LogLevel.Warning
&& w.EventId == 4
&& w.Message == "No exception handler was found, rethrowing original exception.");
&& w.Message == $"The exception handler configured on ExceptionHandlerOptions produced a 404 status response. " +
$"An InvalidOperationException containing the original exception will be thrown since this is often due to a misconfigured ExceptionHandlingPath. " +
$"If the exception handler should be allowed to return 404 status responses, AllowStatusCode404Response must be set to true.");
}

[Fact]
Expand Down