Better example

This commit is contained in:
Magnus Åhall 2024-09-27 07:43:50 +02:00
parent 616835f641
commit 0570e1925b

View File

@ -2,25 +2,45 @@
package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
// External
werr "git.gibonuddevalla.se/go/wrappederror"
// Standard
"errors"
"fmt"
// Standard
"encoding/json"
"errors"
"fmt"
"os"
)
func errorHandler(err werr.Error) {
// For example print or log error to file
fmt.Printf("ERROR - %s\n", err)
// For example print or log error to file
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", err)
fmt.Printf("\x1b[33;1m%s\x1b[0m\n", err.NoTrace())
j, _ := json.MarshalIndent(err, "", " ")
fmt.Printf("%s\n\n", j)
}
func foo() {
werr.SetLogCallback(errorHandler)
func main() {
// Make file paths relative to this file.
werr.Init()
err := errors.New("foobar 1")
err1 := werr.Wrap(err)
// Handler to call when using Log().
werr.SetLogCallback(errorHandler)
err2 := werr.New("foobar 2")
// Wrap an existing error.
err := errors.New("foobar 1")
err1 := werr.Wrap(err)
// Create a new error with extra information.
err2 := werr.New("foobar 2").WithCode("FOO-100").WithData(137)
// The os error contains more information.
_, errOS := os.ReadFile("/tmp/does_not_exist")
werr.Wrap(errOS).Log()
// Log the previously wrapped errors.
werr.Wrap(err1).Log()
werr.Wrap(err2).Log()
}
```