Removed SetDefaultVisibility for ErrorPageOptions

- Removed `SetDefaultVisibility` method from `(Database)ErrorPageOptions`
- `(Database)ErrorPageOptions` properties are now auto-properties since there is no default visibility anymore
- Removed `isDevMode` parameter from `ErrorPageMiddleware` ctor
- Removed comments from `ErroPageExtensions.UseErrorPage()`
- Updated Entity tests

#130
This commit is contained in:
Henk Mollema 2015-06-01 16:25:13 +02:00 committed by Chris R
parent 39fa2782d2
commit 6dbbe831c4
6 changed files with 45 additions and 141 deletions

View File

@ -1,45 +1,18 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Diagnostics.Entity.Utilities;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Diagnostics.Entity namespace Microsoft.AspNet.Diagnostics.Entity
{ {
public class DatabaseErrorPageOptions public class DatabaseErrorPageOptions
{ {
private bool _defaultVisibility; public static DatabaseErrorPageOptions ShowAll => new DatabaseErrorPageOptions
private bool? _showExceptionDetails; {
private bool? _listMigrations; ShowExceptionDetails = true,
ListMigrations = true,
};
public static DatabaseErrorPageOptions ShowAll public virtual bool ShowExceptionDetails { get; set; }
{
get
{
// We don't use a static instance because it's mutable.
return new DatabaseErrorPageOptions()
{
ShowExceptionDetails = true,
ListMigrations = true,
};
}
}
public virtual bool ShowExceptionDetails public virtual bool ListMigrations { get; set; }
{
get { return _showExceptionDetails ?? _defaultVisibility; }
set { _showExceptionDetails = value; }
}
public virtual bool ListMigrations
{
get { return _listMigrations ?? _defaultVisibility; }
set { _listMigrations = value; }
}
public virtual void SetDefaultVisibility(bool isVisible)
{
_defaultVisibility = isVisible;
}
} }
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.Diagnostics;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
@ -32,11 +31,7 @@ namespace Microsoft.AspNet.Builder
/// <returns></returns> /// <returns></returns>
public static IApplicationBuilder UseErrorPage([NotNull] this IApplicationBuilder builder, ErrorPageOptions options) public static IApplicationBuilder UseErrorPage([NotNull] this IApplicationBuilder builder, ErrorPageOptions options)
{ {
/* TODO: Development, Staging, or Production return builder.Use(next => new ErrorPageMiddleware(next, options).Invoke);
string appMode = new AppProperties(builder.Properties).Get<string>(Constants.HostAppMode);
bool isDevMode = string.Equals(Constants.DevMode, appMode, StringComparison.Ordinal);*/
bool isDevMode = true;
return builder.Use(next => new ErrorPageMiddleware(next, options, isDevMode).Invoke);
} }
} }
} }

View File

@ -31,13 +31,8 @@ namespace Microsoft.AspNet.Diagnostics
/// </summary> /// </summary>
/// <param name="next"></param> /// <param name="next"></param>
/// <param name="options"></param> /// <param name="options"></param>
/// <param name="isDevMode"></param> public ErrorPageMiddleware([NotNull] RequestDelegate next, [NotNull] ErrorPageOptions options)
public ErrorPageMiddleware([NotNull] RequestDelegate next, [NotNull] ErrorPageOptions options, bool isDevMode)
{ {
if (isDevMode)
{
options.SetDefaultVisibility(isVisible: true);
}
_next = next; _next = next;
_options = options; _options = options;
} }

View File

@ -5,19 +5,10 @@
namespace Microsoft.AspNet.Diagnostics namespace Microsoft.AspNet.Diagnostics
{ {
/// <summary> /// <summary>
/// Options for the ErrorPageMiddleware /// Options for the ErrorPageMiddleware.
/// </summary> /// </summary>
public class ErrorPageOptions public class ErrorPageOptions
{ {
private bool _defaultVisibility;
private bool? _showExceptionDetails;
private bool? _showSourceCode;
private bool? _showQuery;
private bool? _showCookies;
private bool? _showHeaders;
private bool? _showEnvironment;
/// <summary> /// <summary>
/// Create an instance with the default options settings. /// Create an instance with the default options settings.
/// </summary> /// </summary>
@ -29,40 +20,25 @@ namespace Microsoft.AspNet.Diagnostics
/// <summary> /// <summary>
/// Returns a new instance of ErrorPageOptions with all visibility options enabled by default. /// Returns a new instance of ErrorPageOptions with all visibility options enabled by default.
/// </summary> /// </summary>
public static ErrorPageOptions ShowAll public static ErrorPageOptions ShowAll => new ErrorPageOptions
{ {
get ShowExceptionDetails = true,
{ ShowSourceCode = true,
// We don't use a static instance because it's mutable. ShowQuery = true,
return new ErrorPageOptions() ShowCookies = true,
{ ShowHeaders = true,
ShowExceptionDetails = true, ShowEnvironment = true,
ShowSourceCode = true, };
ShowQuery = true,
ShowCookies = true,
ShowHeaders = true,
ShowEnvironment = true,
};
}
}
/// <summary> /// <summary>
/// Enables the display of exception types, messages, and stack traces. /// Enables the display of exception types, messages, and stack traces.
/// </summary> /// </summary>
public bool ShowExceptionDetails public bool ShowExceptionDetails { get; set; }
{
get { return _showExceptionDetails ?? _defaultVisibility; }
set { _showExceptionDetails = value; }
}
/// <summary> /// <summary>
/// Enabled the display of local source code around exception stack frames. /// Enabled the display of local source code around exception stack frames.
/// </summary> /// </summary>
public bool ShowSourceCode public bool ShowSourceCode { get; set; }
{
get { return _showSourceCode ?? _defaultVisibility; }
set { _showSourceCode = value; }
}
/// <summary> /// <summary>
/// Determines how many lines of code to include before and after the line of code /// Determines how many lines of code to include before and after the line of code
@ -74,46 +50,21 @@ namespace Microsoft.AspNet.Diagnostics
/// <summary> /// <summary>
/// Enables the enumeration of any parsed query values. /// Enables the enumeration of any parsed query values.
/// </summary> /// </summary>
public bool ShowQuery public bool ShowQuery { get; set; }
{
get { return _showQuery ?? _defaultVisibility; }
set { _showQuery = value; }
}
/// <summary> /// <summary>
/// Enables the enumeration of any parsed request cookies. /// Enables the enumeration of any parsed request cookies.
/// </summary> /// </summary>
public bool ShowCookies public bool ShowCookies { get; set; }
{
get { return _showCookies ?? _defaultVisibility; }
set { _showCookies = value; }
}
/// <summary> /// <summary>
/// Enables the enumeration of the request headers. /// Enables the enumeration of the request headers.
/// </summary> /// </summary>
public bool ShowHeaders public bool ShowHeaders { get; set; }
{
get { return _showHeaders ?? _defaultVisibility; }
set { _showHeaders = value; }
}
/// <summary> /// <summary>
/// Enables the enumeration of the OWIN environment values. /// Enables the enumeration of the OWIN environment values.
/// </summary> /// </summary>
public bool ShowEnvironment public bool ShowEnvironment { get; set; }
{
get { return _showEnvironment ?? _defaultVisibility; }
set { _showEnvironment = value; }
}
/// <summary>
/// Sets the default visibility for options not otherwise specified.
/// </summary>
/// <param name="isVisible"></param>
public void SetDefaultVisibility(bool isVisible)
{
_defaultVisibility = isVisible;
}
} }
} }

View File

@ -17,32 +17,29 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
} }
[Fact] [Fact]
public void Default_visibility_can_be_changed() public void ShowAll_shows_all_errors()
{ {
var options = new DatabaseErrorPageOptions(); var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true);
Assert.True(options.ShowExceptionDetails); Assert.True(options.ShowExceptionDetails);
Assert.True(options.ListMigrations); Assert.True(options.ListMigrations);
} }
[Fact] [Fact]
public void ShowExceptionDetails_overides_default_visibility() public void ShowExceptionDetails_is_respected()
{ {
var options = new DatabaseErrorPageOptions { ShowExceptionDetails = false }; var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true); options.ShowExceptionDetails = false;
Assert.False(options.ShowExceptionDetails); Assert.False(options.ShowExceptionDetails);
Assert.True(options.ListMigrations);
} }
[Fact] [Fact]
public void ListMigrations_overides_default_visibility() public void ListMigrations_is_respected()
{ {
var options = new DatabaseErrorPageOptions { ListMigrations = false }; var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true); options.ListMigrations = false;
Assert.True(options.ShowExceptionDetails);
Assert.False(options.ListMigrations); Assert.False(options.ListMigrations);
} }
} }

View File

@ -19,8 +19,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task No_database_or_migrations_only_displays_scaffold_first_migration() public async Task No_database_or_migrations_only_displays_scaffold_first_migration()
{ {
var options = new DatabaseErrorPageOptions(); var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true);
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),
@ -40,8 +39,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task No_database_with_migrations_only_displays_apply_migrations() public async Task No_database_with_migrations_only_displays_apply_migrations()
{ {
var options = new DatabaseErrorPageOptions(); var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true);
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),
@ -61,8 +59,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task Existing_database_with_migrations_only_displays_apply_migrations() public async Task Existing_database_with_migrations_only_displays_apply_migrations()
{ {
var options = new DatabaseErrorPageOptions(); var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true);
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),
@ -82,8 +79,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task Existing_database_with_migrations_and_pending_model_changes_only_displays_apply_migrations() public async Task Existing_database_with_migrations_and_pending_model_changes_only_displays_apply_migrations()
{ {
var options = new DatabaseErrorPageOptions(); var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true);
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),
@ -103,8 +99,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task Pending_model_changes_only_displays_scaffold_next_migration() public async Task Pending_model_changes_only_displays_scaffold_next_migration()
{ {
var options = new DatabaseErrorPageOptions(); var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true);
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),
@ -124,8 +119,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task Exception_details_are_displayed() public async Task Exception_details_are_displayed()
{ {
var options = new DatabaseErrorPageOptions(); var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true);
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),
@ -143,8 +137,7 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task Inner_exception_details_are_displayed() public async Task Inner_exception_details_are_displayed()
{ {
var options = new DatabaseErrorPageOptions(); var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true);
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),
@ -163,8 +156,8 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task ShowExceptionDetails_is_respected() public async Task ShowExceptionDetails_is_respected()
{ {
var options = new DatabaseErrorPageOptions { ShowExceptionDetails = false }; var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true); options.ShowExceptionDetails = false;
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),
@ -182,8 +175,8 @@ namespace Microsoft.AspNet.Diagnostics.Entity.Tests
[Fact] [Fact]
public async Task ListMigrations_is_respected() public async Task ListMigrations_is_respected()
{ {
var options = new DatabaseErrorPageOptions { ListMigrations = false }; var options = DatabaseErrorPageOptions.ShowAll;
options.SetDefaultVisibility(true); options.ListMigrations = false;
var model = new DatabaseErrorPageModel( var model = new DatabaseErrorPageModel(
contextType: typeof(BloggingContext), contextType: typeof(BloggingContext),