-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Client core: new logging exceptions checks #45433
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
Merged
lmolkova
merged 29 commits into
Azure:main
from
lmolkova:client-core-logging-exceptions-checks
May 29, 2025
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
31b3abd
Add clientcore logging checks and update core to follow them
lmolkova d0dc0bd
up
lmolkova 5531372
spotless
lmolkova 87ba45d
up
lmolkova dee1e95
spotless
lmolkova 1bed10a
clean up
lmolkova aa1f4ab
tests
lmolkova b3de204
checkstyle fixes and suppressions
lmolkova 1e0ed58
checkstyle suppressions kv
lmolkova 978508c
checkstyle suppressions identity
lmolkova 09dad86
annotation processor and appconfig
lmolkova 12ec5b7
up
lmolkova 6ea581a
spotless
lmolkova 3bd89f0
add string concat
lmolkova 9b3b6ab
static exception text
lmolkova 8119358
spotless
lmolkova 3c11f46
review comments: part 1
lmolkova 05e8edc
Merge branch 'main' into client-core-logging-exceptions-checks
lmolkova eb7165a
clean up
lmolkova 47cd590
Merge branch 'main' into client-core-logging-exceptions-checks
lmolkova f1ee4ce
clean up identity
lmolkova 1772c72
appconfig: coreexception and spotless
lmolkova a393556
add illegal state to the list of ignored exceptions
lmolkova 1714d3f
formatting
lmolkova 77e0f0b
Merge branch 'main' into client-core-logging-exceptions-checks
lmolkova 98266bd
merge
lmolkova 51e57c6
run exception checks on generated code
lmolkova d897fdd
Merge branch 'main' into client-core-logging-exceptions-checks
lmolkova 6ec99cd
update kv suppressions
lmolkova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...ts/src/main/java/com/azure/tools/checkstyle/checks/ExceptionCreatedButNotThrownCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.tools.checkstyle.checks; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.FullIdent; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Checks that the exception is created and logged is also thrown. | ||
*/ | ||
public class ExceptionCreatedButNotThrownCheck extends AbstractCheck { | ||
static final String ERROR_MESSAGE = "An exception is created and logged, but not thrown. Ensure the exception is either thrown or not created at all. " | ||
+ "See https://github.com/Azure/azure-sdk-for-java/wiki/Client-core:-logging-exceptions-best-practices for more details."; | ||
private static final List<String> THROWABLE_AT_LOGGING_METHODS = Arrays.asList( | ||
".throwableAtError", | ||
".throwableAtWarning"); | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return new int[] { | ||
TokenTypes.METHOD_CALL | ||
}; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST token) { | ||
String name = FullIdent.createFullIdentBelow(token).getText(); | ||
if (THROWABLE_AT_LOGGING_METHODS.stream().anyMatch(name::endsWith) && !isInsideThrow(token)) { | ||
DetailAST logMethodCall = token.getParent().getParent(); | ||
if (logMethodCall == null || logMethodCall.getType() != TokenTypes.METHOD_CALL) { | ||
return; | ||
} | ||
|
||
String nextName = FullIdent.createFullIdentBelow(logMethodCall).getText(); | ||
if (nextName.endsWith(".log")) { | ||
log(token, ERROR_MESSAGE); | ||
} | ||
} | ||
} | ||
|
||
private boolean isInsideThrow(DetailAST methodCallAst) { | ||
DetailAST parent = methodCallAst.getParent(); | ||
|
||
// Walk up to skip DOT or EXPR | ||
while (parent != null && (parent.getType() == TokenTypes.DOT | ||
|| parent.getType() == TokenTypes.EXPR | ||
|| parent.getType() == TokenTypes.TYPECAST | ||
|| parent.getType() == TokenTypes.METHOD_CALL)) { | ||
parent = parent.getParent(); | ||
} | ||
|
||
return parent != null && parent.getType() == TokenTypes.LITERAL_THROW; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...ality-reports/src/main/java/com/azure/tools/checkstyle/checks/RawExceptionThrowCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.tools.checkstyle.checks; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.FullIdent; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Check that verifies that exceptions are logged when they are created. | ||
* The check is skipped if: | ||
* - the exception is created in a generated method or service interface | ||
* - the exception is a NullPointerException, IllegalArgumentException or UnsupportedOperationException that are used for immediate input validation | ||
*/ | ||
public class RawExceptionThrowCheck extends AbstractCheck { | ||
static final String ERROR_MESSAGE = "Directly throwing a new exception is disallowed. Use the \"io.clientcore.core.instrumentation.logging.ClientLogger\" API instead, " | ||
+ "such as \"logger.throwableAtError()\" or \"logger.throwableAtWarning()\"." | ||
+ "See https://github.com/Azure/azure-sdk-for-java/wiki/Client-core:-logging-exceptions-best-practices for more details."; | ||
|
||
private static final List<String> IGNORED_EXCEPTIONS = Arrays.asList("NullPointerException", | ||
"IllegalArgumentException", | ||
"IllegalStateException", | ||
"UnsupportedOperationException"); | ||
|
||
private static final String CORE_EXCEPTION_FACTORY_NAME = "CoreException.from"; | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return new int[] { | ||
TokenTypes.LITERAL_THROW | ||
}; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST token) { | ||
DetailAST expr = token.findFirstToken(TokenTypes.EXPR); | ||
if (expr == null) { | ||
return; | ||
} | ||
|
||
DetailAST newToken = expr.findFirstToken(TokenTypes.LITERAL_NEW); | ||
if (newToken != null) { | ||
String name = FullIdent.createFullIdentBelow(newToken).getText(); | ||
if (IGNORED_EXCEPTIONS.stream().noneMatch(name::endsWith)) { | ||
log(newToken, ERROR_MESSAGE); | ||
} | ||
|
||
return; | ||
} | ||
|
||
DetailAST methodCallToken = expr.findFirstToken(TokenTypes.METHOD_CALL); | ||
if (methodCallToken != null) { | ||
if (CORE_EXCEPTION_FACTORY_NAME.equals(FullIdent.createFullIdentBelow(methodCallToken).getText())) | ||
{ | ||
log(methodCallToken, ERROR_MESSAGE); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...src/main/java/com/azure/tools/checkstyle/checks/StringFormattedExceptionMessageCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.tools.checkstyle.checks; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.FullIdent; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Checks that the message provided to "logger.throwableAt*().log" is static (not created using String.format). | ||
*/ | ||
public class StringFormattedExceptionMessageCheck extends AbstractCheck { | ||
static final String ERROR_MESSAGE = "Short message passed to \"logger.throwableAt*().log\" should be static. " | ||
+ "Provide dynamic components using the \"addKeyValue(key, value)\" method instead. " | ||
+ "See https://github.com/Azure/azure-sdk-for-java/wiki/Client-core:-logging-exceptions-best-practices for more details."; | ||
private static final List<String> THROWABLE_AT_LOGGING_METHODS = Arrays.asList( | ||
".throwableAtError", | ||
".throwableAtWarning"); | ||
|
||
private static final String LOG_METHOD_NAME = ".log"; | ||
private static final String STRING_FORMAT_METHOD_NAME = "String.format"; | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() { | ||
return getRequiredTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return new int[] { | ||
TokenTypes.METHOD_CALL | ||
}; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST token) { | ||
String name = FullIdent.createFullIdentBelow(token).getText(); | ||
if (THROWABLE_AT_LOGGING_METHODS.stream().anyMatch(name::endsWith)) { | ||
DetailAST logMethodCall = token.getParent().getParent(); | ||
if (logMethodCall == null || logMethodCall.getType() != TokenTypes.METHOD_CALL) { | ||
return; | ||
} | ||
|
||
String nextName = FullIdent.createFullIdentBelow(logMethodCall).getText(); | ||
if (nextName.endsWith(LOG_METHOD_NAME)) { | ||
DetailAST elist = logMethodCall.findFirstToken(TokenTypes.ELIST); | ||
if (elist == null) { | ||
return; | ||
} | ||
|
||
// first param of `log()` method | ||
DetailAST logExpr = elist.findFirstToken(TokenTypes.EXPR); | ||
if (logExpr == null) { | ||
return; | ||
} | ||
|
||
DetailAST firstParam = logExpr.getFirstChild(); | ||
// flag String.format | ||
if (firstParam.getType() == TokenTypes.METHOD_CALL) { | ||
String logFirstArgMethod = FullIdent.createFullIdentBelow(logExpr.getFirstChild()).getText(); | ||
if (logFirstArgMethod.endsWith(STRING_FORMAT_METHOD_NAME)) { | ||
lmolkova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log(logExpr, ERROR_MESSAGE); | ||
} | ||
} else if (firstParam.getType() == TokenTypes.PLUS) { | ||
// flag if dynamic, i.e. a combination of string literal and ident | ||
DetailAST firstIdent = logExpr.getFirstChild().findFirstToken(TokenTypes.IDENT); | ||
DetailAST firstLiteral = logExpr.getFirstChild().findFirstToken(TokenTypes.STRING_LITERAL); | ||
if (firstIdent != null && firstLiteral != null) { | ||
log(firstParam, ERROR_MESSAGE); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...rc/test/java/com/azure/tools/checkstyle/checks/ExceptionCreatedButNotThrownCheckTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.tools.checkstyle.checks; | ||
|
||
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; | ||
import com.puppycrawl.tools.checkstyle.Checker; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ExceptionCreatedButNotThrownCheckTest extends AbstractModuleTestSupport { | ||
private Checker checker; | ||
|
||
@BeforeEach | ||
public void prepare() throws Exception { | ||
checker = createChecker(createModuleConfig(ExceptionCreatedButNotThrownCheck.class)); | ||
} | ||
|
||
@AfterEach | ||
public void cleanup() { | ||
checker.destroy(); | ||
} | ||
|
||
@Override | ||
protected String getPackageLocation() { | ||
return "com/azure/tools/checkstyle/checks/ExceptionLoggingChecks"; | ||
} | ||
|
||
@Test | ||
public void throwCreatedException() throws Exception { | ||
String[] expected = { | ||
expectedErrorMessage(8, 32, ExceptionCreatedButNotThrownCheck.ERROR_MESSAGE), | ||
}; | ||
verify(checker, getPath("ThrowCreatedExceptionCheckTestData.java"), expected); | ||
} | ||
|
||
private String expectedErrorMessage(int line, int column, String message) { | ||
return String.format("%d:%d: %s", line, column, message); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...y-reports/src/test/java/com/azure/tools/checkstyle/checks/RawExceptionThrowCheckTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.tools.checkstyle.checks; | ||
|
||
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; | ||
import com.puppycrawl.tools.checkstyle.Checker; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class RawExceptionThrowCheckTest extends AbstractModuleTestSupport { | ||
private Checker checker; | ||
|
||
@BeforeEach | ||
public void prepare() throws Exception { | ||
checker = createChecker(createModuleConfig(RawExceptionThrowCheck.class)); | ||
} | ||
|
||
@AfterEach | ||
public void cleanup() { | ||
checker.destroy(); | ||
} | ||
|
||
@Override | ||
protected String getPackageLocation() { | ||
return "com/azure/tools/checkstyle/checks/ExceptionLoggingChecks"; | ||
} | ||
|
||
@Test | ||
public void logNewExceptionTestData() throws Exception { | ||
String[] expected = { | ||
expectedErrorMessage(8, 15, RawExceptionThrowCheck.ERROR_MESSAGE), | ||
expectedErrorMessage(13, 33, RawExceptionThrowCheck.ERROR_MESSAGE), | ||
}; | ||
verify(checker, getPath("LogNewExceptionCheckTestData.java"), expected); | ||
} | ||
|
||
private String expectedErrorMessage(int line, int column, String message) { | ||
return String.format("%d:%d: %s", line, column, message); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.