Skip to content
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

Web API integration: check for null argument #230

Merged
merged 2 commits into from
Dec 11, 2018
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
1 change: 1 addition & 0 deletions GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1200:UsingDirectivesMustBePlacedWithinNamespace", Justification = "Reviewed.")]
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1512:Single-line comments must not be followed by blank line", Justification = "Reviewed.")]
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1515:Single-line comment must be preceded by blank line", Justification = "Reviewed.")]
[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1501:Statement must not be on a single line", Justification = "Reviewed.")]
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public static class AspNetWebApi2Integration
/// <returns>A task with the result</returns>
public static object ExecuteAsync(object apiController, object controllerContext, object cancellationTokenSource)
{
var cancellationToken = ((CancellationTokenSource)cancellationTokenSource).Token;
if (apiController == null) { throw new ArgumentNullException(nameof(apiController)); }

if (controllerContext == null) { throw new ArgumentNullException(nameof(controllerContext)); }
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be in favor of not throwing exceptions to make sure we interfere as little as possible with the normal program execution. Maybe we could log something instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Those arguments can't actually be null in normal program execution. ExecuteAsync() is an instance method and apiController is the this argument.

I removed the null check for controllerContext, since we only use it to populate the span (we already avoid using it if it is null). If it is null, Web API will throw quickly later on.

I left the check for apiController since we actually need that value and we cannot move forward without it. Also, ArgumentNullException looks better and provides more information than the inevitable NullReferenceException.


var tokenSource = cancellationTokenSource as CancellationTokenSource;
var cancellationToken = tokenSource?.Token ?? CancellationToken.None;
return ExecuteAsyncInternal(apiController, controllerContext, cancellationToken);
}

Expand Down