Always wrap errors with meaningful context using fmt.Errorf and %w verb. Include relevant identifiers (filenames, IDs, paths) in error messages to aid debugging. This helps pinpoint error sources and simplifies troubleshooting.
Always wrap errors with meaningful context using fmt.Errorf and %w verb. Include relevant identifiers (filenames, IDs, paths) in error messages to aid debugging. This helps pinpoint error sources and simplifies troubleshooting.
Example:
// Bad:
if err := os.Open(path); err != nil {
return err // Original context is lost
}
// Good:
if err := os.Open(path); err != nil {
return fmt.Errorf("failed to open config file %q: %w", path, err)
}
// Bad:
func (s *Store) SetShardNewReadersBlocked(shardID uint64, blocked bool) error {
if sh == nil {
return ErrShardNotFound // Missing context
}
}
// Good:
func (s *Store) SetShardNewReadersBlocked(shardID uint64, blocked bool) error {
if sh == nil {
return fmt.Errorf("shard not found: id=%d blocked=%v: %w",
shardID, blocked, ErrShardNotFound)
}
}
Key points:
Enter the URL of a public GitHub repository