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

Conversation

lucaspimentel
Copy link
Member

@lucaspimentel lucaspimentel commented Dec 11, 2018

We got a report of a NullReferenceException in AspNetWebApi2Integration.ExecuteAsync(). This PR adds a null check for argument cancellationTokenSource with a fallback to CancellationToken.None. Also, if argument apiController is null (should never ever happen), throw ArgumentNullException with the name of the argument to make troubleshooting easier.

@lucaspimentel lucaspimentel added type:bug area:automatic-instrumentation Automatic instrumentation managed C# code (Datadog.Trace.ClrProfiler.Managed) labels Dec 11, 2018
@lucaspimentel lucaspimentel added this to the 0.5.3-beta milestone Dec 11, 2018
@lucaspimentel lucaspimentel self-assigned this Dec 11, 2018
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.

@lucaspimentel lucaspimentel merged commit 26ffcbe into develop Dec 11, 2018
@lucaspimentel lucaspimentel deleted the lpimentel/web-api-null-reference branch December 11, 2018 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:automatic-instrumentation Automatic instrumentation managed C# code (Datadog.Trace.ClrProfiler.Managed) type:bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants