Skip to content

Commit

Permalink
new samles to test certain features
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Aug 17, 2023
1 parent 958efb0 commit d1bd41d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ PKG_ERR2 := github.com/lainio/err2
PKG_ASSERT := github.com/lainio/err2/assert
PKG_TRY := github.com/lainio/err2/try
PKG_DEBUG := github.com/lainio/err2/internal/debug
PKG_HANDLER := github.com/lainio/err2/internal/handler
PKG_STR := github.com/lainio/err2/internal/str
PKG_X := github.com/lainio/err2/internal/x
PKGS := $(PKG_ERR2) $(PKG_ASSERT) $(PKG_TRY) $(PKG_DEBUG) $(PKG_STR) $(PKG_X)
PKGS := $(PKG_ERR2) $(PKG_ASSERT) $(PKG_TRY) $(PKG_DEBUG) $(PKG_HANDLER) $(PKG_STR) $(PKG_X)

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

Expand All @@ -29,6 +30,9 @@ test_try:
test_debug:
$(GO) test $(TEST_ARGS) $(PKG_DEBUG)

test_handler:
$(GO) test $(TEST_ARGS) $(PKG_HANDLER)

test_str:
$(GO) test $(TEST_ARGS) $(PKG_STR)

Expand Down
84 changes: 84 additions & 0 deletions samples/main-nil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"errors"
"os"

"github.com/lainio/err2"
"github.com/lainio/err2/try"
"golang.org/x/exp/slog"
)

var (
opts = slog.HandlerOptions{
Level: slog.LevelDebug,
}

errAddNode = errors.New("add node error")
myErr error = nil

logger *slog.Logger
)

func Init() {
textHandler := opts.NewTextHandler(os.Stdout)
logger = slog.New(textHandler)
slog.SetDefault(logger)
if *isErr {
myErr = errAddNode
}
}

func doMainAll() {
logger.Info("=== 1. preferred successful status output ===")
doMain1()
logger.Info("=== 2. NilThen and try.To successful status ===")
doMain2()
logger.Info("=== 3. NilThen and try.Out successful status ===")
doMain3()

logger.Info("=== ERROR status versions ===")
myErr = errAddNode
logger.Info("=== 1. preferred successful status output ===")
doMain1()
logger.Info("=== 2. NilThen and try.To successful status ===")
doMain2()
logger.Info("=== 3. NilThen and try.Out successful status ===")
doMain3()
}
func doMain3() {
defer err2.Catch("CATCH")
logger.Debug("3: ADD node")
defer NilThen(func() {
logger.Debug("3: add node successful")
})
try.Out(AddNode()).Logf("3: no error handling, only logging")
}

func doMain2() {
defer err2.Catch("CATCH")
logger.Debug("2: ADD node")
defer NilThen(func() {
logger.Debug("2: add node successful")
})

try.To(AddNode())
}

func doMain1() {
defer err2.Catch("CATCH")
logger.Debug("1: ADD node")

try.To(AddNode())
logger.Debug("1: add node successful")
}

func AddNode() error { return myErr }

func NilThen(fn func()) {
if r := recover(); r != nil {
panic(r)
}
fn()
}

20 changes: 19 additions & 1 deletion samples/main-play.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ import (
// CopyFile copies the source file to the given destination. If any error occurs it
// returns an error value describing the reason.
func CopyFile(src, dst string) (err error) {
defer err2.Handle(&err)

r := try.To1(os.Open(src))
defer r.Close()

w := try.To1(os.Create(dst))
defer err2.Handle(&err, func() {
try.Out(os.Remove(dst)).Logf()
})
defer w.Close()

try.To1(io.Copy(w, r))
return nil
}

// OrgCopyFile copies the source file to the given destination. If any error occurs it
// returns an error value describing the reason.
func OrgCopyFile(src, dst string) (err error) {
defer err2.Handle(&err) // automatic error message: see err2.Formatter
// You can out-comment above handler line(s) to see what happens.

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

// Both source and destination don't exist
//try.To(CopyFile("/notfound/path/file.go", "/notfound/path/file.bak"))
Expand Down
10 changes: 9 additions & 1 deletion samples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ import (
)

var (
mode = flag.String("mode", "play", "runs the wanted playground: db, play,")
mode = flag.String("mode", "play", "runs the wanted playground: db, play, nil")
isErr = flag.Bool("err", false, "tells if we have error")
)

func main() {
defer err2.Catch()
log.SetFlags(log.Lshortfile | log.LstdFlags)

flag.Parse()
Init()

switch *mode {
case "db":
doDBMain()
case "nil":
doMainAll()
case "nil1":
doMain1()
case "nil2":
doMain2()
case "play":
doPlayMain()
default:
Expand Down

0 comments on commit d1bd41d

Please sign in to comment.