| 1 |
package errors |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/google/go-cmp/cmp" |
| 10 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 11 |
) |
| 12 |
|
| 13 |
var err = fmt.Errorf("test error") |
| 14 |
|
| 15 |
func TestFormat(t *testing.T) { |
| 16 |
|
| 17 |
e1 := Newf("test") |
| 18 |
str := fmt.Sprintf("%+s", e1) |
| 19 |
if str != e1.m { |
| 20 |
t.Errorf("Error message invalid %s, expected %s", str, e1.m) |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
e2 := Annotatef(e1, "another") |
| 25 |
str = fmt.Sprintf("%+s", e2) |
| 26 |
val := e2.m + ": " + e2.c.Error() |
| 27 |
if str != val { |
| 28 |
t.Errorf("Error message invalid %s, expected %s", str, val) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
includeBacktrace.Store(true) |
| 33 |
e3 := Newf("test1") |
| 34 |
str = fmt.Sprintf("%+v", e3) |
| 35 |
if !strings.Contains(str, e3.m) { |
| 36 |
t.Errorf("Error message %s\n should contain %s", str, e3.m) |
| 37 |
} |
| 38 |
if !strings.Contains(str, fmt.Sprintf("%+v", e3.t.StackTrace())) { |
| 39 |
t.Errorf("Error message %s\n should contain %+v", str, e3.t.StackTrace()) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestMarshalJSON(t *testing.T) { |
| 44 |
includeBacktrace.Store(true) |
| 45 |
e := Newf("test") |
| 46 |
|
| 47 |
b, err := e.t.StackTrace().MarshalJSON() |
| 48 |
if err != nil { |
| 49 |
t.Errorf("MarshalJSON failed with error: %+s", err) |
| 50 |
} |
| 51 |
stack := make([]json.RawMessage, 0) |
| 52 |
err = json.Unmarshal(b, &stack) |
| 53 |
if err != nil { |
| 54 |
t.Errorf("JSON message could not be unmarshaled: %+s", err) |
| 55 |
} |
| 56 |
if len(stack) != len(e.t) { |
| 57 |
t.Errorf("Count of stack frames different after marshaling, expected %d got %d", len(e.t), len(stack)) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestAnnotatef(t *testing.T) { |
| 62 |
const testStr = "Annotatef string" |
| 63 |
te := Annotatef(err, testStr) |
| 64 |
if !cmp.Equal(te.c, err, cmpopts.EquateErrors()) { |
| 65 |
t.Errorf("Invalid parent error %s", cmp.Diff(te.c, err, cmpopts.EquateErrors())) |
| 66 |
} |
| 67 |
if te.m != testStr { |
| 68 |
t.Errorf("Invalid error message %s, expected %s", te.m, testStr) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
var homeVal = "$HOME" |
| 73 |
|
| 74 |
func TestNewf(t *testing.T) { |
| 75 |
const testStr = "Newf string" |
| 76 |
te := Newf(testStr) |
| 77 |
if te.c != nil { |
| 78 |
t.Errorf("Invalid parent error %T:%s, expected nil", te.c, te.c) |
| 79 |
} |
| 80 |
if te.m != testStr { |
| 81 |
t.Errorf("Invalid error message %s, expected %s", te.m, testStr) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestErrorf(t *testing.T) { |
| 86 |
const testStr = "Errorf string" |
| 87 |
err := Errorf(testStr) |
| 88 |
if te, ok := err.(*Err); ok { |
| 89 |
if te.c != nil { |
| 90 |
t.Errorf("Invalid parent error %T:%s, expected nil", te.c, te.c) |
| 91 |
} |
| 92 |
if te.m != testStr { |
| 93 |
t.Errorf("Invalid error message %s, expected %s", te.m, testStr) |
| 94 |
} |
| 95 |
} else { |
| 96 |
t.Errorf("Invalid error type returned %T, expected type %T", err, &Err{}) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
func TestErr_Error(t *testing.T) { |
| 178 |
type fields struct { |
| 179 |
m string |
| 180 |
c error |
| 181 |
t stack |
| 182 |
} |
| 183 |
tests := []struct { |
| 184 |
quiet bool |
| 185 |
name string |
| 186 |
fields fields |
| 187 |
want string |
| 188 |
}{ |
| 189 |
{ |
| 190 |
name: "empty", |
| 191 |
fields: fields{}, |
| 192 |
want: "", |
| 193 |
}, |
| 194 |
{ |
| 195 |
name: "just text", |
| 196 |
fields: fields{m: "test"}, |
| 197 |
want: "test", |
| 198 |
}, |
| 199 |
{ |
| 200 |
name: "text with single wrapped error", |
| 201 |
fields: fields{m: "test", c: fmt.Errorf("error")}, |
| 202 |
want: "test: error", |
| 203 |
}, |
| 204 |
{ |
| 205 |
name: "text with two wrapped errors", |
| 206 |
fields: fields{m: "test", c: fmt.Errorf("error: %w", Newf("some error"))}, |
| 207 |
want: "test: error: some error", |
| 208 |
}, |
| 209 |
{ |
| 210 |
name: "text with two wrapped errors, but no unwrapping", |
| 211 |
quiet: true, |
| 212 |
fields: fields{m: "test", c: fmt.Errorf("error: %w", Newf("some error"))}, |
| 213 |
want: "test", |
| 214 |
}, |
| 215 |
{ |
| 216 |
name: "text with two wrapped Err errors", |
| 217 |
fields: fields{m: "test", c: &Err{m: "error", c: &Err{m: "another error"}}}, |
| 218 |
want: "test: error: another error", |
| 219 |
}, |
| 220 |
{ |
| 221 |
name: "text with two wrapped Err errors, without unwrapping", |
| 222 |
quiet: true, |
| 223 |
fields: fields{m: "test", c: &Err{m: "error", c: &Err{m: "another error"}}}, |
| 224 |
want: "test", |
| 225 |
}, |
| 226 |
} |
| 227 |
for _, tt := range tests { |
| 228 |
t.Run(tt.name, func(t *testing.T) { |
| 229 |
includeBacktrace.Store(tt.quiet) |
| 230 |
e := Err{ |
| 231 |
m: tt.fields.m, |
| 232 |
c: tt.fields.c, |
| 233 |
t: tt.fields.t, |
| 234 |
} |
| 235 |
if got := e.Error(); got != tt.want { |
| 236 |
t.Errorf("Error() = %v, want %v", got, tt.want) |
| 237 |
} |
| 238 |
}) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
func areErrors(a, b any) bool { |
| 243 |
_, ok1 := a.(error) |
| 244 |
_, ok2 := b.(error) |
| 245 |
return ok1 && ok2 |
| 246 |
} |
| 247 |
|
| 248 |
func compareErrors(x, y interface{}) bool { |
| 249 |
xe := x.(error) |
| 250 |
ye := y.(error) |
| 251 |
if Is(xe, ye) || Is(ye, xe) { |
| 252 |
return true |
| 253 |
} |
| 254 |
return xe.Error() == ye.Error() |
| 255 |
} |
| 256 |
|
| 257 |
var EquateWeakErrors = cmp.FilterValues(areErrors, cmp.Comparer(compareErrors)) |