Skip to content

go.panic_in_library

Stability High

Detects panic() calls in library code that should return errors instead.

Panics in libraries:

  • Crash calling applications — Library panic terminates the app
  • Remove caller control — Callers can’t handle errors gracefully
  • Violate Go conventions — Libraries should return errors
  • Cause production outages — One bad input crashes everything
// ❌ Before (panic in library)
func Parse(data []byte) Config {
if len(data) == 0 {
panic("empty data") // Crashes caller!
}
// ...
}
// ✅ After (returns error)
func Parse(data []byte) (Config, error) {
if len(data) == 0 {
return Config{}, errors.New("empty data")
}
// ...
}
  • panic() in exported functions
  • panic() in package without main
  • panic() for recoverable errors

Unfault converts panic to error return:

func Process(input string) (Result, error) {
if input == "" {
return Result{}, errors.New("empty input")
}
// ...
}