Skip to content

Commit 30032f8

Browse files
committed
Merge branch 'bench-tests' of ssh://g.yxqyang.asia-lainio/lainio/err2 into bench-tests
2 parents ac8f630 + c038678 commit 30032f8

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

err2_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package err2_test
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
@@ -14,18 +15,17 @@ import (
1415

1516
const errStringInThrow = "this is an ERROR"
1617

17-
func throw() (string, error) {
18-
return "", fmt.Errorf(errStringInThrow)
19-
}
18+
var (
19+
errToTest = errors.New(errStringInThrow)
20+
)
21+
22+
func throw() (string, error) { return "", errToTest }
23+
func noThrow() (string, error) { return "test", nil }
24+
func noErr() error { return nil }
2025

2126
func twoStrNoThrow() (string, string, error) { return "test", "test", nil }
2227
func intStrNoThrow() (int, string, error) { return 1, "test", nil }
2328
func boolIntStrNoThrow() (bool, int, string, error) { return true, 1, "test", nil }
24-
func noThrow() (string, error) { return "test", nil }
25-
26-
func noErr() error {
27-
return nil
28-
}
2929

3030
func TestTry_noError(t *testing.T) {
3131
t.Parallel()

samples/main-play.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
package main
1010

1111
import (
12+
"flag"
1213
"fmt"
1314
"io"
1415
"os"
16+
"strconv"
1517

1618
"github.com/lainio/err2"
1719
"github.com/lainio/err2/assert"
@@ -90,22 +92,23 @@ func OrgCopyFile(src, dst string) (err error) {
9092
return nil
9193
}
9294

93-
func CallRecur(d int) (err error) {
95+
func CallRecur(d int) (ret int, err error) {
9496
defer err2.Handle(&err)
9597

9698
return doRecur(d)
9799
}
98100

99-
func doRecur(d int) (err error) {
101+
func doRecur(d int) (ret int, err error) {
100102
d--
101103
if d >= 0 {
102104
// Keep below to show how asserts work
103-
assert.NotZero(d)
105+
//assert.NotZero(d)
104106
// Comment out the above assert statement to simulate runtime-error
105-
fmt.Println(10 / d)
106-
return doRecur(d)
107+
ret = 10 / d
108+
fmt.Println(ret)
109+
//return doRecur(d)
107110
}
108-
return fmt.Errorf("root error")
111+
return ret, fmt.Errorf("root error")
109112
}
110113

111114
func doPlayMain() {
@@ -136,7 +139,7 @@ func doPlayMain() {
136139
doDoMain()
137140
//try.To(doMain())
138141

139-
println("___ happy ending ===")
142+
fmt.Println("___ happy ending ===")
140143
}
141144

142145
func doDoMain() {
@@ -163,13 +166,20 @@ func doMain() (err error) {
163166
// Both source and destination don't exist
164167
//try.To(OrgCopyFile("/notfound/path/file.go", "/notfound/path/file.bak"))
165168

166-
// 2nd argument is empty
167-
try.To(OrgCopyFile("main.go", ""))
168-
169-
// Next fn demonstrates how error and panic traces work, comment out all
170-
// above CopyFile calls to play with:
171-
try.To(CallRecur(1))
169+
// to play with real args:
170+
try.To(CopyFile(flag.Arg(0), flag.Arg(1)))
171+
172+
if len(flag.Args()) > 0 {
173+
// Next fn demonstrates how error and panic traces work, comment out all
174+
// above CopyFile calls to play with:
175+
argument := try.To1(strconv.Atoi(flag.Arg(0)))
176+
ret := try.To1(CallRecur(argument))
177+
fmt.Println("ret val:", ret)
178+
} else {
179+
// 2nd argument is empty to assert
180+
try.To(OrgCopyFile("main.go", ""))
181+
}
172182

173-
println("=== you cannot see this ===")
183+
fmt.Println("=== you cannot see this ===")
174184
return nil
175185
}

0 commit comments

Comments
 (0)