nopanic
About 235 wordsLess than 1 minute
2025-01-16
Ensures library code returns errors instead of panicking.
Category
Safety
What It Checks
This analyzer detects panic() calls in library code that should return errors instead.
Why It Matters
Libraries that panic crash their callers:
// Caller's code crashes unexpectedly
result := yourlib.Process(data) // panic!Libraries should return errors, letting callers decide how to handle them:
result, err := yourlib.Process(data)
if err != nil {
// Caller handles it appropriately
}Examples
Bad: Library Panics
// mylib/processor.go
func Process(data []byte) Result {
if len(data) == 0 {
panic("data cannot be empty") // Crashes caller!
}
// ...
}Good: Library Returns Error
// mylib/processor.go
func Process(data []byte) (Result, error) {
if len(data) == 0 {
return Result{}, errors.New("data cannot be empty")
}
// ...
}Allowed: Panic in main/init
// cmd/myapp/main.go
func main() {
if err := run(); err != nil {
panic(err) // OK in main
}
}
func init() {
if os.Getenv("REQUIRED") == "" {
panic("REQUIRED env var not set") // OK in init
}
}Allowed: Unreachable Code
func processType(t Type) string {
switch t {
case TypeA:
return "a"
case TypeB:
return "b"
default:
panic("unreachable") // OK - indicates bug in caller
}
}Configuration
# .golint-sl.yaml
analyzers:
nopanic: true # enabled by defaultWhen to Disable
- Application code (not a library)
- Internal packages not meant for external use
analyzers:
nopanic: false