Skip to content

Initial design for exception page filters #8958

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

Merged
merged 8 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -31,6 +31,7 @@ public class DeveloperExceptionPageMiddleware
private readonly IFileProvider _fileProvider;
private readonly DiagnosticSource _diagnosticSource;
private readonly ExceptionDetailsProvider _exceptionDetailsProvider;
private readonly Func<HttpContext, Exception, Task> _exceptionHandler;

/// <summary>
/// Initializes a new instance of the <see cref="DeveloperExceptionPageMiddleware"/> class
Expand All @@ -40,12 +41,14 @@ public class DeveloperExceptionPageMiddleware
/// <param name="loggerFactory"></param>
/// <param name="hostingEnvironment"></param>
/// <param name="diagnosticSource"></param>
/// <param name="filters"></param>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this are some hq docs

public DeveloperExceptionPageMiddleware(
RequestDelegate next,
IOptions<DeveloperExceptionPageOptions> options,
ILoggerFactory loggerFactory,
IWebHostEnvironment hostingEnvironment,
DiagnosticSource diagnosticSource)
DiagnosticSource diagnosticSource,
IEnumerable<IDeveloperPageExceptionFilter> filters)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaking change yo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know but it's 3.0 and I'm expecting this to be acceptable. I didn't update the refs tho because who ever remembers that the first time 😄

{
if (next == null)
{
Expand All @@ -63,6 +66,13 @@ public DeveloperExceptionPageMiddleware(
_fileProvider = _options.FileProvider ?? hostingEnvironment.ContentRootFileProvider;
_diagnosticSource = diagnosticSource;
_exceptionDetailsProvider = new ExceptionDetailsProvider(_fileProvider, _options.SourceCodeLineCount);
_exceptionHandler = DisplayException;

foreach (var filter in filters.Reverse())
{
var nextFilter = _exceptionHandler;
_exceptionHandler = (context, exception) => filter.HandleExceptionAsync(context, exception, nextFilter);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the CoR is so elegant and cool

}

/// <summary>
Expand Down Expand Up @@ -91,7 +101,7 @@ public async Task Invoke(HttpContext context)
context.Response.Clear();
context.Response.StatusCode = 500;

await DisplayException(context, ex);
await _exceptionHandler(context, ex);

if (_diagnosticSource.IsEnabled("Microsoft.AspNetCore.Diagnostics.UnhandledException"))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Diagnostics
{
public interface IDeveloperPageExceptionFilter
{
Task HandleExceptionAsync(HttpContext context, Exception exception, Func<HttpContext, Exception, Task> next);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Diagnostics
{
Expand Down Expand Up @@ -46,6 +45,88 @@ public async Task UnhandledErrorsWriteToDiagnosticWhenUsingExceptionPage()
Assert.Null(listener.DiagnosticHandledException?.Exception);
}

[Fact]
public async Task ExceptionPageFiltersAreApplied()
{
// Arrange
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<IDeveloperPageExceptionFilter, ExceptionMessageFilter>();
})
.Configure(app =>
{
app.UseDeveloperExceptionPage();
app.Run(context =>
{
throw new Exception("Test exception");
});
});
var server = new TestServer(builder);

// Act
var response = await server.CreateClient().GetAsync("/path");

// Assert
Assert.Equal("Test exception", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task ExceptionFilterCallingNextWorks()
{
// Arrange
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<IDeveloperPageExceptionFilter, PassThroughExceptionFilter>();
services.AddSingleton<IDeveloperPageExceptionFilter, AlwaysBadFormatExceptionFilter>();
services.AddSingleton<IDeveloperPageExceptionFilter, ExceptionMessageFilter>();
})
.Configure(app =>
{
app.UseDeveloperExceptionPage();
app.Run(context =>
{
throw new Exception("Test exception");
});
});
var server = new TestServer(builder);

// Act
var response = await server.CreateClient().GetAsync("/path");

// Assert
Assert.Equal("Bad format exception!", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task ExceptionPageFiltersAreAppliedInOrder()
{
// Arrange
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<IDeveloperPageExceptionFilter, AlwaysThrowSameMessageFilter>();
services.AddSingleton<IDeveloperPageExceptionFilter, ExceptionMessageFilter>();
services.AddSingleton<IDeveloperPageExceptionFilter, ExceptionToStringFilter>();
})
.Configure(app =>
{
app.UseDeveloperExceptionPage();
app.Run(context =>
{
throw new Exception("Test exception");
});
});
var server = new TestServer(builder);

// Act
var response = await server.CreateClient().GetAsync("/path");

// Assert
Assert.Equal("An error occurred", await response.Content.ReadAsStringAsync());
}

public static TheoryData CompilationExceptionData
{
get
Expand Down Expand Up @@ -140,5 +221,45 @@ public CustomCompilationException(IEnumerable<CompilationFailure> compilationFai

public IEnumerable<CompilationFailure> CompilationFailures { get; }
}

public class ExceptionMessageFilter : IDeveloperPageExceptionFilter
{
public Task HandleExceptionAsync(HttpContext context, Exception exception, Func<HttpContext, Exception, Task> next)
{
return context.Response.WriteAsync(exception.Message);
}
}

public class ExceptionToStringFilter : IDeveloperPageExceptionFilter
{
public Task HandleExceptionAsync(HttpContext context, Exception exception, Func<HttpContext, Exception, Task> next)
{
return context.Response.WriteAsync(exception.ToString());
}
}

public class AlwaysThrowSameMessageFilter : IDeveloperPageExceptionFilter
{
public Task HandleExceptionAsync(HttpContext context, Exception exception, Func<HttpContext, Exception, Task> next)
{
return context.Response.WriteAsync("An error occurred");
}
}

public class AlwaysBadFormatExceptionFilter : IDeveloperPageExceptionFilter
{
public Task HandleExceptionAsync(HttpContext context, Exception exception, Func<HttpContext, Exception, Task> next)
{
return next(context, new FormatException("Bad format exception!"));
}
}

public class PassThroughExceptionFilter : IDeveloperPageExceptionFilter
{
public Task HandleExceptionAsync(HttpContext context, Exception exception, Func<HttpContext, Exception, Task> next)
{
return next(context, exception);
}
}
}
}