Skip to content

Commit

Permalink
Create rule S2139: Add C# (#2465)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Mar 8, 2024
1 parent 710031d commit 334e99d
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 35 deletions.
3 changes: 0 additions & 3 deletions rules/S2139/comments-and-links.adoc

This file was deleted.

21 changes: 21 additions & 0 deletions rules/S2139/csharp/compliant.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[source,csharp,diff-id=1,diff-type=compliant]
----
try {}
catch (Exception ex)
{
logger.LogError(ex.Message);
// Handle exception
}
----

or

[source,csharp]
----
try {}
catch (Exception ex)
{
// ...
throw;
}
----
2 changes: 2 additions & 0 deletions rules/S2139/csharp/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
9 changes: 9 additions & 0 deletions rules/S2139/csharp/noncompliant.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[source,csharp,diff-id=1,diff-type=noncompliant]
----
try {}
catch (Exception ex)
{
logger.LogError(ex.Message);
throw;
}
----
3 changes: 3 additions & 0 deletions rules/S2139/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:language: csharp

include::../rule-dotnet.adoc[]
8 changes: 0 additions & 8 deletions rules/S2139/highlighting.adoc

This file was deleted.

18 changes: 1 addition & 17 deletions rules/S2139/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
include::../rule.adoc[]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
include::../rspecator.adoc[]
4 changes: 0 additions & 4 deletions rules/S2139/message.adoc

This file was deleted.

6 changes: 3 additions & 3 deletions rules/S2139/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"constantCost": "15min"
},
"tags": [
"error-handling",
"clumsy"
"logging",
"error-handling"
],
"extra": {
"replacementRules": [
Expand All @@ -31,5 +31,5 @@
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown"
"quickfix": "infeasible"
}
26 changes: 26 additions & 0 deletions rules/S2139/rspecator.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Either log this exception and handle it, or rethrow it with some contextual information.

=== Highlighting

* Primary: The exception caught
* Secondary 1: The logging statement:
** message: 'Logging statement.'
* Secondary 2: The `throw` statement
** message: 'Thrown exception.'

'''
== Comments And Links
(visible only on this page)

=== on 11 Oct 2014, 12:09:59 Freddy Mallet wrote:
Sounds good to me!

endif::env-github,rspecator-view[]
38 changes: 38 additions & 0 deletions rules/S2139/rule-dotnet.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
== Why is this an issue?

When an exception is logged and rethrown, the upstream code may not be aware that the exception has already been logged.
As a result, the same exception gets logged multiple times, making it difficult to identify the root cause of the issue.
This can be particularly problematic in multi-threaded applications where messages from other threads can be interwoven with the repeated log entries.

=== Exceptions

This rule will not generate issues if, within the catch block, one of the following conditions are met:

* The logs generated within the catch block do not contain any references to the exception being caught.
* The exception being thrown from the catch block is not the same exception that is being caught.

== How to fix it

To address this issue, it is recommended to modify the code to log exceptions only when they are handled locally. In all other cases, simply rethrow the exception and allow the higher-level layers of the application to handle the logging and appropriate actions.

=== Code examples

==== Noncompliant code example

include::{language}/noncompliant.adoc[]

==== Compliant solution

include::{language}/compliant.adoc[]

== Resources

=== Documentation

* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/exception-handling-statements[Exception-handling statements]

=== Articles & blog posts

* https://rolf-engelhard.de/2013/04/logging-anti-patterns-part-ii/[Rolf Engelhard - Logging anti-patterns]

include::rspecator.adoc[]

0 comments on commit 334e99d

Please sign in to comment.