Skip to content

Commit d1bd41d

Browse files
committed
new samles to test certain features
1 parent 958efb0 commit d1bd41d

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ PKG_ERR2 := github.com/lainio/err2
44
PKG_ASSERT := github.com/lainio/err2/assert
55
PKG_TRY := github.com/lainio/err2/try
66
PKG_DEBUG := github.com/lainio/err2/internal/debug
7+
PKG_HANDLER := github.com/lainio/err2/internal/handler
78
PKG_STR := github.com/lainio/err2/internal/str
89
PKG_X := github.com/lainio/err2/internal/x
9-
PKGS := $(PKG_ERR2) $(PKG_ASSERT) $(PKG_TRY) $(PKG_DEBUG) $(PKG_STR) $(PKG_X)
10+
PKGS := $(PKG_ERR2) $(PKG_ASSERT) $(PKG_TRY) $(PKG_DEBUG) $(PKG_HANDLER) $(PKG_STR) $(PKG_X)
1011

1112
SRCDIRS := $(shell go list -f '{{.Dir}}' $(PKGS))
1213

@@ -29,6 +30,9 @@ test_try:
2930
test_debug:
3031
$(GO) test $(TEST_ARGS) $(PKG_DEBUG)
3132

33+
test_handler:
34+
$(GO) test $(TEST_ARGS) $(PKG_HANDLER)
35+
3236
test_str:
3337
$(GO) test $(TEST_ARGS) $(PKG_STR)
3438

samples/main-nil.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"os"
6+
7+
"github.com/lainio/err2"
8+
"github.com/lainio/err2/try"
9+
"golang.org/x/exp/slog"
10+
)
11+
12+
var (
13+
opts = slog.HandlerOptions{
14+
Level: slog.LevelDebug,
15+
}
16+
17+
errAddNode = errors.New("add node error")
18+
myErr error = nil
19+
20+
logger *slog.Logger
21+
)
22+
23+
func Init() {
24+
textHandler := opts.NewTextHandler(os.Stdout)
25+
logger = slog.New(textHandler)
26+
slog.SetDefault(logger)
27+
if *isErr {
28+
myErr = errAddNode
29+
}
30+
}
31+
32+
func doMainAll() {
33+
logger.Info("=== 1. preferred successful status output ===")
34+
doMain1()
35+
logger.Info("=== 2. NilThen and try.To successful status ===")
36+
doMain2()
37+
logger.Info("=== 3. NilThen and try.Out successful status ===")
38+
doMain3()
39+
40+
logger.Info("=== ERROR status versions ===")
41+
myErr = errAddNode
42+
logger.Info("=== 1. preferred successful status output ===")
43+
doMain1()
44+
logger.Info("=== 2. NilThen and try.To successful status ===")
45+
doMain2()
46+
logger.Info("=== 3. NilThen and try.Out successful status ===")
47+
doMain3()
48+
}
49+
func doMain3() {
50+
defer err2.Catch("CATCH")
51+
logger.Debug("3: ADD node")
52+
defer NilThen(func() {
53+
logger.Debug("3: add node successful")
54+
})
55+
try.Out(AddNode()).Logf("3: no error handling, only logging")
56+
}
57+
58+
func doMain2() {
59+
defer err2.Catch("CATCH")
60+
logger.Debug("2: ADD node")
61+
defer NilThen(func() {
62+
logger.Debug("2: add node successful")
63+
})
64+
65+
try.To(AddNode())
66+
}
67+
68+
func doMain1() {
69+
defer err2.Catch("CATCH")
70+
logger.Debug("1: ADD node")
71+
72+
try.To(AddNode())
73+
logger.Debug("1: add node successful")
74+
}
75+
76+
func AddNode() error { return myErr }
77+
78+
func NilThen(fn func()) {
79+
if r := recover(); r != nil {
80+
panic(r)
81+
}
82+
fn()
83+
}
84+

samples/main-play.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ import (
2121
// CopyFile copies the source file to the given destination. If any error occurs it
2222
// returns an error value describing the reason.
2323
func CopyFile(src, dst string) (err error) {
24+
defer err2.Handle(&err)
25+
26+
r := try.To1(os.Open(src))
27+
defer r.Close()
28+
29+
w := try.To1(os.Create(dst))
30+
defer err2.Handle(&err, func() {
31+
try.Out(os.Remove(dst)).Logf()
32+
})
33+
defer w.Close()
34+
35+
try.To1(io.Copy(w, r))
36+
return nil
37+
}
38+
39+
// OrgCopyFile copies the source file to the given destination. If any error occurs it
40+
// returns an error value describing the reason.
41+
func OrgCopyFile(src, dst string) (err error) {
2442
defer err2.Handle(&err) // automatic error message: see err2.Formatter
2543
// You can out-comment above handler line(s) to see what happens.
2644

@@ -112,7 +130,7 @@ func doMain() (err error) {
112130
// how err2 works. Especially interesting is automatic stack tracing.
113131
//
114132
// source file exists, but the destination is not in high probability
115-
//try.To(CopyFile("main.go", "/notfound/path/file.bak"))
133+
try.To(CopyFile("main.go", "/notfound/path/file.bak"))
116134

117135
// Both source and destination don't exist
118136
//try.To(CopyFile("/notfound/path/file.go", "/notfound/path/file.bak"))

samples/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ import (
88
)
99

1010
var (
11-
mode = flag.String("mode", "play", "runs the wanted playground: db, play,")
11+
mode = flag.String("mode", "play", "runs the wanted playground: db, play, nil")
12+
isErr = flag.Bool("err", false, "tells if we have error")
1213
)
1314

1415
func main() {
1516
defer err2.Catch()
1617
log.SetFlags(log.Lshortfile | log.LstdFlags)
1718

1819
flag.Parse()
20+
Init()
1921

2022
switch *mode {
2123
case "db":
2224
doDBMain()
25+
case "nil":
26+
doMainAll()
27+
case "nil1":
28+
doMain1()
29+
case "nil2":
30+
doMain2()
2331
case "play":
2432
doPlayMain()
2533
default:

0 commit comments

Comments
 (0)