Skip to content

Commit

Permalink
Add README example (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Jun 11, 2023
1 parent 9f16be8 commit fd3d73c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
20 changes: 20 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fd3d73c

Please sign in to comment.