Skip to content

Commit 0c559f2

Browse files
authored
Merge pull request #28 from lainio/bug-fix-issue-27
bug fix issue 27
2 parents bd16496 + cabad34 commit 0c559f2

File tree

8 files changed

+97
-13
lines changed

8 files changed

+97
-13
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ linters:
112112
- staticcheck
113113
- stylecheck
114114
# - tagliatelle
115-
- tenv
115+
- usetesting
116116
# - testpackage
117117
# - thelper
118118
- tparallel

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
### Version history
44

5+
##### 1.2.1
6+
- Optimization and Refactoring
7+
- Updated documentation
8+
59
##### 1.2.0
610
- Now `-err2-ret-trace` and `err2.SetErrRetTracer` gives us *error return traces*
711
which are even more readable than `-err2-trace`, `err2.SetErrorTracer` with

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,6 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md).
623623
624624
### Latest Release
625625
626-
##### 1.2.1
627-
- Optimization and Refactoring
626+
##### 1.2.2
627+
- Bug Fix (issue-27): automatic error annotation works now for try.T functions
628628
- Updated documentation

internal/debug/debug.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,25 @@ func fnName(line string) string {
246246
j += i
247247
}
248248

249-
// remove all anonumous function names (generated by compiler) like
250-
// func1.2, func1, func1.1.1.1, etc.
251-
retval, _, _ := strings.Cut(line[i:j], ".func")
252-
return retval
249+
tryFnNames := []string{".T.", ".T1[...]", ".T2[...]", ".T3[...]"}
250+
line = cleanFuncNames(line[i:j], tryFnNames, true)
251+
252+
fnNames := []string{".func", "func"}
253+
line = cleanFuncNames(line, fnNames, false)
254+
return line
255+
}
256+
257+
func cleanFuncNames(line string, names []string, concat bool) string {
258+
for _, name := range names {
259+
b, e, found := strings.Cut(line, name)
260+
if found {
261+
line = b
262+
if concat {
263+
line += e
264+
}
265+
}
266+
}
267+
return line
253268
}
254269

255270
// fnLNro returns line number in the call stack line.

internal/debug/debug_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,36 @@ func TestFnName(t *testing.T) {
206206
"github.com/findy-network/findy-agent/agent/ssi.(*DIDAgent).AssertWallet(...)",
207207
"ssi.(*DIDAgent).AssertWallet",
208208
},
209+
{
210+
"try.T simple",
211+
"main.TCopyFile.T.func(...)",
212+
"TCopyFile",
213+
},
214+
{
215+
"try.T simple A",
216+
"main.TCopyFile.T.func3(...)",
217+
"TCopyFile",
218+
},
219+
{
220+
"try.T1",
221+
"ssi.TCopyFile.T1[...].func3(...)",
222+
"ssi.TCopyFile",
223+
},
224+
{
225+
"try.T1",
226+
"main.TCopyFile.T1[...].func3(...)",
227+
"TCopyFile",
228+
},
229+
{
230+
"try.T2 in not main pkg",
231+
"github.com/findy-network/findy-agent/agent/ssi.TCopyFile.T2[...].func3(...)",
232+
"ssi.TCopyFile",
233+
},
234+
{
235+
"try.T3",
236+
"main.TCopyFile.T3[...].func3(...)",
237+
"TCopyFile",
238+
},
209239
}
210240
for _, ttv := range tests {
211241
tt := ttv

samples/main-play.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ func TryCopyFile(src, dst string) {
8888
try.To1(io.Copy(w, r))
8989
}
9090

91+
func AnnotativeCopyFile(src, dst string) (err error) {
92+
defer err2.Handle(&err)
93+
94+
r := try.T1(os.Open(src))("failed")
95+
defer r.Close()
96+
97+
w := try.T1(os.Create(dst))("failed")
98+
defer err2.Handle(&err, func(err error) error {
99+
try.Out(os.Remove(dst)).Logf()
100+
return err
101+
})
102+
defer w.Close()
103+
104+
try.T1(io.Copy(w, r))("failed")
105+
return nil
106+
}
107+
91108
func CallRecur(d int) (ret int, err error) {
92109
defer err2.Handle(&err)
93110

@@ -191,8 +208,10 @@ func doMain() (err error) {
191208
}
192209
} else {
193210
// 2nd argument is empty to assert
194-
TryCopyFile("main.go", "")
195-
//try.To(CopyFile("main.go", ""))
211+
//TryCopyFile("main.go", "")
212+
213+
// testing try.T function removal from annotation
214+
try.To(AnnotativeCopyFile("main.go", ""))
196215
}
197216

198217
fmt.Println("=== you cannot see this ===")

try/try.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ func IsNotEnabled(err error) bool {
261261
// T is similar as [To] but it let's you to annotate a possible error at place.
262262
//
263263
// try.T(f.Close)("annotations")
264+
//
265+
// Note that T is a helper, which means that you start with it randomly. You
266+
// start with [To] and end up using T if you really need to add context
267+
// related to a specific error check, which is a very rare case.
264268
func T(err error) func(fs string) {
265269
return func(fs string) {
266270
if err == nil {
@@ -273,6 +277,10 @@ func T(err error) func(fs string) {
273277
// T1 is similar as [To1] but it let's you to annotate a possible error at place.
274278
//
275279
// f := try.T1(os.Open("filename")("cannot open cfg file")
280+
//
281+
// Note that T1 is a helper, which means that you start with it randomly. You
282+
// start with [To1] and end up using T1 if you really need to add context
283+
// related to a specific error check, which is a very rare case.
276284
func T1[T any](v T, err error) func(fs string) T {
277285
return func(fs string) T {
278286
if err == nil {
@@ -283,6 +291,10 @@ func T1[T any](v T, err error) func(fs string) T {
283291
}
284292

285293
// T2 is similar as [To2] but it let's you to annotate a possible error at place.
294+
//
295+
// Note that T2 is a helper, which means that you start with it randomly. You
296+
// start with [To2] and end up using T2 if you really need to add context
297+
// related to a specific error check, which is a very rare case.
286298
func T2[T, U any](v T, u U, err error) func(fs string) (T, U) {
287299
return func(fs string) (T, U) {
288300
if err == nil {
@@ -298,6 +310,10 @@ func annotateErr(err error, fs string) error {
298310
}
299311

300312
// T3 is similar as [To3] but it let's you to annotate a possible error at place.
313+
//
314+
// Note that T3 is a helper, which means that you start with it randomly. You
315+
// start with [To3] and end up using T3 if you really need to add context
316+
// related to a specific error check, which is a very rare case.
301317
func T3[T, U, V any](v1 T, v2 U, v3 V, err error) func(fs string) (T, U, V) {
302318
return func(fs string) (T, U, V) {
303319
if err == nil {

try/try_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ func ExampleIsEOF1() {
105105

106106
func Example_copyFile() {
107107
copyFile := func(src, dst string) (err error) {
108-
defer err2.Handle(&err, "copy file %s %s", src, dst)
108+
defer err2.Handle(&err, "copy")
109109

110110
// These try package helpers are as fast as Check() calls which is as
111111
// fast as `if err != nil {}`
112112

113-
r := try.To1(os.Open(src))
113+
r := try.T1(os.Open(src))("source file")
114114
defer r.Close()
115115

116-
w := try.To1(os.Create(dst))
116+
w := try.T1(os.Create(dst))("target file")
117117
defer err2.Handle(&err, err2.Err(func(error) {
118118
os.Remove(dst)
119119
}))
@@ -126,5 +126,5 @@ func Example_copyFile() {
126126
if err != nil {
127127
fmt.Println(err)
128128
}
129-
// Output: copy file /notfound/path/file.go /notfound/path/file.bak: open /notfound/path/file.go: no such file or directory
129+
// Output: copy: source file: open /notfound/path/file.go: no such file or directory
130130
}

0 commit comments

Comments
 (0)