Skip to content

Commit 29a69c6

Browse files
author
Dean Karn
authored
Merge pull request #4 from go-playground/chain_wrap
New method: (Chain) Wrap(string)
2 parents 59a499b + 0b88ad7 commit 29a69c6

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

chain.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,8 @@ func (c Chain) AddTypes(typ ...string) Chain {
106106
l.Types = append(l.Types, typ...)
107107
return c
108108
}
109+
110+
// Wrap adds another contextual prefix to the error chain
111+
func (c Chain) Wrap(prefix string) Chain {
112+
return wrap(c, prefix)
113+
}

errors_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,31 @@ import (
88
)
99

1010
func TestWrap(t *testing.T) {
11+
defaultErr := fmt.Errorf("this is an %s", "error")
12+
err1 := Wrap(defaultErr, "prefix 1")
13+
err2 := err1.Wrap("prefix 2")
14+
1115
tests := []struct {
16+
err Chain
1217
pre string
1318
suf string
1419
}{
1520
{
16-
pre: "source=TestWrap: ",
17-
suf: "errors_test.go:24 prefix: this is an error",
21+
err: err1,
22+
pre: "TestWrap: ",
23+
suf: "errors_test.go:12",
24+
},
25+
{
26+
err: err2,
27+
pre: "TestWrap: ",
28+
suf: "errors_test.go:13",
1829
},
1930
}
2031

21-
defaultErr := fmt.Errorf("this is an %s", "error")
22-
2332
for i, tt := range tests {
24-
err := Wrap(defaultErr, "prefix")
25-
if !strings.HasSuffix(err.Error(), tt.suf) || !strings.HasPrefix(err.Error(), tt.pre) {
26-
t.Fatalf("IDX: %d want %s<path>%s got %s", i, tt.pre, tt.suf, err.Error())
33+
link := tt.err.current()
34+
if !strings.HasSuffix(link.Source, tt.suf) || !strings.HasPrefix(link.Source, tt.pre) {
35+
t.Fatalf("IDX: %d want %s<path>%s got %s", i, tt.pre, tt.suf, link.Source)
2736
}
2837
}
2938
}

0 commit comments

Comments
 (0)