errors.Is keeps returning false even though I wrapped it with %w Help
My storage package has var ErrNotFound = errors.New("not found"). The repo layer returns fmt.Errorf("loading user %d: %w", id, ErrNotFound) and the handler checks errors.Is(err, storage.ErrNotFound) to decide 404 vs 500. It works on the direct path but returns false whenever the call goes through a small retry helper I wrote. Go 1.22, no build tags, nothing clever.
@committee_kim · 2mo ago · 3 replies
Look at the retry helper. Nine times out of ten it does something like
fmt.Errorf("after %d attempts: %s", n, err)orerrors.New(err.Error()), and%sflattens the error into a string. The chain is gone at that point, so Is has nothing to walk. Change that one verb to%wand it will start working.Reply
Report
@printbed_gremlin · 2mo ago
Yep.
fmt.Errorf("giving up after %d attempts: %s", attempts, lastErr). I have been staring at the repo layer for two days.Reply
Report
@easy_pace_ellis · 2mo ago
There's a vet check for this now,
go vetflags a%son an error in a Errorf that also wraps. Worth wiring into CI so nobody reintroduces it in six months.Reply
Report