github.com/sandwich-go/boost@v1.3.29/xerror/xerror_x_test.go (about) 1 package xerror_test 2 3 import ( 4 ctx "context" 5 "errors" 6 "fmt" 7 "os" 8 "testing" 9 "time" 10 11 "github.com/sandwich-go/boost/xerror" 12 13 . "github.com/smartystreets/goconvey/convey" 14 ) 15 16 func TestTimeout(t *testing.T) { 17 Convey("timeout", t, func() { 18 { 19 err := xerror.Wrap(errors.New("1"), "wrap with xerror") 20 So(os.IsTimeout(err), ShouldBeFalse) 21 } 22 { 23 err := xerror.NewText("1").SetTimeout() 24 So(os.IsTimeout(err), ShouldBeTrue) 25 err.UnsetTimeout() 26 So(os.IsTimeout(err), ShouldBeFalse) 27 } 28 { 29 c, _ := ctx.WithTimeout(ctx.Background(), time.Millisecond) 30 time.Sleep(time.Millisecond * 2) 31 err := c.Err() 32 So(err, ShouldNotBeNil) 33 So(os.IsTimeout(err), ShouldBeTrue) 34 err2 := xerror.Wrap(err, "wrap with xerror") 35 So(os.IsTimeout(err2), ShouldBeTrue) 36 } 37 }) 38 } 39 40 func TestErrors(t *testing.T) { 41 Convey("xerror errors", t, func() { 42 { 43 err := xerror.Wrap(errors.New("1"), "wrap with xerror") 44 So(errors.Unwrap(err), ShouldNotBeNil) 45 } 46 { 47 err := xerror.NewText("1") 48 So(errors.Unwrap(err), ShouldBeNil) 49 } 50 { 51 errChild := xerror.NewText("1") 52 errParent := xerror.Wrap(errChild, "wrap with xerror") 53 So(errors.Is(errParent, errChild), ShouldBeTrue) 54 } 55 }) 56 } 57 58 func TestCause(t *testing.T) { 59 Convey("xerror case from system errors", t, func() { 60 { 61 err := errors.New("1") 62 So(xerror.Cause(err), ShouldEqual, err) 63 } 64 { 65 err1 := errors.New("1") 66 err := xerror.Wrap(err1, "2") 67 err = xerror.Wrap(err, "3") 68 So(xerror.Cause(err), ShouldEqual, err1) 69 } 70 }) 71 72 Convey("xerror case from xerror new", t, func() { 73 { 74 err := xerror.NewText("1") 75 So(xerror.Cause(err).Error(), ShouldEqual, err.Error()) 76 } 77 { 78 err1 := xerror.NewText("1") 79 err := xerror.Wrap(err1, "2") 80 err = xerror.Wrap(err, "3") 81 So(xerror.Cause(err).Error(), ShouldEqual, err1.Error()) 82 } 83 }) 84 } 85 86 func TestStack(t *testing.T) { 87 Convey("xerror stack", t, func() { 88 { 89 err := errors.New("1") 90 So(xerror.Stack(err), ShouldEqual, err.Error()) 91 } 92 { 93 old := xerror.IsErrorWithStack 94 xerror.IsErrorWithStack = true 95 defer func() { 96 xerror.IsErrorWithStack = old 97 }() 98 err := xerror.New(xerror.WithText("io error"), xerror.WithStack()) 99 errW := xerror.Wrap(err, "link error") 100 errW = xerror.Wrap(errW, "session error") 101 fmt.Printf("no stack===> %v\n", errW) 102 fmt.Printf("with stack===> %+s\n", errW) 103 104 fmt.Println(xerror.Caller(err, 0)) // xerror_x_test.go github.com/sandwich-go/boost/xerror/xerror_test.TestStack.func1 51 105 fmt.Println(xerror.Caller(err.Cause(), 0)) // xerror_x_test.go github.com/sandwich-go/boost/xerror/xerror_test.TestStack.func1 49 106 } 107 }) 108 } 109 110 func TestCode(t *testing.T) { 111 Convey("xerror code", t, func() { 112 { 113 err := errors.New("1") 114 So(xerror.Code(err), ShouldEqual, xerror.ErrorCodeUnsetAsDefault) 115 err = xerror.Wrap(err, "wrap 1") 116 So(xerror.Code(err), ShouldEqual, xerror.ErrorCodeUnsetAsDefault) 117 err = xerror.WrapCode(2, err, "wrap 2") 118 So(xerror.Code(err), ShouldEqual, 2) 119 So(xerror.Wrap(nil, "wrap nil"), ShouldEqual, nil) 120 121 So(xerror.Code(xerror.Wrap(nil, "wrap nil")), ShouldEqual, xerror.ErrorCodeOk) 122 } 123 }) 124 } 125 126 func TestUserCase(t *testing.T) { 127 { 128 err := xerror.NewText("error info is %s,error code will be %d", "crash", xerror.ErrorCodeUnsetAsDefault) 129 fmt.Println(err.Error()) 130 fmt.Println(xerror.Code(err)) 131 } 132 { 133 err := xerror.NewCode(10000, "error info") 134 fmt.Println(err.Error()) 135 fmt.Println(xerror.Code(err)) 136 } 137 { 138 err1 := errors.New("from some lib") 139 err2 := xerror.WrapCode(10000, err1, "wrap with error code") 140 fmt.Println(err2.Error()) 141 fmt.Println(xerror.Code(err1)) // 使用默认的 error code:xerror.ErrorCodeUnsetAsDefault 142 fmt.Println(xerror.Code(err2)) 143 } 144 } 145 146 func TestDyncOpenStackInfo(t *testing.T) { 147 old := xerror.IsErrorWithStack 148 xerror.IsErrorWithStack = false 149 defer func() { 150 xerror.IsErrorWithStack = old 151 }() 152 fmt.Printf("with stack===> %+s\n", xerror.NewText("open stack by function").WithStack()) 153 fmt.Printf("with stack===> %+s\n", xerror.New(xerror.WithText("open stack by option"), xerror.WithStack())) 154 } 155 156 func TestWrapNilError(t *testing.T) { 157 Convey("wrap nil error", t, func() { 158 { 159 nilErr := func() error { 160 return nil 161 } 162 err := xerror.Wrap(nilErr(), "should nil") 163 So(err, ShouldBeNil) 164 { 165 var errInterface error 166 errInterface = err 167 if errInterface == nil { 168 fmt.Printf("errInterface is nil, and error should be nil:%v\n", err) 169 } 170 } 171 { 172 errInterface := err 173 if errInterface == nil { 174 fmt.Printf("errInterface is nil, and error should be nil:%v\n", err) 175 } 176 } 177 testFiler := func(msg interface{}) { 178 switch msg.(type) { 179 case error: 180 fmt.Println("msg is a error, but is nil", msg) 181 default: 182 fmt.Println("msg is not error") 183 } 184 } 185 testFiler(err) 186 } 187 }) 188 }