Added new extensions UseExceptionHandler & UseDeveloperExceptionPage
This commit is contained in:
parent
5cb8ad3d87
commit
c46615dc53
|
|
@ -12,7 +12,7 @@ namespace ErrorHandlerSample
|
|||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
// Configure the error handler to show an error page.
|
||||
app.UseErrorHandler(errorApp =>
|
||||
app.UseExceptionHandler(errorApp =>
|
||||
{
|
||||
// Normally you'd use MVC or similar to render a nice page.
|
||||
errorApp.Run(async context =>
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ namespace ErrorPageSample
|
|||
{
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
app.UseErrorPage();
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.Run(context =>
|
||||
{
|
||||
throw new Exception(string.Concat(
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ namespace StatusCodePagesSample
|
|||
{
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
app.UseErrorPage();
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseStatusCodePages(); // There is a default response but any of the following can be used to change the behavior.
|
||||
|
||||
// app.UseStatusCodePages(context => context.HttpContext.Response.SendAsync("Handler, status code: " + context.HttpContext.Response.StatusCode, "text/plain"));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNet.Diagnostics;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Builder
|
||||
{
|
||||
/// <summary>
|
||||
/// IApplicationBuilder extension methods for the ErrorPageMiddleware.
|
||||
/// </summary>
|
||||
public static class ErrorPageExtensions
|
||||
{
|
||||
///// <summary>
|
||||
///// Captures synchronous and asynchronous exceptions from the pipeline and generates HTML error responses.
|
||||
///// Full error details are only displayed by default if 'host.AppMode' is set to 'development' in the IApplicationBuilder.Properties.
|
||||
///// </summary>
|
||||
///// <param name="builder"></param>
|
||||
///// <returns></returns>
|
||||
//public static IApplicationBuilder UseErrorPage([NotNull] this IApplicationBuilder builder)
|
||||
//{
|
||||
// return builder.UseErrorPage(new ErrorPageOptions());
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// Captures synchronous and asynchronous exceptions from the pipeline and generates HTML error responses.
|
||||
///// Full error details are only displayed by default if 'host.AppMode' is set to 'development' in the IApplicationBuilder.Properties.
|
||||
///// </summary>
|
||||
///// <param name="builder"></param>
|
||||
///// <param name="options"></param>
|
||||
///// <returns></returns>
|
||||
//public static IApplicationBuilder UseErrorPage([NotNull] this IApplicationBuilder builder, ErrorPageOptions options)
|
||||
//{
|
||||
// return builder.UseMiddleware<DeveloperExceptionPageMiddleware>(options);
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Captures synchronous and asynchronous exceptions from the pipeline and generates HTML error responses.
|
||||
/// Full error details are only displayed by default if 'host.AppMode' is set to 'development' in the IApplicationBuilder.Properties.
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseDeveloperExceptionPage([NotNull] this IApplicationBuilder builder)
|
||||
{
|
||||
return builder.UseDeveloperExceptionPage(new ErrorPageOptions());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Captures synchronous and asynchronous exceptions from the pipeline and generates HTML error responses.
|
||||
/// Full error details are only displayed by default if 'host.AppMode' is set to 'development' in the IApplicationBuilder.Properties.
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseDeveloperExceptionPage([NotNull] this IApplicationBuilder builder, ErrorPageOptions options)
|
||||
{
|
||||
return builder.UseMiddleware<DeveloperExceptionPageMiddleware>(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNet.Diagnostics;
|
||||
using Microsoft.AspNet.Http;
|
||||
|
||||
namespace Microsoft.AspNet.Builder
|
||||
{
|
||||
public static class ErrorHandlerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a middleware to the pipeline that will catch exceptions, log them, reset the request path, and re-execute the request.
|
||||
/// The request will not be re-executed if the response has already started.
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="errorHandlingPath"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseErrorHandler(this IApplicationBuilder app, string errorHandlingPath)
|
||||
{
|
||||
var options = new ErrorHandlerOptions()
|
||||
{
|
||||
ErrorHandlingPath = new PathString(errorHandlingPath)
|
||||
};
|
||||
return app.UseMiddleware<ErrorHandlerMiddleware>(options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a middleware to the pipeline that will catch exceptions, log them, and re-execute the request in an alternate pipeline.
|
||||
/// The request will not be re-executed if the response has already started.
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="configure"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseErrorHandler(this IApplicationBuilder app, Action<IApplicationBuilder> configure)
|
||||
{
|
||||
var subAppBuilder = app.New();
|
||||
configure(subAppBuilder);
|
||||
var errorPipeline = subAppBuilder.Build();
|
||||
var options = new ErrorHandlerOptions()
|
||||
{
|
||||
ErrorHandler = errorPipeline
|
||||
};
|
||||
return app.UseMiddleware<ErrorHandlerMiddleware>(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNet.Diagnostics;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Builder
|
||||
{
|
||||
/// <summary>
|
||||
/// IApplicationBuilder extension methods for the ErrorPageMiddleware.
|
||||
/// </summary>
|
||||
public static class ErrorPageExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Captures synchronous and asynchronous exceptions from the pipeline and generates HTML error responses.
|
||||
/// Full error details are only displayed by default if 'host.AppMode' is set to 'development' in the IApplicationBuilder.Properties.
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseErrorPage([NotNull] this IApplicationBuilder builder)
|
||||
{
|
||||
return builder.UseErrorPage(new ErrorPageOptions());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Captures synchronous and asynchronous exceptions from the pipeline and generates HTML error responses.
|
||||
/// Full error details are only displayed by default if 'host.AppMode' is set to 'development' in the IApplicationBuilder.Properties.
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseErrorPage([NotNull] this IApplicationBuilder builder, ErrorPageOptions options)
|
||||
{
|
||||
return builder.UseMiddleware<ErrorPageMiddleware>(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
/// <summary>
|
||||
/// Captures synchronous and asynchronous exceptions from the pipeline and generates HTML error responses.
|
||||
/// </summary>
|
||||
public class ErrorPageMiddleware
|
||||
public class DeveloperExceptionPageMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ErrorPageOptions _options;
|
||||
|
|
@ -33,11 +33,11 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
private readonly IFileProvider _fileProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ErrorPageMiddleware"/> class
|
||||
/// Initializes a new instance of the <see cref="DeveloperExceptionPageMiddleware"/> class
|
||||
/// </summary>
|
||||
/// <param name="next"></param>
|
||||
/// <param name="options"></param>
|
||||
public ErrorPageMiddleware(
|
||||
public DeveloperExceptionPageMiddleware(
|
||||
[NotNull] RequestDelegate next,
|
||||
[NotNull] ErrorPageOptions options,
|
||||
ILoggerFactory loggerFactory,
|
||||
|
|
@ -45,7 +45,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
{
|
||||
_next = next;
|
||||
_options = options;
|
||||
_logger = loggerFactory.CreateLogger<ErrorPageMiddleware>();
|
||||
_logger = loggerFactory.CreateLogger<DeveloperExceptionPageMiddleware>();
|
||||
_fileProvider = options.FileProvider ?? new PhysicalFileProvider(appEnvironment.ApplicationBasePath);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
/// Provides files containing source code used to display contextual information of an exception.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If <c>null</c> <see cref="ErrorPageMiddleware" /> will use a <see cref="PhysicalFileProvider"/>.
|
||||
/// If <c>null</c> <see cref="DeveloperExceptionPageMiddleware" /> will use a <see cref="PhysicalFileProvider"/>.
|
||||
/// </remarks>
|
||||
public IFileProvider FileProvider { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,82 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNet.Diagnostics;
|
||||
using Microsoft.AspNet.Http;
|
||||
|
||||
namespace Microsoft.AspNet.Builder
|
||||
{
|
||||
public static class ExceptionHandlerExtensions
|
||||
{
|
||||
///// <summary>
|
||||
///// Adds a middleware to the pipeline that will catch exceptions, log them, reset the request path, and re-execute the request.
|
||||
///// The request will not be re-executed if the response has already started.
|
||||
///// </summary>
|
||||
///// <param name="app"></param>
|
||||
///// <param name="errorHandlingPath"></param>
|
||||
///// <returns></returns>
|
||||
//public static IApplicationBuilder UseErrorHandler(this IApplicationBuilder app, string errorHandlingPath)
|
||||
//{
|
||||
// var options = new ErrorHandlerOptions()
|
||||
// {
|
||||
// ErrorHandlingPath = new PathString(errorHandlingPath)
|
||||
// };
|
||||
// return app.UseMiddleware<ErrorHandlerMiddleware>(options);
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// Adds a middleware to the pipeline that will catch exceptions, log them, and re-execute the request in an alternate pipeline.
|
||||
///// The request will not be re-executed if the response has already started.
|
||||
///// </summary>
|
||||
///// <param name="app"></param>
|
||||
///// <param name="configure"></param>
|
||||
///// <returns></returns>
|
||||
//public static IApplicationBuilder UseErrorHandler(this IApplicationBuilder app, Action<IApplicationBuilder> configure)
|
||||
//{
|
||||
// var subAppBuilder = app.New();
|
||||
// configure(subAppBuilder);
|
||||
// var errorPipeline = subAppBuilder.Build();
|
||||
// var options = new ErrorHandlerOptions()
|
||||
// {
|
||||
// ErrorHandler = errorPipeline
|
||||
// };
|
||||
// return app.UseMiddleware<ErrorHandlerMiddleware>(options);
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a middleware to the pipeline that will catch exceptions, log them, reset the request path, and re-execute the request.
|
||||
/// The request will not be re-executed if the response has already started.
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="errorHandlingPath"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseExceptionHandler(this IApplicationBuilder app, string errorHandlingPath)
|
||||
{
|
||||
var options = new ErrorHandlerOptions()
|
||||
{
|
||||
ErrorHandlingPath = new PathString(errorHandlingPath)
|
||||
};
|
||||
return app.UseMiddleware<ErrorHandlerMiddleware>(options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a middleware to the pipeline that will catch exceptions, log them, and re-execute the request in an alternate pipeline.
|
||||
/// The request will not be re-executed if the response has already started.
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="configure"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseExceptionHandler(this IApplicationBuilder app, Action<IApplicationBuilder> configure)
|
||||
{
|
||||
var subAppBuilder = app.New();
|
||||
configure(subAppBuilder);
|
||||
var errorPipeline = subAppBuilder.Build();
|
||||
var options = new ErrorHandlerOptions()
|
||||
{
|
||||
ErrorHandler = errorPipeline
|
||||
};
|
||||
return app.UseMiddleware<ErrorHandlerMiddleware>(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
app.UseErrorHandler("/handle-errors");
|
||||
app.UseExceptionHandler("/handle-errors");
|
||||
|
||||
app.Map("/handle-errors", (innerAppBuilder) =>
|
||||
{
|
||||
|
|
@ -72,7 +72,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
Assert.Equal("Something bad happened", exception.Message);
|
||||
});
|
||||
|
||||
app.UseErrorHandler("/handle-errors");
|
||||
app.UseExceptionHandler("/handle-errors");
|
||||
|
||||
app.Map("/handle-errors", (innerAppBuilder) =>
|
||||
{
|
||||
|
|
@ -124,7 +124,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
}
|
||||
});
|
||||
|
||||
app.UseErrorHandler("/handle-errors");
|
||||
app.UseExceptionHandler("/handle-errors");
|
||||
|
||||
app.Map("/handle-errors", (innerAppBuilder) =>
|
||||
{
|
||||
|
|
@ -171,7 +171,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
var expectedResponseBody = "Handled error in a custom way.";
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
app.UseErrorHandler("/handle-errors");
|
||||
app.UseExceptionHandler("/handle-errors");
|
||||
|
||||
app.Map("/handle-errors", (innerAppBuilder) =>
|
||||
{
|
||||
|
|
@ -218,7 +218,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
var expectedResponseBody = "Hello world!";
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
app.UseErrorHandler("/handle-errors");
|
||||
app.UseExceptionHandler("/handle-errors");
|
||||
|
||||
app.Map("/handle-errors", (innerAppBuilder) =>
|
||||
{
|
||||
|
|
@ -281,7 +281,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
Assert.Equal("Something bad happened", exception.Message);
|
||||
});
|
||||
|
||||
app.UseErrorHandler("/handle-errors");
|
||||
app.UseExceptionHandler("/handle-errors");
|
||||
|
||||
app.Map("/handle-errors", (innerAppBuilder) =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -283,7 +283,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
return Enumerable.Range(start, count).Select(i => string.Format("Line{0}", i));
|
||||
}
|
||||
|
||||
private ErrorPageMiddleware GetErrorPageMiddleware(
|
||||
private DeveloperExceptionPageMiddleware GetErrorPageMiddleware(
|
||||
IFileProvider fileProvider = null, int sourceCodeLineCount = 6)
|
||||
{
|
||||
var errorPageOptions = new ErrorPageOptions();
|
||||
|
|
@ -294,7 +294,7 @@ namespace Microsoft.AspNet.Diagnostics
|
|||
errorPageOptions.FileProvider = fileProvider;
|
||||
}
|
||||
|
||||
var middleware = new ErrorPageMiddleware(
|
||||
var middleware = new DeveloperExceptionPageMiddleware(
|
||||
(httpContext) => { return Task.FromResult(0); },
|
||||
errorPageOptions,
|
||||
new LoggerFactory(),
|
||||
|
|
|
|||
Loading…
Reference in New Issue