README.md improved

This commit is contained in:
Timon Ringwald
2020-09-09 14:06:31 +02:00
parent b33b40c9ff
commit 37acf646f4
5 changed files with 99 additions and 9 deletions

View File

@ -83,4 +83,77 @@ func doStuffWrapped() error {
fmt.Println(adverr.Trace())
```
###
### Example of a printed error
Code:
```go
package main
import (
"adverr"
"errors"
)
var (
ErrDoStuffFailed = adverr.NewErrTmpl("ErrDoStuffFailed", "Could'nt do stuff because of %s")
)
func main() {
err := doStuffInAnotherGoroutine()
if err != nil {
adverr.Fatalln(err, 1)
}
}
func doStuff() error {
err := doGoNativeStuff()
if err != nil {
return ErrDoStuffFailed.Wrap(err, "reasons")
}
return nil
}
func doStuffInAnotherGoroutine() error {
ch := make(chan error, 1)
go func() {
ch <- doStuff()
close(ch)
}()
err := <-ch
if err != nil {
return adverr.Wrap("Goroutine failed because of errors", err)
}
return nil
}
func doGoNativeStuff() error {
return errors.New("some go error")
}
```
Output:
```
adverr.Error: Goroutine failed because of errors
at main.doStuffInAnotherGoroutine (/home/user/go/src/test/main.go:38)
at main.main (/home/user/go/src/test/main.go:13)
at runtime.main (/usr/local/go/src/runtime/proc.go:204)
Caused by ErrDoStuffFailed: Could'nt do stuff because of reasons
at main.doStuff (/home/user/go/src/test/main.go:22)
at main.doStuffInAnotherGoroutine.func1 (/home/user/go/src/test/main.go:32)
Caused by errors.errorString: some go error
(Unknown source)
```
### Globals
You can set the maximum limit of the call stack trace via
```go
adverr.CallStackLength = 50 // default value: 100
```
If you are in a productive environment, consider disabling call traces completely for performance reasons:
```go
adverr.DisableTrace = true // default value: false
```