Error transformation converts an error into another error without preserving the original for unwrapping. Use it when exposing the source error would create unwanted coupling. section 7.2
Choose the handling strategy according to what callers should access:
- Return the error directly when no additional context or classification is needed. section 7.2
- Wrap with fmt.Errorf("bar failed: %w", err) when callers should retain access to the source error. section 7.2
- Transform with fmt.Errorf("bar failed: %v", err) when adding context without making the source error available. section 7.2
Preventing error unwrapping
if err != nil { return fmt.Errorf("bar failed: %v", err) } The resulting message retains the context and text but does not expose the original error for unwrapping. section 7.2
Use the error-conversion figure to compare transformed errors with wrapped errors. section 7.2
