Replacing NotNullAttribute with thrown exceptions

This commit is contained in:
Pranav K 2015-09-14 10:25:22 -07:00
parent 2766113ba9
commit d3f1437764
10 changed files with 128 additions and 35 deletions

View File

@ -1,8 +1,8 @@
// 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.Framework.Internal;
namespace Microsoft.AspNet.Builder
{
@ -17,8 +17,13 @@ namespace Microsoft.AspNet.Builder
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseDeveloperExceptionPage([NotNull] this IApplicationBuilder builder)
public static IApplicationBuilder UseDeveloperExceptionPage(this IApplicationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.UseDeveloperExceptionPage(new ErrorPageOptions());
}
@ -29,8 +34,13 @@ namespace Microsoft.AspNet.Builder
/// <param name="builder"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseDeveloperExceptionPage([NotNull] this IApplicationBuilder builder, ErrorPageOptions options)
public static IApplicationBuilder UseDeveloperExceptionPage(this IApplicationBuilder builder, ErrorPageOptions options)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.UseMiddleware<DeveloperExceptionPageMiddleware>(options);
}
}

View File

@ -13,10 +13,8 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics.Views;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.Dnx.Compilation;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Diagnostics
@ -38,11 +36,21 @@ namespace Microsoft.AspNet.Diagnostics
/// <param name="next"></param>
/// <param name="options"></param>
public DeveloperExceptionPageMiddleware(
[NotNull] RequestDelegate next,
[NotNull] ErrorPageOptions options,
RequestDelegate next,
ErrorPageOptions options,
ILoggerFactory loggerFactory,
IApplicationEnvironment appEnvironment)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_next = next;
_options = options;
_logger = loggerFactory.CreateLogger<DeveloperExceptionPageMiddleware>();

View File

@ -1,7 +1,6 @@
// 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.FileProviders;
namespace Microsoft.AspNet.Diagnostics

View File

@ -1,10 +1,10 @@
// 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;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Builder
{
@ -21,9 +21,19 @@ namespace Microsoft.AspNet.Builder
}
public static IApplicationBuilder UseRuntimeInfoPage(
[NotNull] this IApplicationBuilder builder,
[NotNull] RuntimeInfoPageOptions options)
this IApplicationBuilder builder,
RuntimeInfoPageOptions options)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
var libraryManager = builder.ApplicationServices.GetService(typeof(ILibraryManager)) as ILibraryManager;
var runtimeEnvironment = builder.ApplicationServices.GetService(typeof(IRuntimeEnvironment)) as IRuntimeEnvironment;
return builder.Use(next => new RuntimeInfoMiddleware(next, options, libraryManager, runtimeEnvironment).Invoke);

View File

@ -1,12 +1,12 @@
// 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 System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics.Views;
using Microsoft.AspNet.Http;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Diagnostics
{
@ -26,11 +26,31 @@ namespace Microsoft.AspNet.Diagnostics
/// <param name="next"></param>
/// <param name="options"></param>
public RuntimeInfoMiddleware(
[NotNull] RequestDelegate next,
[NotNull] RuntimeInfoPageOptions options,
[NotNull] ILibraryManager libraryManager,
[NotNull] IRuntimeEnvironment runtimeEnvironment)
RequestDelegate next,
RuntimeInfoPageOptions options,
ILibraryManager libraryManager,
IRuntimeEnvironment runtimeEnvironment)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (libraryManager == null)
{
throw new ArgumentNullException(nameof(libraryManager));
}
if (runtimeEnvironment == null)
{
throw new ArgumentNullException(nameof(runtimeEnvironment));
}
_next = next;
_options = options;
_libraryManager = libraryManager;

View File

@ -7,7 +7,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Builder
{
@ -20,8 +19,13 @@ namespace Microsoft.AspNet.Builder
/// <param name="app"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseStatusCodePages([NotNull]this IApplicationBuilder app, StatusCodePagesOptions options)
public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, StatusCodePagesOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseMiddleware<StatusCodePagesMiddleware>(options);
}

View File

@ -9,7 +9,6 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
using Microsoft.Framework.WebEncoders;
namespace Microsoft.AspNet.Diagnostics.Views
@ -90,11 +89,26 @@ namespace Microsoft.AspNet.Diagnostics.Views
/// <param name="trailer">The value and position of the suffix</param>
/// <param name="values">The <see cref="AttributeValue"/>s to write.</param>
protected void WriteAttribute(
[NotNull] string name,
[NotNull] Tuple<string, int> leader,
[NotNull] Tuple<string, int> trailer,
string name,
Tuple<string, int> leader,
Tuple<string, int> trailer,
params AttributeValue[] values)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (leader == null)
{
throw new ArgumentNullException(nameof(leader));
}
if (trailer == null)
{
throw new ArgumentNullException(nameof(trailer));
}
WriteAttributeTo(
Output,
name,
@ -112,12 +126,32 @@ namespace Microsoft.AspNet.Diagnostics.Views
/// <param name="trailer">The value and position of the suffix</param>
/// <param name="values">The <see cref="AttributeValue"/>s to write.</param>
protected void WriteAttributeTo(
[NotNull] TextWriter writer,
[NotNull] string name,
[NotNull] Tuple<string, int> leader,
[NotNull] Tuple<string, int> trailer,
TextWriter writer,
string name,
Tuple<string, int> leader,
Tuple<string, int> trailer,
params AttributeValue[] values)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (leader == null)
{
throw new ArgumentNullException(nameof(leader));
}
if (trailer == null)
{
throw new ArgumentNullException(nameof(trailer));
}
WriteLiteralTo(writer, leader.Item1);
foreach (var value in values)

View File

@ -5,7 +5,6 @@
using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Diagnostics;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Builder
{
@ -20,8 +19,13 @@ namespace Microsoft.AspNet.Builder
/// <param name="builder"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseWelcomePage([NotNull] this IApplicationBuilder builder, WelcomePageOptions options)
public static IApplicationBuilder UseWelcomePage(this IApplicationBuilder builder, WelcomePageOptions options)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.Use(next => new WelcomePageMiddleware(next, options).Invoke);
}

View File

@ -3,12 +3,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNet.Diagnostics.Views;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Diagnostics
{
@ -25,8 +23,18 @@ namespace Microsoft.AspNet.Diagnostics
/// </summary>
/// <param name="next"></param>
/// <param name="options"></param>
public WelcomePageMiddleware([NotNull] RequestDelegate next, [NotNull] WelcomePageOptions options)
public WelcomePageMiddleware(RequestDelegate next, WelcomePageOptions options)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_next = next;
_options = options;
}

View File

@ -11,10 +11,6 @@
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
"Microsoft.AspNet.WebUtilities": "1.0.0-*",
"Microsoft.Framework.Logging.Abstractions": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Sources": {
"type": "build",
"version": "1.0.0-*"
},
"Microsoft.Framework.OptionsModel": "1.0.0-*",
"Microsoft.Dnx.Compilation.Abstractions": "1.0.0-*",
"Microsoft.Framework.WebEncoders.Core": "1.0.0-*"