gopkg.in/hedzr/errors.v3@v3.3.1/asisunwrap_test.go (about) 1 package errors 2 3 import ( 4 // errors2 "errors" 5 "io" 6 "strconv" 7 "testing" 8 ) 9 10 func TestErrorCodeIs(t *testing.T) { 11 var err error = BadRequest 12 if !Is(err, BadRequest) { 13 t.Fatalf("want is") 14 } 15 16 err = io.ErrClosedPipe 17 if Is(err, BadRequest) { 18 t.Fatalf("want not is") 19 } 20 21 err = NotFound 22 if Is(err, BadRequest) { 23 t.Fatalf("want not is (code)") 24 } 25 26 // 27 28 _, err = strconv.ParseInt("hello", 10, 64) 29 if !Is(err, strconv.ErrRange) { 30 t.Logf("'%v' recoganized OK.", err) 31 } else { 32 t.Fatalf("'%+v' CANNOT be recoganized", err) 33 } 34 35 // for v1.11, Is(err, strconv.ErrSyntax) should be false 36 // and for v1.12+, Is(err, strconv.ErrSyntax) must be true 37 } 38 39 // TestErrorsIs _ 40 func TestErrorsIs(t *testing.T) { 41 _, err := strconv.ParseFloat("hello", 64) 42 t.Logf("err = %+v", err) 43 44 // e1 := errors2.Unwrap(err) 45 // t.Logf("e1 = %+v", e1) 46 47 t.Logf("errors.Is(err, strconv.ErrSyntax): %v", Is(err, strconv.ErrSyntax)) 48 t.Logf("errors.Is(err, &strconv.NumError{}): %v", Is(err, &strconv.NumError{Err: strconv.ErrRange})) 49 50 var e2 *strconv.NumError 51 if As(err, &e2) { 52 t.Logf("As() ok, e2 = %v", e2) 53 } else { 54 t.Logf("As() not ok") 55 } 56 } 57 58 func TestIs(t *testing.T) { 59 err := &causes2{ 60 Code: 0, 61 Causers: nil, 62 msg: "ui err", 63 } 64 _ = err.WithErrors(io.EOF, io.ErrClosedPipe) 65 66 for _, e := range []error{io.EOF, io.ErrClosedPipe} { 67 if !Is(err, e) { 68 t.Fatalf("test for Is(%v) failed", e) 69 } 70 } 71 72 t.Logf("the error is: %+v", err) 73 } 74 75 func TestIs2(t *testing.T) { //nolint:revive 76 series := []error{io.EOF, io.ErrShortWrite, io.ErrClosedPipe, Internal} 77 78 err2 := New("hello %v", "world") 79 80 // the old err2 (i.e. err3) will be moved into err2's slice 81 // container, and more errors (io.EOF, io.ErrShortWrite, and 82 // io.ErrClosedPipe) will be appended into the slice 83 // container. 84 err2.WithErrors(io.EOF, io.ErrShortWrite). 85 WithErrors(io.ErrClosedPipe). 86 WithCode(Internal). 87 End() 88 89 for _, e := range series { 90 if !Is(err2, e) { 91 t.Fatalf("test for Is(%v) failed", e) 92 } 93 } 94 95 var code Code 96 if !(As(err2, &code) && code == Internal) { //nolint:revive 97 t.Fatalf("cannot extract coded error with As()") 98 } 99 100 // so As() will extract the first element in err2's slice container, 101 // that is err3. 102 var ee1 error 103 if !(As(err2, &ee1) && ee1 == io.EOF) { //nolint:revive 104 t.Fatalf("cannot extract 'hello world' error with As(), ee1 = %v", ee1) 105 } 106 107 var ee2 []error 108 if !(As(err2, &ee2) && len(ee2) == 3) { 109 t.Fatalf("cannot extract []error error with As(), ee2 = %v (len=%v)", ee2, len(ee2)) 110 } 111 112 var index int 113 for ; ee1 != nil; index++ { 114 ee1 = Unwrap(err2) 115 if ee1 != nil && ee1 != series[index] { 116 t.Fatalf("%d. cannot extract '%v' error with As(), ee1 = %v", index, series[index], ee1) 117 } 118 } 119 } 120 121 func TestWrap(t *testing.T) { 122 err := Wrap(nil, "hello, %v", "world") 123 t.Logf("failed: %+v", err) 124 125 err = Wrap(Internal, "hello, %v", "world") 126 t.Logf("failed: %+v", err) 127 128 err = Wrap(Internal, "hello, %v", "world") 129 t.Logf("failed: %+v", err) 130 131 _ = Unwrap(io.EOF) 132 } 133 134 func TestTypeIsSlice(t *testing.T) { 135 TypeIsSlice(nil, nil) 136 TypeIsSlice(nil, io.EOF) 137 138 TypeIsSlice([]error{io.EOF}, io.EOF) 139 140 err := Wrap(Internal, "hello, %v", "world") 141 err.WithErrors(NotFound).End() 142 143 if !TypeIsSlice(err.Causes(), NotFound) { 144 t.Fatalf("not ok") 145 } 146 147 if !TypeIs(err, NotFound) { 148 t.Fatalf("not ok") 149 } 150 151 err2 := New().WithErrors(NotFound, err) 152 err3 := New().WithErrors(NotFound, err2) 153 if !TypeIsSlice(Causes(err3), err) { 154 t.Fatalf("not ok") 155 } 156 157 if !TypeIs(err2, NotFound) { 158 t.Fatalf("not ok") 159 } 160 if !TypeIs(err3, NotFound) { 161 t.Fatalf("not ok") 162 } 163 if !TypeIs(err3, err2) { 164 t.Fatalf("not ok") 165 } 166 if !TypeIs(err3, err) { 167 t.Fatalf("not ok") 168 } 169 TypeIs(err3, nil) 170 171 IsSlice(Causes(err3), nil) 172 IsSlice(Causes(err3), io.ErrShortBuffer) 173 IsSlice(Causes(err3), io.EOF) 174 IsSlice(Causes(err3), NotFound) 175 IsSlice(Causes(err3), Internal) 176 IsSlice(Causes(err3), err2) 177 IsSlice(Causes(err3), err) 178 179 Is(err3, nil) 180 Is(err3, io.ErrShortBuffer) 181 Is(err3, io.EOF) 182 Is(err3, NotFound) 183 Is(err3, Internal) 184 Is(err3, err2) 185 Is(err3, err) 186 } 187 188 func TestAsRaisePanic(t *testing.T) { 189 t.Run("1", func(t *testing.T) { 190 defer func() { recover() }() //nolint:errcheck //no need 191 As(nil, nil) 192 }) 193 194 t.Run("2", func(t *testing.T) { 195 defer func() { recover() }() //nolint:errcheck //no need 196 var v int 197 As(nil, &v) 198 }) 199 200 t.Run("3", func(t *testing.T) { 201 defer func() { recover() }() //nolint:errcheck //no need 202 var err error 203 As(nil, &err) 204 }) 205 206 t.Run("4", func(t *testing.T) { 207 defer func() { recover() }() //nolint:errcheck //no need 208 var err int 209 As(nil, err) 210 }) 211 } 212 213 func TestAsSliceRaisePanic(t *testing.T) { 214 t.Run("1", func(t *testing.T) { 215 defer func() { recover() }() //nolint:errcheck //no need 216 AsSlice(nil, nil) 217 }) 218 219 t.Run("2", func(t *testing.T) { 220 defer func() { recover() }() //nolint:errcheck //no need 221 var v int 222 AsSlice(nil, &v) 223 }) 224 225 t.Run("3", func(t *testing.T) { 226 defer func() { recover() }() //nolint:errcheck //no need 227 var err error 228 AsSlice(nil, &err) 229 }) 230 231 t.Run("4", func(t *testing.T) { 232 defer func() { recover() }() //nolint:errcheck //no need 233 var err int 234 AsSlice(nil, err) 235 }) 236 }