github.com/thanos-io/thanos@v0.32.5/pkg/errors/errors_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package errors 5 6 import ( 7 //lint:ignore faillint Custom errors package tests need to import standard library errors. 8 stderrors "errors" 9 "fmt" 10 "regexp" 11 "strconv" 12 "testing" 13 14 "github.com/efficientgo/core/testutil" 15 ) 16 17 const msg = "test_error_message" 18 const wrapper = "test_wrapper" 19 20 func TestNewf(t *testing.T) { 21 err := Newf(msg) 22 testutil.Equals(t, err.Error(), msg, "the root error message must match") 23 24 reg := regexp.MustCompile(msg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNewf .*\/pkg\/errors\/errors_test\.go:\d+`) 25 testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "matching stacktrace in errors.New") 26 } 27 28 func TestNewfFormatted(t *testing.T) { 29 fmtMsg := msg + " key=%v" 30 expectedMsg := msg + " key=value" 31 32 err := Newf(fmtMsg, "value") 33 testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") 34 reg := regexp.MustCompile(expectedMsg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNewfFormatted .*\/pkg\/errors\/errors_test\.go:\d+`) 35 testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "matching stacktrace in errors.New with format string") 36 } 37 38 func TestWrapf(t *testing.T) { 39 err := Newf(msg) 40 err = Wrapf(err, wrapper) 41 42 expectedMsg := wrapper + ": " + msg 43 testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match") 44 45 reg := regexp.MustCompile(`test_wrapper[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrapf .*\/pkg\/errors\/errors_test\.go:\d+ 46 [[:ascii:]]+test_error_message[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrapf .*\/pkg\/errors\/errors_test\.go:\d+`) 47 48 testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "matching stacktrace in errors.Wrap") 49 } 50 51 func TestUnwrap(t *testing.T) { 52 // test with base error 53 err := Newf(msg) 54 55 for i, tc := range []struct { 56 err error 57 expected string 58 isNil bool 59 }{ 60 { 61 // no wrapping 62 err: err, 63 isNil: true, 64 }, 65 { 66 err: Wrapf(err, wrapper), 67 expected: "test_error_message", 68 }, 69 { 70 err: Wrapf(Wrapf(err, wrapper), wrapper), 71 expected: "test_wrapper: test_error_message", 72 }, 73 // check primitives errors 74 { 75 err: stderrors.New("std-error"), 76 isNil: true, 77 }, 78 { 79 err: Wrapf(stderrors.New("std-error"), wrapper), 80 expected: "std-error", 81 }, 82 { 83 err: nil, 84 isNil: true, 85 }, 86 } { 87 t.Run("TestCase"+strconv.Itoa(i), func(t *testing.T) { 88 unwrapped := Unwrap(tc.err) 89 if tc.isNil { 90 testutil.Equals(t, unwrapped, nil) 91 return 92 } 93 testutil.Equals(t, unwrapped.Error(), tc.expected, "Unwrap must match expected output") 94 }) 95 } 96 } 97 98 func TestCause(t *testing.T) { 99 // test with base error that implements interface containing Unwrap method 100 err := Newf(msg) 101 102 for i, tc := range []struct { 103 err error 104 expected string 105 isNil bool 106 }{ 107 { 108 // no wrapping 109 err: err, 110 isNil: true, 111 }, 112 { 113 err: Wrapf(err, wrapper), 114 isNil: true, 115 }, 116 { 117 err: Wrapf(Wrapf(err, wrapper), wrapper), 118 isNil: true, 119 }, 120 // check primitives errors 121 { 122 err: stderrors.New("std-error"), 123 expected: "std-error", 124 }, 125 { 126 err: Wrapf(stderrors.New("std-error"), wrapper), 127 expected: "std-error", 128 }, 129 { 130 err: nil, 131 isNil: true, 132 }, 133 } { 134 t.Run("TestCase"+strconv.Itoa(i), func(t *testing.T) { 135 cause := Cause(tc.err) 136 if tc.isNil { 137 testutil.Equals(t, cause, nil) 138 return 139 } 140 testutil.Equals(t, cause.Error(), tc.expected, "Cause must match expected output") 141 }) 142 } 143 }