Documentation ¶
Overview ¶
Package errors implements functions to manipulate errors.
Example ¶
package main import ( "fmt" "github.com/chai2010/errors" ) func main() { err0 := errors.New("err0") err1 := errors.Wrap(err0, "err1") err2 := func() error { return errors.Wrap(err1, "err2") }() err3 := func() error { return errors.Wrap(err2, "err3") }() fmt.Println(err0) fmt.Println(err1) fmt.Println(err2) fmt.Println(err3) }
Output: err0 err1 -> {err0} err2 -> {err1 -> {err0}} err3 -> {err2 -> {err1 -> {err0}}}
Example (Caller) ¶
package main import ( "fmt" "github.com/chai2010/errors" ) func main() { err := errors.New("error message") fmt.Println(err) for i, x := range err.(errors.Error).Caller() { fmt.Printf("caller:%d: %s\n", i, x.FuncName) } }
Output: error message caller:0: github.com/chai2010/errors_test.Example_caller caller:1: testing.runExample caller:2: testing.RunExamples caller:3: testing.(*M).Run caller:4: main.main
Example (Code) ¶
package main import ( "fmt" "github.com/chai2010/errors" ) func main() { err := errors.NewWithCode(404, "http error code") fmt.Println(err) fmt.Println(err.(errors.Error).Code()) }
Output: http error code 404
Example (Json) ¶
package main import ( "encoding/json" "fmt" "log" "reflect" "github.com/chai2010/errors" ) func main() { err0 := errors.New("err0") err1 := errors.Wrap(err0, "err1") err2 := func() error { return errors.Wrap(err1, "err2") }() err3 := func() error { return errors.Wrap(err2, "err3") }() err3JsonData, err := json.Marshal(err3) if err != nil { log.Fatal(err) } errx := errors.MustFromJson(string(err3JsonData)) if !reflect.DeepEqual(errx, err3) { log.Fatal("errors_test.Example_json:", errx, "!=", err3) } fmt.Println("done") }
Output: done
Example (Stringer) ¶
package main import ( "fmt" "github.com/chai2010/errors" ) func main() { err0 := errors.New("err0") err1 := errors.Wrap(err0, "err1") err2 := func() error { return errors.Wrap(err1, "err2") }() err3 := func() error { return errors.Wrap(err2, "err3") }() fmt.Println(err3) // fmt.Println(err3.Error()) fmt.Println(err3.(fmt.Stringer).String()) // print json string }
Output:
Example (Wraped) ¶
package main import ( "fmt" "runtime" "strings" "github.com/chai2010/errors" ) func main() { err0 := errors.New("err0") err1 := errors.Wrap(err0, "err1") err2 := func() error { return errors.Wrap(err1, "err2") }() err3 := func() error { return errors.Wrap(err2, "err3") }() fmt.Println(err3) for j, x := range err3.(errors.Error).Caller() { if j == 0 { // Closure path is different between Go1.4 and Go1.5 ver := runtime.Version() if strings.HasPrefix(ver, "go1.3") || strings.HasPrefix(ver, "go1.4") { if x.FuncName == "github.com/chai2010/errors_test.func" { x.FuncName = "github.com/chai2010/errors_test.Example_wraped.func" } } } fmt.Printf("caller:%d: %s\n", j, x.FuncName) } for i, err := range err3.(errors.Error).Wraped() { fmt.Printf("wraped:%d: %v\n", i, err) for j, x := range err.(errors.Error).Caller() { fmt.Printf(" caller:%d: %s\n", j, x.FuncName) } } }
Output: err3 -> {err2 -> {err1 -> {err0}}} caller:0: github.com/chai2010/errors_test.Example_wraped.func caller:1: github.com/chai2010/errors_test.Example_wraped caller:2: testing.runExample caller:3: testing.RunExamples caller:4: testing.(*M).Run caller:5: main.main wraped:0: err2 -> {err1 -> {err0}} caller:0: github.com/chai2010/errors_test.Example_wraped.func caller:1: github.com/chai2010/errors_test.Example_wraped caller:2: testing.runExample caller:3: testing.RunExamples caller:4: testing.(*M).Run caller:5: main.main wraped:1: err1 -> {err0} caller:0: github.com/chai2010/errors_test.Example_wraped caller:1: testing.runExample caller:2: testing.RunExamples caller:3: testing.(*M).Run caller:4: main.main wraped:2: err0 caller:0: github.com/chai2010/errors_test.Example_wraped caller:1: testing.runExample caller:2: testing.RunExamples caller:3: testing.(*M).Run caller:4: main.main
Index ¶
- func MustFromJson(json string) error
- func New(msg string) error
- func NewFrom(err error) error
- func NewWithCode(code int, msg string) error
- func NewWithCodef(code int, format string, args ...interface{}) error
- func Newf(format string, args ...interface{}) error
- func ToJson(err error) string
- func Wrap(err error, msg string) error
- func WrapWithCode(code int, err error, msg string) error
- func WrapWithCodef(code int, err error, format string, args ...interface{}) error
- func Wrapf(err error, format string, args ...interface{}) error
- type CallerInfo
- type Error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustFromJson ¶
func NewWithCode ¶
func NewWithCodef ¶
func WrapWithCodef ¶
Types ¶
type CallerInfo ¶
func Caller ¶
func Caller(skip int) []CallerInfo
Example (Skip0) ¶
package main import ( "fmt" "github.com/chai2010/errors" ) func main() { skip0Caller := errors.Caller(0) fmt.Println(skip0Caller[0].FuncName) fmt.Println(skip0Caller[1].FuncName) }
Output: github.com/chai2010/errors.Caller github.com/chai2010/errors_test.ExampleCaller_skip0
Example (Skip1) ¶
package main import ( "fmt" "github.com/chai2010/errors" ) func main() { skip1Caller := errors.Caller(1) fmt.Println(skip1Caller[0].FuncName) fmt.Println(skip1Caller[1].FuncName) }
Output: github.com/chai2010/errors_test.ExampleCaller_skip1 testing.runExample
Click to show internal directories.
Click to hide internal directories.