Compare commits
No commits in common. "main" and "v0.3.5" have entirely different histories.
2 changed files with 12 additions and 80 deletions
44
README.md
44
README.md
|
@ -2,45 +2,25 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// External
|
// External
|
||||||
werr "git.gibonuddevalla.se/go/wrappederror"
|
werr "git.gibonuddevalla.se/go/wrappederror"
|
||||||
|
|
||||||
// Standard
|
// Standard
|
||||||
"encoding/json"
|
"errors"
|
||||||
"errors"
|
"fmt"
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func errorHandler(err werr.Error) {
|
func errorHandler(err werr.Error) {
|
||||||
// For example print or log error to file
|
// For example print or log error to file
|
||||||
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", err)
|
fmt.Printf("ERROR - %s\n", err)
|
||||||
fmt.Printf("\x1b[33;1m%s\x1b[0m\n", err.NoTrace())
|
|
||||||
|
|
||||||
j, _ := json.MarshalIndent(err, "", " ")
|
|
||||||
fmt.Printf("%s\n\n", j)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func foo() {
|
||||||
// Make file paths relative to this file.
|
werr.SetLogCallback(errorHandler)
|
||||||
werr.Init()
|
|
||||||
|
|
||||||
// Handler to call when using Log().
|
err := errors.New("foobar 1")
|
||||||
werr.SetLogCallback(errorHandler)
|
err1 := werr.Wrap(err)
|
||||||
|
|
||||||
// Wrap an existing error.
|
err2 := werr.New("foobar 2")
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
48
pkg.go
48
pkg.go
|
@ -1,51 +1,3 @@
|
||||||
/*
|
|
||||||
wrappederror provides traceable errors:
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
// External
|
|
||||||
werr "git.gibonuddevalla.se/go/wrappederror"
|
|
||||||
|
|
||||||
// Standard
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func errorHandler(err werr.Error) {
|
|
||||||
// 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 main() {
|
|
||||||
// Make file paths relative to this file.
|
|
||||||
werr.Init()
|
|
||||||
|
|
||||||
// Handler to call when using Log().
|
|
||||||
werr.SetLogCallback(errorHandler)
|
|
||||||
|
|
||||||
// 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()
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
package WrappedError
|
package WrappedError
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
Loading…
Add table
Reference in a new issue