Skip to content

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

Description

@rgeraskin

Description

When using err2.Handle(&err) in functions whose names contain the substring "handle" (case-insensitive), error annotations show incorrect function names in the error chain. Instead of displaying the actual function names, it shows main: main: repeatedly.

Steps to Reproduce

package main

import (
	"fmt"
	"github.com/lainio/err2"
	"github.com/lainio/err2/try"
)

func FirstHandle() (err error) {
	defer err2.Handle(&err)
	try.To(SecondHandle())
	return nil
}

func SecondHandle() (err error) {
	defer err2.Handle(&err)
	try.To(fmt.Errorf("my error"))
	return nil
}

func main() {
	err := FirstHandle()
	fmt.Printf("Final error: %v\n", err)
}

Current (incorrect) output:

Final error: main: main: my error

Expected output:

Final error: first handle: second handle: my error

Root Cause

The isFuncAnchor function in internal/debug/debug.go uses simple substring matching to find "Handle" in stack traces. This causes it to incorrectly match user functions like "FirstHandle" or "MyHandler" when searching for the err2.Handle function, leading to incorrect stack trace parsing and error annotation.

Workaround

Currently, the only workaround is to avoid using "handle" (case-insensitive) as a substring in function names when using err2.Handle. Or to annotate errors manually.

Environment

  • Go version: 1.24.4
  • err2 version: v1.2.2

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions