From fd3d73c66bf4229d6bc262c25478181f8d753f2c Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Sun, 11 Jun 2023 12:51:09 +0200 Subject: [PATCH] Add README example (#1) --- README.md | 13 +++++++++++++ example_test.go | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/README.md b/README.md index f70a981..44d15f8 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,19 @@ go get github.com/cristalhq/errorx ## Example ```go +err := errorx.Newf("this is the error") +if err != nil { + return errorx.Wrapf(err, "something happened") +} + +errAt := errorx.Newf("happened at: %s", time.Now()) +if errAt != nil { + return errorx.Trace(err) +} + +if errorx.Tracing() { + println("error tracing is enabled") +} ``` See examples: [example_test.go](example_test.go). diff --git a/example_test.go b/example_test.go index d480cda..effaae1 100644 --- a/example_test.go +++ b/example_test.go @@ -3,10 +3,30 @@ package errorx_test import ( "fmt" "os" + "time" "github.com/cristalhq/errorx" ) +func Example() { + func() error { + err := errorx.Newf("this is the error") + if err != nil { + return errorx.Wrapf(err, "something happened") + } + + errAt := errorx.Newf("happened at: %s", time.Now()) + if errAt != nil { + return errorx.Trace(err) + } + + if errorx.Tracing() { + println("error tracing is enabled") + } + return nil + }() +} + func ExampleNewf() { err := errorx.Newf("this is the error") err2 := errorx.Newf("this is the error, with code: %d", 123)