diff --git a/internal/debug/debug.go b/internal/debug/debug.go index 2936c17..905a2b3 100644 --- a/internal/debug/debug.go +++ b/internal/debug/debug.go @@ -56,6 +56,11 @@ var ( } ) +// Err2PackageID is the name ID of err2. When its API will update, e.g., v2, the +// ID must be updated as following "lainio/err2/v2", etc. See tests for +// isFuncAnchor. +const Err2PackageID = "lainio/err2" + func (si StackInfo) fullName() string { dot := "" if si.PackageName != "" && si.FuncName != "" { diff --git a/internal/debug/debug_test.go b/internal/debug/debug_test.go index 8923f56..552a327 100644 --- a/internal/debug/debug_test.go +++ b/internal/debug/debug_test.go @@ -119,28 +119,65 @@ func TestIsFuncAnchor(t *testing.T) { StackInfo{"", "", 0, PackageRegexp, nil, false}}, true}, {"short", args{ "github.com/lainio/err2.printStackIf({0x1545d2, 0x6}, 0x0, {0x12e3e0?, 0x188f50?})", - StackInfo{"", "", 0, nil, nil, false}}, true}, + StackInfo{Err2PackageID, "", 0, nil, nil, false}}, true}, {"short-but-false", args{ "github.com/lainio/err2.printStackIf({0x1545d2, 0x6}, 0x0, {0x12e3e0?, 0x188f50?})", - StackInfo{"err2", "Handle", 0, nil, nil, false}}, false}, + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, false}, {"medium", args{ "github.com/lainio/err2.Returnw(0x40000b3e60, {0x0, 0x0}, {0x0, 0x0, 0x0})", - StackInfo{"err2", "Returnw", 0, nil, nil, false}}, true}, + StackInfo{Err2PackageID, "Returnw", 0, nil, nil, false}}, true}, {"medium-but-false", args{ "github.com/lainio/err2.Returnw(0x40000b3e60, {0x0, 0x0}, {0x0, 0x0, 0x0})", - StackInfo{"err2", "Return(", 0, nil, nil, false}}, false}, + StackInfo{Err2PackageID, "Return(", 0, nil, nil, false}}, false}, {"long", args{ "github.com/lainio/err2.Handle(0x40000b3ed8, 0x40000b3ef8)", - StackInfo{"err2", "Handle", 0, nil, nil, false}}, true}, + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, true}, {"package name only", args{ "github.com/lainio/err2/try.To1[...](...)", - StackInfo{"lainio/err2", "", 0, nil, nil, false}}, true}, + StackInfo{Err2PackageID, "", 0, nil, nil, false}}, true}, + + // From bug (issue #30 and PR #31) tests, Handle name in own pkg + {"user pkg containing Handle should not match", args{ + "mainHandle.First(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, false}, + {"user function containing Handle should not match", args{ + "main.FirstHandle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, false}, + {"user function containing Handle matches err2.Handle", args{ + "github.com/lainio/err2.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, true}, + {"user function named exactly Handle should not match", args{ + "main.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, false}, + {"user package function named Handle should not match", args{ + "mypackage.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{"err2", "Handle", 0, nil, nil, false}}, false}, + {"err2.Handle should match", args{ + "err2.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{"err2", "Handle", 0, nil, nil, false}}, true}, + {"lainio/err2.Handle should match", args{ + "github.com/lainio/err2.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, true}, + {"user package with err2 in path should not match", args{ + "github.com/mycompany/err2/mypackage.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, false}, + {"non-err2 versioned package should not match", args{ + "github.com/someone/otherpkg/v2.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID, "Handle", 0, nil, nil, false}}, false}, + + // See the PackageID!! + {"versioned err2/v2.Handle should match", args{ + "github.com/lainio/err2/v2.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID + "/v2", "Handle", 0, nil, nil, false}}, true}, + {"versioned err2/v10.Handle should match", args{ + "github.com/lainio/err2/v10.Handle(0x40000b3ed8, 0x40000b3ef8)", + StackInfo{Err2PackageID + "/v10", "Handle", 0, nil, nil, false}}, true}, } for _, ttv := range tests { tt := ttv t.Run(tt.name, func(t *testing.T) { t.Parallel() - expect.Equal(t, tt.retval, tt.isFuncAnchor(tt.input)) + expect.Equal(t, tt.isFuncAnchor(tt.input), tt.retval) }) } } diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 98e3831..207e4d9 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -349,7 +349,7 @@ func doBuildFormatStr(info *Info, lvl int) (fs string, ok bool) { fnName = info.CallerName } funcName, _, _, ok := debug.FuncName(debug.StackInfo{ - PackageName: "", + PackageName: debug.Err2PackageID, // limit fn name search to err2 pkg FuncName: fnName, Level: lvl, }) diff --git a/internal/handler/handler_test.go b/internal/handler/handler_test.go index 1755a0d..0aae249 100644 --- a/internal/handler/handler_test.go +++ b/internal/handler/handler_test.go @@ -157,7 +157,7 @@ func TestPreProcess_debug(t *testing.T) { expect.ThatNot(t, nilHandlerCalled) // See the name of this test function. Decamel it + error - const want = "testing: t runner: error" + const want = "error" expect.Equal(t, myErrVal.Error(), want) resetCalled() diff --git a/samples/main-db-sample.go b/samples/db-sample.go similarity index 100% rename from samples/main-db-sample.go rename to samples/db-sample.go diff --git a/samples/main.go b/samples/main.go index 9d901e9..f7d8806 100644 --- a/samples/main.go +++ b/samples/main.go @@ -16,6 +16,7 @@ var ( "play", "runs the wanted playground: db, play, nil, assert,"+ "\nassert-keep (= uses assert.Debug in GLS),"+ + "\noverlap (= our fn names can be like MyHandle, myCatch, ..),"+ "\nplay-recursion (= runs recursion example)", ) isErr = flag.Bool("err", false, "tells if we want to have an error") @@ -41,6 +42,8 @@ func main() { } switch *mode { + case "overlap": + doOverlapMain() case "db": doDBMain() case "nil": diff --git a/samples/main-nil.go b/samples/nil.go similarity index 100% rename from samples/main-nil.go rename to samples/nil.go diff --git a/samples/overlap.go b/samples/overlap.go new file mode 100644 index 0000000..f276fb6 --- /dev/null +++ b/samples/overlap.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + + "github.com/lainio/err2" + "github.com/lainio/err2/try" +) + +func doOverlapMain() { + err := FirstHandle() + fmt.Printf("Final error: %v\n", err) +} + +func FirstHandle() (err error) { + defer err2.Handle(&err) + try.To(SecondHandle()) + return nil +} + +func SecondHandle() (err error) { + defer err2.Handle(&err) + try.T(fmt.Errorf("my error"))("my call lvl annotation") + return nil +} diff --git a/samples/main-play.go b/samples/play.go similarity index 100% rename from samples/main-play.go rename to samples/play.go