Aller au contenu

Wrapping Loses Cause (ARCH5154)

Ce contenu n’est pas encore disponible dans votre langue.

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.

func load(id string) (*User, error) {
u, err := db.Get(id)
if err != nil {
return nil, errors.New("user lookup failed")
}
return u, nil
}
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.