Skip to content

Commit 14c5964

Browse files
committed
Print field name and type on panic
1 parent 6df69a1 commit 14c5964

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pkg/analysis/helpers/inspector/inspector.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ limitations under the License.
1616
package inspector
1717

1818
import (
19+
"fmt"
1920
"go/ast"
2021
"go/token"
2122

2223
astinspector "golang.org/x/tools/go/ast/inspector"
2324
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
2425
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
26+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
2527
)
2628

2729
// Inspector is an interface that allows for the inspection of fields in structs.
@@ -103,6 +105,14 @@ func (i *inspector) InspectFields(inspectField func(field *ast.Field, stack []as
103105
return false
104106
}
105107

108+
defer func() {
109+
if r := recover(); r != nil {
110+
// If the inspectField function panics, we recover and log information that will help identify the issue.
111+
debug := printDebugInfo(field)
112+
panic(fmt.Sprintf("%s %v", debug, r)) // Re-panic to propagate the error.
113+
}
114+
}()
115+
106116
inspectField(field, stack, tagInfo, i.markers)
107117

108118
return true
@@ -152,3 +162,13 @@ func isItemsType(structType *ast.StructType) bool {
152162

153163
return true
154164
}
165+
166+
// printDebugInfo prints debug information about the field that caused a panic during inspection.
167+
// This function is designed to allow us to help identify which fields are causing issues during inspection.
168+
func printDebugInfo(field *ast.Field) string {
169+
var debug string
170+
171+
debug += fmt.Sprintf("Panic observed while inspecting field: %v (type: %v)\n", utils.FieldName(field), field.Type)
172+
173+
return debug
174+
}

0 commit comments

Comments
 (0)