Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
51 changes: 44 additions & 7 deletions internal/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions samples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -41,6 +42,8 @@ func main() {
}

switch *mode {
case "overlap":
doOverlapMain()
case "db":
doDBMain()
case "nil":
Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions samples/overlap.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
Loading