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

Fix DatabaseDeveloperPageExceptionFilter to handle wrapped DbExceptions #41041

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -42,7 +42,10 @@ public DatabaseDeveloperPageExceptionFilter(ILogger<DatabaseDeveloperPageExcepti
/// <inheritdoc />
public async Task HandleExceptionAsync(ErrorContext errorContext, Func<ErrorContext, Task> next)
{
if (!(errorContext.Exception is DbException))
var dbException = errorContext.Exception as DbException
?? errorContext.Exception?.InnerException as DbException;

if (dbException == null)
{
await next(errorContext);
return;
Expand Down Expand Up @@ -73,7 +76,7 @@ public async Task HandleExceptionAsync(ErrorContext errorContext, Func<ErrorCont
{
var page = new DatabaseErrorPage
{
Model = new DatabaseErrorPageModel(errorContext.Exception, contextDetails, _options, errorContext.HttpContext.Request.PathBase)
Model = new DatabaseErrorPageModel(dbException, contextDetails, _options, errorContext.HttpContext.Request.PathBase)
};

await page.ExecuteAsync(errorContext.HttpContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ await filter.HandleExceptionAsync(
Assert.True(nextFilterInvoked);
}

[Fact]
public async Task Wrapped_DbExceptions_HandlingFails_InvokesNextFilter()
{
var sink = new TestSink();
var filter = new DatabaseDeveloperPageExceptionFilter(
new TestLogger<DatabaseDeveloperPageExceptionFilter>(new TestLoggerFactory(sink, true)),
Options.Create(new DatabaseErrorPageOptions()));
var context = new DefaultHttpContext();
var exception = new InvalidOperationException("Bang!", new Mock<DbException>().Object);
var nextFilterInvoked = false;

await filter.HandleExceptionAsync(
new ErrorContext(context, exception),
context =>
{
nextFilterInvoked = true;
return Task.CompletedTask;
});

Assert.True(nextFilterInvoked);
Assert.Equal(1, sink.Writes.Count);
var message = sink.Writes.Single();
Assert.Equal(LogLevel.Error, message.LogLevel);
Assert.Contains("An exception occurred while calculating the database error page content.", message.Message);
}

[Fact]
public async Task DbExceptions_HandlingFails_InvokesNextFilter()
{
Expand Down