You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add custom Linter to not allow improper usage of logger.Error(nil, ...) calls and fix found linter issues. (#1599)
Fix: Prevent nil errors in setupLog.Error to ensure proper logging
Closes; #1566Closes: #1556
Furthermore, it solves a similar scenario in the EventHandler code implementation found with the new custom linter check.
"Incorrect usage of 'logger.Error(nil, ...)'. The first argument must be a non-nil 'error'. "+
75
+
"Passing 'nil' results in silent failures, making debugging harder.\n\n"+
76
+
"\U0001F41B **What is wrong?**\n %s\n\n"+
77
+
"\U0001F4A1 **How to solve? Return the error, i.e.:**\n logger.Error(%s, %s, \"key\", value)\n\n",
78
+
sourceLine, suggestedError, suggestedMessage)
79
+
returntrue
80
+
}
81
+
82
+
// Ensure at least two arguments exist (error + message)
83
+
iflen(callExpr.Args) <2 {
84
+
pass.Reportf(callExpr.Pos(),
85
+
"Incorrect usage of 'logger.Error(error, ...)'. Expected at least an error and a message string.\n\n"+
86
+
"\U0001F41B **What is wrong?**\n %s\n\n"+
87
+
"\U0001F4A1 **How to solve?**\n Provide a message, e.g. logger.Error(err, \"descriptive message\")\n\n",
88
+
sourceLine)
89
+
returntrue
90
+
}
91
+
92
+
// Ensure key-value pairs (if any) are valid
93
+
if (len(callExpr.Args)-2)%2!=0 {
94
+
pass.Reportf(callExpr.Pos(),
95
+
"Incorrect usage of 'logger.Error(error, \"msg\", ...)'. Key-value pairs must be provided after the message, but an odd number of arguments was found.\n\n"+
96
+
"\U0001F41B **What is wrong?**\n %s\n\n"+
97
+
"\U0001F4A1 **How to solve?**\n Ensure all key-value pairs are complete, e.g. logger.Error(err, \"msg\", \"key\", value, \"key2\", value2)\n\n",
98
+
sourceLine)
99
+
returntrue
100
+
}
101
+
102
+
fori:=2; i<len(callExpr.Args); i+=2 {
103
+
keyArg:=callExpr.Args[i]
104
+
keyType:=pass.TypesInfo.TypeOf(keyArg)
105
+
ifkeyType==nil||keyType.String() !="string" {
106
+
pass.Reportf(callExpr.Pos(),
107
+
"Incorrect usage of 'logger.Error(error, \"msg\", key, value)'. Keys in key-value pairs must be strings, but got: %s.\n\n"+
108
+
"\U0001F41B **What is wrong?**\n %s\n\n"+
109
+
"\U0001F4A1 **How to solve?**\n Ensure keys are strings, e.g. logger.Error(err, \"msg\", \"key\", value)\n\n",
// Case 1: Nil error - Ensures the first argument cannot be nil.
13
+
logger.Error(nil, "message") // want ".*results in silent failures, making debugging harder.*"
14
+
15
+
// Case 2: Odd number of key-value arguments - Ensures key-value pairs are complete.
16
+
logger.Error(err, "message", "key1") // want ".*Key-value pairs must be provided after the message, but an odd number of arguments was found.*"
17
+
18
+
// Case 3: Key in key-value pair is not a string - Ensures keys in key-value pairs are strings.
19
+
logger.Error(err, "message", 123, value) // want ".*Ensure keys are strings.*"
20
+
21
+
// Case 4: Values are passed without corresponding keys - Ensures key-value arguments are structured properly.
22
+
logger.Error(err, "message", value, "key2", value) // want ".*Key-value pairs must be provided after the message, but an odd number of arguments was found.*"
23
+
24
+
// Case 5: Correct Usage - Should not trigger any warnings.
0 commit comments