WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@rgeraskin
Copy link

Summary

  • Fix error annotation when user functions contain the substring “handle” by making function-anchor detection precise.
  • Ensures err2.Handle stack anchors are matched only for the err2 package (including versioned modules), not for user functions like FirstHandle or main.Handle.
  • Closes issue #30.

Background

When using err2.Handle(&err) inside functions whose names contain “handle”, error annotations showed incorrect function names (e.g., main: main:). See Bug: Error annotations fail when user functions contain "Handle" in their name.

Changes

  • Tightened isFuncAnchor logic in err2/internal/debug/debug.go:
    • Match only at a dot boundary.
    • Require the package token immediately before the dot to be exactly err2, or a version token vN preceded by err2 (supports .../err2/v2.Handle).
    • Single O(n) scan with strings.Index, no regex construction.
  • Added helper isErr2BeforeDot to validate the package token.
  • No changes to handler logic; only stack parsing is updated.

Why this approach

  • Prevents substring collisions (e.g., “FirstHandle(”).
  • Correctly identifies err2.Handle and versioned err2/vN.Handle.
  • Avoids risky broad checks (like searching for /err2/ anywhere before).
  • Maintains Go 1.18 compatibility (uses strings.LastIndex).

Tests

Added/extended cases in err2/internal/debug/debug_test.go to cover:

  • Matches:
    • err2.Handle(...)
    • github.com/lainio/err2.Handle(...)
    • github.com/lainio/err2/v2.Handle(...)
    • github.com/lainio/err2/v10.Handle(...)
  • Non-matches:
    • main.Handle(...)
    • mypackage.Handle(...)
    • main.FirstHandle(...)
    • github.com/myco/err2/mypackage.Handle(...)

Example

Before:

  • Final error: main: main: my error

After:

  • Final error: first handle: second handle: my error

Compatibility and Performance

  • Backwards compatible with existing behavior for err2 stack parsing.
  • O(n) per line; no new allocations beyond substring indexing.

Risks

  • Low: Narrowed detection to dot boundaries and err2-scoped packages; tests cover edge cases.

Links

@lainio
Copy link
Owner

lainio commented Oct 16, 2025

Thank you! I really appreciate your work. However, I'm a little confused with the fix, and that's why I have created a separate branch that fixes the bug just by changing one line in the file internal/handler/handler.go.

The complete fix can be seen in the branch fix-candidate-for-bug-30. I think we should continue the discussion here.

I apologize for not having had time to comment on this earlier.

@rgeraskin
Copy link
Author

Thanks! I checked out your branch. In my opinion, your solution is much simpler and cleaner, and it resolves the issue. Also, I see that the same tests as for this PR pass for your branch. So I see no reason to accept this PR🙂

Looking forward to your fix being merged into master. Thanks!

@lainio
Copy link
Owner

lainio commented Nov 14, 2025

Closing this. Fix done in #33.

@lainio lainio closed this Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Error annotations fail when user functions contain "Handle" in their name

2 participants