CA1031 raised as false positive in async test method containing using statement

312 views
Skip to first unread message

Graham King

unread,
May 11, 2016, 4:51:02 AM5/11/16
to SonarQube
We are getting an major issue (/coding_rules#rule_key=fxcop%3ADoNotCatchGeneralExceptionTypes / CA1031) on the following async test method, which I believe is a false positive.  This is just one example, we are seeing it on many async test methods.

[Test]
public async Task UploadNonMimeMultipartContentExpectBadRequest()
{
    var client = server.HttpClient;
    client.DefaultRequestHeaders.Add("Accept", "application/json");
    using (var stringContent = new StringContent("not a file"))
    {
        var result = await client.PostAsync("some/url", stringContent);
        await AssertBadRequestResponse(result);    
    }
} // <-- Issue is identified here

private static async Task AssertBadRequestResponse(HttpResponseMessage result)
{
result.StatusCode.Should().Be(HttpStatusCode.BadRequest);

var content = await result.Content.ReadAsStringAsync();
content.Should().NotBeNullOrWhiteSpace();
}

Modify 'ProviderControllerTests.<UploadNoFileExpectBadRequest>d__0.MoveNext()' to catch a more specific exception than 'Exception' or rethrow the exception. 

As you can see we are not catching general exception (System.Exception or System.SystemException), in fact, we're not catching any exceptions.

I'm not sure if this is an issue with using, async/await or the mix of both but in either case the issues is with the IL generated by the compiler (I can see MoveNext() in the IL code) rather than with our code.

If this is a false positive, could it be raised as a bug, if not, how should we fix the code?

Many thanks

luci...@gmail.com

unread,
Jun 2, 2016, 6:57:26 AM6/2/16
to SonarQube, graha...@gmail.com
Hi Graham,
Any luck with this ? We are facing the same problem.

Thanks.

Tamas Vajk

unread,
Jun 3, 2016, 4:13:18 AM6/3/16
to luci...@gmail.com, SonarQube, graha...@gmail.com
Hi,

This issue is cause by the code generated for the async/await C# feature. As you could see in the IL code there's quite some code generated to set up a state machine for the await. And there's a generic catch [mscorlib]System.Exception in the MoveNext method. You can't do anything about this, you should mark this issue as a False Positive in SQ. 

FxCop is quite old, and known to have issues with new C# features, Microsoft is working on its replacement: https://www.nuget.org/packages/Microsoft.CodeAnalysis.FxCopAnalyzers.

Regards,
Tamas

Tamas VAJK | SonarSource
Language Team

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/93e4a852-8522-4efa-9480-71ac3e19b4d0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

luci...@gmail.com

unread,
Jun 3, 2016, 7:56:44 AM6/3/16
to SonarQube, luci...@gmail.com, graha...@gmail.com
We already went for the //NOSONAR approach for this issue. We thought there might be some other way around this but now, we will stick to it :)
Thank you Tamas. 
Reply all
Reply to author
Forward
0 new messages