diff --git a/Makefile b/Makefile index 007cf8f..11abb03 100644 --- a/Makefile +++ b/Makefile @@ -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)) @@ -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) diff --git a/samples/main-nil.go b/samples/main-nil.go new file mode 100644 index 0000000..f5303a4 --- /dev/null +++ b/samples/main-nil.go @@ -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() +} + diff --git a/samples/main-play.go b/samples/main-play.go index 41eca8c..a26cee7 100644 --- a/samples/main-play.go +++ b/samples/main-play.go @@ -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. @@ -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")) diff --git a/samples/main.go b/samples/main.go index f583dc7..15b07bb 100644 --- a/samples/main.go +++ b/samples/main.go @@ -8,7 +8,8 @@ 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() { @@ -16,10 +17,17 @@ func main() { 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: