Wrapping Loses Cause (ARCH5154)
Esta página aún no está disponible en tu idioma.
Key go.logging.error-wrap-loses-cause · Severity warning · Category logging
Returning a new error inside a handler without wrapping the original discards the root cause and the call chain. The %w verb preserves the original error so it stays inspectable via errors.Is/errors.As.
Incorrect
Section titled “Incorrect”func load(id string) (*User, error) { u, err := db.Get(id) if err != nil { return nil, errors.New("user lookup failed") } return u, nil}Correct
Section titled “Correct”func load(id string) (*User, error) { u, err := db.Get(id) if err != nil { return nil, fmt.Errorf("user lookup %s: %w", id, err) } return u, nil}When re-returning an error, wrap the original with fmt.Errorf using the %w verb.