wrap_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package errors_test
  5. import (
  6. "errors"
  7. "fmt"
  8. "io/fs"
  9. "os"
  10. "reflect"
  11. "testing"
  12. )
  13. func TestIs(t *testing.T) {
  14. err1 := errors.New("1")
  15. erra := wrapped{"wrap 2", err1}
  16. errb := wrapped{"wrap 3", erra}
  17. err3 := errors.New("3")
  18. poser := &poser{"either 1 or 3", func(err error) bool {
  19. return err == err1 || err == err3
  20. }}
  21. testCases := []struct {
  22. err error
  23. target error
  24. match bool
  25. }{
  26. {nil, nil, true},
  27. {err1, nil, false},
  28. {err1, err1, true},
  29. {erra, err1, true},
  30. {errb, err1, true},
  31. {err1, err3, false},
  32. {erra, err3, false},
  33. {errb, err3, false},
  34. {poser, err1, true},
  35. {poser, err3, true},
  36. {poser, erra, false},
  37. {poser, errb, false},
  38. {errorUncomparable{}, errorUncomparable{}, true},
  39. {errorUncomparable{}, &errorUncomparable{}, false},
  40. {&errorUncomparable{}, errorUncomparable{}, true},
  41. {&errorUncomparable{}, &errorUncomparable{}, false},
  42. {errorUncomparable{}, err1, false},
  43. {&errorUncomparable{}, err1, false},
  44. }
  45. for _, tc := range testCases {
  46. t.Run("", func(t *testing.T) {
  47. if got := errors.Is(tc.err, tc.target); got != tc.match {
  48. t.Errorf("Is(%v, %v) = %v, want %v", tc.err, tc.target, got, tc.match)
  49. }
  50. })
  51. }
  52. }
  53. type poser struct {
  54. msg string
  55. f func(error) bool
  56. }
  57. var poserPathErr = &fs.PathError{Op: "poser"}
  58. func (p *poser) Error() string { return p.msg }
  59. func (p *poser) Is(err error) bool { return p.f(err) }
  60. func (p *poser) As(err any) bool {
  61. switch x := err.(type) {
  62. case **poser:
  63. *x = p
  64. case *errorT:
  65. *x = errorT{"poser"}
  66. case **fs.PathError:
  67. *x = poserPathErr
  68. default:
  69. return false
  70. }
  71. return true
  72. }
  73. func TestAs(t *testing.T) {
  74. var errT errorT
  75. var errP *fs.PathError
  76. var timeout interface{ Timeout() bool }
  77. var p *poser
  78. _, errF := os.Open("non-existing")
  79. poserErr := &poser{"oh no", nil}
  80. testCases := []struct {
  81. err error
  82. target any
  83. match bool
  84. want any // value of target on match
  85. }{{
  86. nil,
  87. &errP,
  88. false,
  89. nil,
  90. }, {
  91. wrapped{"pitied the fool", errorT{"T"}},
  92. &errT,
  93. true,
  94. errorT{"T"},
  95. }, {
  96. errF,
  97. &errP,
  98. true,
  99. errF,
  100. }, {
  101. errorT{},
  102. &errP,
  103. false,
  104. nil,
  105. }, {
  106. wrapped{"wrapped", nil},
  107. &errT,
  108. false,
  109. nil,
  110. }, {
  111. &poser{"error", nil},
  112. &errT,
  113. true,
  114. errorT{"poser"},
  115. }, {
  116. &poser{"path", nil},
  117. &errP,
  118. true,
  119. poserPathErr,
  120. }, {
  121. poserErr,
  122. &p,
  123. true,
  124. poserErr,
  125. }, {
  126. errors.New("err"),
  127. &timeout,
  128. false,
  129. nil,
  130. }, {
  131. errF,
  132. &timeout,
  133. true,
  134. errF,
  135. }, {
  136. wrapped{"path error", errF},
  137. &timeout,
  138. true,
  139. errF,
  140. }}
  141. for i, tc := range testCases {
  142. name := fmt.Sprintf("%d:As(Errorf(..., %v), %v)", i, tc.err, tc.target)
  143. // Clear the target pointer, in case it was set in a previous test.
  144. rtarget := reflect.ValueOf(tc.target)
  145. rtarget.Elem().Set(reflect.Zero(reflect.TypeOf(tc.target).Elem()))
  146. t.Run(name, func(t *testing.T) {
  147. match := errors.As(tc.err, tc.target)
  148. if match != tc.match {
  149. t.Fatalf("match: got %v; want %v", match, tc.match)
  150. }
  151. if !match {
  152. return
  153. }
  154. if got := rtarget.Elem().Interface(); got != tc.want {
  155. t.Fatalf("got %#v, want %#v", got, tc.want)
  156. }
  157. })
  158. }
  159. }
  160. func TestAsValidation(t *testing.T) {
  161. var s string
  162. testCases := []any{
  163. nil,
  164. (*int)(nil),
  165. "error",
  166. &s,
  167. }
  168. err := errors.New("error")
  169. for _, tc := range testCases {
  170. t.Run(fmt.Sprintf("%T(%v)", tc, tc), func(t *testing.T) {
  171. defer func() {
  172. recover()
  173. }()
  174. if errors.As(err, tc) {
  175. t.Errorf("As(err, %T(%v)) = true, want false", tc, tc)
  176. return
  177. }
  178. t.Errorf("As(err, %T(%v)) did not panic", tc, tc)
  179. })
  180. }
  181. }
  182. func TestUnwrap(t *testing.T) {
  183. err1 := errors.New("1")
  184. erra := wrapped{"wrap 2", err1}
  185. testCases := []struct {
  186. err error
  187. want error
  188. }{
  189. {nil, nil},
  190. {wrapped{"wrapped", nil}, nil},
  191. {err1, nil},
  192. {erra, err1},
  193. {wrapped{"wrap 3", erra}, erra},
  194. }
  195. for _, tc := range testCases {
  196. if got := errors.Unwrap(tc.err); got != tc.want {
  197. t.Errorf("Unwrap(%v) = %v, want %v", tc.err, got, tc.want)
  198. }
  199. }
  200. }
  201. type errorT struct{ s string }
  202. func (e errorT) Error() string { return fmt.Sprintf("errorT(%s)", e.s) }
  203. type wrapped struct {
  204. msg string
  205. err error
  206. }
  207. func (e wrapped) Error() string { return e.msg }
  208. func (e wrapped) Unwrap() error { return e.err }
  209. type errorUncomparable struct {
  210. f []string
  211. }
  212. func (errorUncomparable) Error() string {
  213. return "uncomparable error"
  214. }
  215. func (errorUncomparable) Is(target error) bool {
  216. _, ok := target.(errorUncomparable)
  217. return ok
  218. }
  219. func ExampleIs() {
  220. if _, err := os.Open("non-existing"); err != nil {
  221. if errors.Is(err, fs.ErrNotExist) {
  222. fmt.Println("file does not exist")
  223. } else {
  224. fmt.Println(err)
  225. }
  226. }
  227. // Output:
  228. // file does not exist
  229. }
  230. func ExampleAs() {
  231. if _, err := os.Open("non-existing"); err != nil {
  232. var pathError *fs.PathError
  233. if errors.As(err, &pathError) {
  234. fmt.Println("Failed at path:", pathError.Path)
  235. } else {
  236. fmt.Println(err)
  237. }
  238. }
  239. // Output:
  240. // Failed at path: non-existing
  241. }
  242. func ExampleUnwrap() {
  243. err1 := errors.New("error1")
  244. err2 := fmt.Errorf("error2: [%w]", err1)
  245. fmt.Println(err2)
  246. fmt.Println(errors.Unwrap(err2))
  247. // Output
  248. // error2: [error1]
  249. // error1
  250. }