Skip to content

Commit

Permalink
Fix DatabaseDeveloperPageExceptionFilter to handle wrapped DbExceptio…
Browse files Browse the repository at this point in the history
…ns. (#41041)
  • Loading branch information
adityamandaleeka authored Apr 5, 2022
1 parent ce10dca commit 24280d9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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

0 comments on commit 24280d9

Please sign in to comment.