| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | // Initially copied from Thanos and contributed by https://github.com/bisakhmondal. |
| 5 | // |
| 6 | // Copyright (c) The Thanos Authors. |
| 7 | // Licensed under the Apache License 2.0. |
| 8 | |
| 9 | package errors_test |
| 10 | |
| 11 | import ( |
| 12 | //lint:ignore faillint Custom errors package tests need to import standard library errors. |
| 13 | stderrors "errors" |
| 14 | "fmt" |
| 15 | "regexp" |
| 16 | "testing" |
| 17 | |
| 18 | "github.com/efficientgo/core/errors" |
| 19 | "github.com/efficientgo/core/testutil" |
| 20 | ) |
| 21 | |
| 22 | func ExampleWrap() { |
| 23 | if err := func() error { |
| 24 | // Do something... |
| 25 | return errors.New("error!") |
| 26 | }(); err != nil { |
| 27 | wrapped := errors.Wrap(err, "doing something") |
| 28 | fmt.Println(wrapped) |
| 29 | } |
| 30 | |
| 31 | // Output: doing something: error! |
| 32 | } |
| 33 | |
| 34 | func ExampleWrapf() { |
| 35 | if err := func() error { |
| 36 | // Do something... |
| 37 | return errors.Newf("I am surprised %d + %d equals %v", 2, 3, 5) |
| 38 | }(); err != nil { |
| 39 | wrapped := errors.Wrapf(err, "doing some math %v times", 10) |
| 40 | fmt.Println(wrapped) |
| 41 | } |
| 42 | |
| 43 | // Output: doing some math 10 times: I am surprised 2 + 3 equals 5 |
| 44 | } |
| 45 | |
| 46 | const msg = "test_error_message" |
| 47 | const wrapper = "test_wrapper" |
| 48 | |
| 49 | func TestNew(t *testing.T) { |
| 50 | err := errors.New(msg) |
| 51 | testutil.NotOk(t, err) |
| 52 | testutil.Equals(t, err.Error(), msg, "the root error message must match") |
| 53 | |
| 54 | // The %+v triggers stacktrace print. |
| 55 | reg := regexp.MustCompile(msg + `[ \n]+> github\.com\/efficientgo\/core\/errors_test\.TestNew .*\/errors\/errors_test\.go:\d+`) |
| 56 | testutil.Assert(t, reg.MatchString(fmt.Sprintf("%+v", err)), "matching stacktrace in errors.New") |
| 57 | } |
| 58 | |
| 59 | func TestNewf(t *testing.T) { |
| 60 | fmtMsg := msg + " key=%v" |
| 61 | expectedMsg := msg + " key=value" |
| 62 | |
| 63 | err := errors.Newf(fmtMsg, "value") |
| 64 | testutil.NotOk(t, err) |
| 65 | testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") |
| 66 | |
| 67 | reg := regexp.MustCompile(expectedMsg + `[ \n]+> github\.com\/efficientgo\/core\/errors_test\.TestNewf .*\/errors\/errors_test\.go:\d+`) |
| 68 | testutil.Assert(t, reg.MatchString(fmt.Sprintf("%+v", err)), "matching stacktrace in errors.Newf") |
| 69 | } |
| 70 | |
| 71 | func TestWrap(t *testing.T) { |
| 72 | err := errors.New(msg) |
| 73 | err = errors.Wrap(err, wrapper) |
| 74 | testutil.NotOk(t, err) |
| 75 | |
| 76 | expectedMsg := wrapper + ": " + msg |
| 77 | testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") |
| 78 | |
| 79 | reg := regexp.MustCompile(`test_wrapper[ \n]+> github\.com\/efficientgo\/core\/errors_test\.TestWrap .*\/errors\/errors_test\.go:\d+ |
| 80 | [[:ascii:]]+test_error_message[ \n]+> github\.com\/efficientgo\/core\/errors_test\.TestWrap .*\/errors\/errors_test\.go:\d+`) |
| 81 | testutil.Assert(t, reg.MatchString(fmt.Sprintf("%+v", err)), "not matching stacktrace in errors.Wrap") |
| 82 | |
| 83 | testutil.Ok(t, errors.Wrap(nil, wrapper)) |
| 84 | } |
| 85 | |
| 86 | func TestWrapf(t *testing.T) { |
| 87 | err := errors.New(msg) |
| 88 | |
| 89 | fmtWrapper := wrapper + " key=%v" |
| 90 | err = errors.Wrapf(err, fmtWrapper, "value") |
| 91 | testutil.NotOk(t, err) |
| 92 | |
| 93 | expectedMsg := wrapper + " key=value: " + msg |
| 94 | testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") |
| 95 | |
| 96 | reg := regexp.MustCompile(`[ \n]+> github\.com\/efficientgo\/core\/errors_test\.TestWrapf .*\/errors\/errors_test\.go:\d+ |
| 97 | [[:ascii:]]+test_error_message[ \n]+> github\.com\/efficientgo\/core\/errors_test\.TestWrapf .*\/errors\/errors_test\.go:\d+`) |
| 98 | testutil.Assert(t, reg.MatchString(fmt.Sprintf("%+v", err)), "not matching stacktrace in errors.Wrapf") |
| 99 | |
| 100 | testutil.Ok(t, errors.Wrapf(nil, fmtWrapper)) |
| 101 | } |
| 102 | |
| 103 | func TestUnwrap(t *testing.T) { |
| 104 | baseErr := errors.Newf(msg) |
| 105 | |
| 106 | for _, tc := range []struct { |
| 107 | err error |
| 108 | expectedUnwrap string |
| 109 | }{ |
| 110 | { |
| 111 | err: baseErr, |
| 112 | }, |
| 113 | { |
| 114 | err: errors.Wrap(baseErr, wrapper), |
| 115 | expectedUnwrap: "test_error_message", |
| 116 | }, |
| 117 | { |
| 118 | err: errors.Wrap(errors.Wrap(baseErr, wrapper), wrapper), |
| 119 | expectedUnwrap: "test_wrapper: test_error_message", |
| 120 | }, |
| 121 | { |
| 122 | err: stderrors.New("std-error"), |
| 123 | }, |
| 124 | { |
| 125 | err: errors.Wrap(stderrors.New("std-error"), wrapper), |
| 126 | expectedUnwrap: "std-error", |
| 127 | }, |
| 128 | { |
| 129 | err: nil, |
| 130 | }, |
| 131 | } { |
| 132 | t.Run("", func(t *testing.T) { |
| 133 | unwrapped := errors.Unwrap(tc.err) |
| 134 | if tc.expectedUnwrap == "" { |
| 135 | testutil.Ok(t, unwrapped) |
| 136 | return |
| 137 | } |
| 138 | testutil.NotOk(t, unwrapped) |
| 139 | testutil.Equals(t, tc.expectedUnwrap, unwrapped.Error()) |
| 140 | }) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestCause(t *testing.T) { |
| 145 | baseErr := errors.Newf(msg) |
| 146 | |
| 147 | for _, tc := range []struct { |
| 148 | err error |
| 149 | expectedCause string |
| 150 | }{ |
| 151 | { |
| 152 | // no wrapping |
| 153 | err: baseErr, |
| 154 | }, |
| 155 | { |
| 156 | err: errors.Wrap(baseErr, wrapper), |
| 157 | }, |
| 158 | { |
| 159 | err: errors.Wrap(errors.Wrapf(baseErr, wrapper), wrapper), |
| 160 | }, |
| 161 | { |
| 162 | err: stderrors.New("std-error"), |
| 163 | expectedCause: "std-error", |
| 164 | }, |
| 165 | { |
| 166 | err: errors.Wrap(stderrors.New("std-error"), wrapper), |
| 167 | expectedCause: "std-error", |
| 168 | }, |
| 169 | { |
| 170 | err: nil, |
| 171 | }, |
| 172 | } { |
| 173 | t.Run("", func(t *testing.T) { |
| 174 | cause := errors.Cause(tc.err) |
| 175 | if tc.expectedCause == "" { |
| 176 | testutil.Ok(t, cause) |
| 177 | return |
| 178 | } |
| 179 | testutil.NotOk(t, cause) |
| 180 | testutil.Equals(t, tc.expectedCause, cause.Error()) |
| 181 | }) |
| 182 | } |
| 183 | } |