Skip to content

Commit bafc69c

Browse files
update readme
1 parent 3f46da0 commit bafc69c

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,58 @@
11
# errorstack
22

3-
A lightweight package to get stacktrace of go errors. Designed to work with go1.21+
3+
A lightweight package to get stacktrace of go errors. Designed to work with `errors` standard package as of go1.20+. It also works well with `zap.Error` field to log error stacktrace.
44

55
This is a fork of [pkg/errors](https://github.com/pkg/errors) which is archived now. The package has been simplified to provide only stacktrace functionality.
66

77
## Usage
8+
```go
9+
package main
10+
11+
import (
12+
"errors"
13+
"fmt"
14+
15+
"github.com/ahmadelsagheer/errorstack"
16+
"go.uber.org/zap"
17+
)
18+
19+
func f1() error {
20+
return f2()
21+
}
22+
23+
func f2() error {
24+
return f3()
25+
}
26+
27+
func f3() error {
28+
return errorstack.WithStack(errors.New("f3 error"))
29+
}
30+
31+
func main() {
32+
33+
err := f1()
34+
35+
fmt.Printf("%v\n", err)
36+
// f3 error
37+
fmt.Printf("--\n")
38+
39+
fmt.Printf("%+v\n", err)
40+
// main.f3
41+
// /Users/ahmad/codespace/errorstackexample/main.go:20
42+
// main.f2
43+
// /Users/ahmad/codespace/errorstackexample/main.go:16
44+
// main.f1
45+
// /Users/ahmad/codespace/errorstackexample/main.go:12
46+
// main.main
47+
// /Users/ahmad/codespace/errorstackexample/main.go:25
48+
// runtime.main
49+
// /usr/local/Cellar/go/1.21.0/libexec/src/runtime/proc.go:267
50+
// runtime.goexit
51+
// /usr/local/Cellar/go/1.21.0/libexec/src/runtime/asm_amd64.s:1650
52+
fmt.Printf("--\n")
53+
54+
logger := zap.NewExample()
55+
logger.Error("error occurred", zap.Error(err))
56+
// {"level":"error","msg":"error occurred","error":"f3 error","errorVerbose":"f3 error\nmain.f3\n\t/Users/ahmad/codespace/errorstackexample/main.go:20\nmain.f2\n\t/Users/ahmad/codespace/errorstackexample/main.go:16\nmain.f1\n\t/Users/ahmad/codespace/errorstackexample/main.go:12\nmain.main\n\t/Users/ahmad/codespace/errorstackexample/main.go:25\nruntime.main\n\t/usr/local/Cellar/go/1.21.0/libexec/src/runtime/proc.go:267\nruntime.goexit\n\t/usr/local/Cellar/go/1.21.0/libexec/src/runtime/asm_amd64.s:1650"}
57+
}
58+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/ahmadelsagheer/errorstack
22

3-
go 1.21.0
3+
go 1.20.0

0 commit comments

Comments
 (0)