func Example_stringer() { 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 }
func Example_wraped() { 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 }
func Example() { 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}}} }
func Example_wraped() { 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() { 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.func2 // 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.func1 // 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 }
func Example_json() { 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 }