error_ext_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2020 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package multierr_test
  21. import (
  22. "errors"
  23. "os"
  24. "testing"
  25. "github.com/stretchr/testify/assert"
  26. "github.com/stretchr/testify/require"
  27. "go.uber.org/multierr"
  28. )
  29. type errGreatSadness struct{ id int }
  30. func (errGreatSadness) Error() string {
  31. return "great sadness"
  32. }
  33. type errUnprecedentedFailure struct{ id int }
  34. func (errUnprecedentedFailure) Error() string {
  35. return "unprecedented failure"
  36. }
  37. func (e errUnprecedentedFailure) Unwrap() error {
  38. return errRootCause{e.id}
  39. }
  40. type errRootCause struct{ i int }
  41. func (errRootCause) Error() string {
  42. return "root cause"
  43. }
  44. func TestErrorsWrapping(t *testing.T) {
  45. err := multierr.Append(
  46. errGreatSadness{42},
  47. errUnprecedentedFailure{43},
  48. )
  49. t.Run("left", func(t *testing.T) {
  50. t.Run("As", func(t *testing.T) {
  51. var got errGreatSadness
  52. require.True(t, errors.As(err, &got))
  53. assert.Equal(t, 42, got.id)
  54. })
  55. t.Run("Is", func(t *testing.T) {
  56. assert.False(t, errors.Is(err, errGreatSadness{41}))
  57. assert.True(t, errors.Is(err, errGreatSadness{42}))
  58. })
  59. })
  60. t.Run("right", func(t *testing.T) {
  61. t.Run("As", func(t *testing.T) {
  62. var got errUnprecedentedFailure
  63. require.True(t, errors.As(err, &got))
  64. assert.Equal(t, 43, got.id)
  65. })
  66. t.Run("Is", func(t *testing.T) {
  67. assert.False(t, errors.Is(err, errUnprecedentedFailure{42}))
  68. assert.True(t, errors.Is(err, errUnprecedentedFailure{43}))
  69. })
  70. })
  71. t.Run("top-level", func(t *testing.T) {
  72. t.Run("As", func(t *testing.T) {
  73. var got interface{ Errors() []error }
  74. require.True(t, errors.As(err, &got))
  75. assert.Len(t, got.Errors(), 2)
  76. })
  77. t.Run("Is", func(t *testing.T) {
  78. assert.True(t, errors.Is(err, err))
  79. })
  80. })
  81. t.Run("root cause", func(t *testing.T) {
  82. t.Run("As", func(t *testing.T) {
  83. var got errRootCause
  84. require.True(t, errors.As(err, &got))
  85. assert.Equal(t, 43, got.i)
  86. })
  87. t.Run("Is", func(t *testing.T) {
  88. assert.False(t, errors.Is(err, errRootCause{42}))
  89. assert.True(t, errors.Is(err, errRootCause{43}))
  90. })
  91. })
  92. t.Run("mismatch", func(t *testing.T) {
  93. t.Run("As", func(t *testing.T) {
  94. var got *os.PathError
  95. assert.False(t, errors.As(err, &got))
  96. })
  97. t.Run("Is", func(t *testing.T) {
  98. assert.False(t, errors.Is(err, errors.New("great sadness")))
  99. })
  100. })
  101. }
  102. func TestErrorsWrappingSameType(t *testing.T) {
  103. err := multierr.Combine(
  104. errGreatSadness{1},
  105. errGreatSadness{2},
  106. errGreatSadness{3},
  107. )
  108. t.Run("As returns first", func(t *testing.T) {
  109. var got errGreatSadness
  110. require.True(t, errors.As(err, &got))
  111. assert.Equal(t, 1, got.id)
  112. })
  113. t.Run("Is matches all", func(t *testing.T) {
  114. assert.True(t, errors.Is(err, errGreatSadness{1}))
  115. assert.True(t, errors.Is(err, errGreatSadness{2}))
  116. assert.True(t, errors.Is(err, errGreatSadness{3}))
  117. })
  118. }