Skip to content

Commit 52a0c7c

Browse files
author
Dean Karn
authored
Merge pull request #3 from go-playground/mutation-implying-names
Renamed With* methods to Add* to imply that they mutate original object
2 parents 5a91b6f + 04b985d commit 52a0c7c

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func level1(value string) error {
6464

6565
func level2(value string) error {
6666
err := fmt.Errorf("this is an %s", "error")
67-
return errors.Wrap(err, "failed to do something").WithTypes("Permanent").WithTags(errors.T("value", value))
67+
return errors.Wrap(err, "failed to do something").AddTypes("Permanent").AddTags(errors.T("value", value))
6868
}
6969
```
7070

_examples/basic/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ func level1(value string) error {
3232

3333
func level2(value string) error {
3434
err := fmt.Errorf("this is an %s", "error")
35-
return errors.Wrap(err, "failed to do something").WithTypes("Permanent").WithTags(errors.T("value", value))
35+
return errors.Wrap(err, "failed to do something").AddTypes("Permanent").AddTags(errors.T("value", value))
3636
}

chain.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Link struct {
5050
// Prefix contains the error prefix text
5151
Prefix string
5252

53-
// Type stores one or more categorized types of error set by the caller using WithTypes and is optional
53+
// Type stores one or more categorized types of error set by the caller using AddTypes and is optional
5454
Types []string
5555

5656
// Tags contains an array of tags associated with this error, if any
@@ -85,8 +85,8 @@ func (c Chain) current() *Link {
8585
return c[len(c)-1]
8686
}
8787

88-
// WithTags allows the addition of multiple tags
89-
func (c Chain) WithTags(tags ...Tag) Chain {
88+
// AddTags allows the addition of multiple tags
89+
func (c Chain) AddTags(tags ...Tag) Chain {
9090
l := c.current()
9191
if len(l.Tags) == 0 {
9292
l.Tags = make([]Tag, 0, len(tags))
@@ -95,13 +95,13 @@ func (c Chain) WithTags(tags ...Tag) Chain {
9595
return c
9696
}
9797

98-
// WithTag allows the addition of a single tag
99-
func (c Chain) WithTag(key string, value interface{}) Chain {
100-
return c.WithTags(Tag{Key: key, Value: value})
98+
// AddTag allows the addition of a single tag
99+
func (c Chain) AddTag(key string, value interface{}) Chain {
100+
return c.AddTags(Tag{Key: key, Value: value})
101101
}
102102

103-
// WithTypes sets one or more categorized types on the Link error
104-
func (c Chain) WithTypes(typ ...string) Chain {
103+
// AddTypes sets one or more categorized types on the Link error
104+
func (c Chain) AddTypes(typ ...string) Chain {
105105
l := c.current()
106106
l.Types = append(l.Types, typ...)
107107
return c

errors_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestTags(t *testing.T) {
4242
defaultErr := fmt.Errorf("this is an %s", "error")
4343

4444
for i, tt := range tests {
45-
err := Wrap(defaultErr, "prefix").WithTags(tt.tags...)
45+
err := Wrap(defaultErr, "prefix").AddTags(tt.tags...)
4646
if !strings.HasSuffix(err.Error(), tt.err) {
4747
t.Fatalf("IDX: %d want %s got %s", i, tt.err, err.Error())
4848
}
@@ -65,7 +65,7 @@ func TestTypes(t *testing.T) {
6565
defaultErr := fmt.Errorf("this is an %s", "error")
6666

6767
for i, tt := range tests {
68-
err := Wrap(defaultErr, "prefix").WithTags(tt.tags...).WithTypes(tt.types...)
68+
err := Wrap(defaultErr, "prefix").AddTags(tt.tags...).AddTypes(tt.types...)
6969
if !strings.HasSuffix(err.Error(), tt.err) {
7070
t.Fatalf("IDX: %d want %s got %s", i, tt.err, err.Error())
7171
}
@@ -86,7 +86,7 @@ func TestHasType(t *testing.T) {
8686
defaultErr := fmt.Errorf("this is an %s", "error")
8787

8888
for i, tt := range tests {
89-
err := Wrap(defaultErr, "prefix").WithTypes(tt.types...)
89+
err := Wrap(defaultErr, "prefix").AddTypes(tt.types...)
9090
if !HasType(err, tt.typ) {
9191
t.Fatalf("IDX: %d want %t got %t", i, true, false)
9292
}
@@ -122,7 +122,7 @@ func TestCause2(t *testing.T) {
122122

123123
func TestHelpers(t *testing.T) {
124124
fn := func(w Chain, err error) (cont bool) {
125-
w.WithTypes("Test").WithTags(T("test", "tag")).WithTag("foo", "bar")
125+
w.AddTypes("Test").AddTags(T("test", "tag")).AddTag("foo", "bar")
126126
return false
127127
}
128128
RegisterHelper(fn)
@@ -134,7 +134,7 @@ func TestHelpers(t *testing.T) {
134134
}
135135

136136
func TestLookupTag(t *testing.T) {
137-
err := Wrap(io.EOF, "prefix").WithTag("Key", "Value")
137+
err := Wrap(io.EOF, "prefix").AddTag("Key", "Value")
138138
if LookupTag(err, "Key").(string) != "Value" {
139139
t.Fatalf("want %s got %v", "Value", LookupTag(err, "Key"))
140140
}

helpers/awserrors/awserrors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import (
99
func AWSErrors(c errors.Chain, err error) (cont bool) {
1010
switch e := err.(type) {
1111
case awserr.BatchError:
12-
c.WithTypes("Transient", "Batch").WithTags(errors.T("aws_error_code", e.Code()))
12+
c.AddTypes("Transient", "Batch").AddTags(errors.T("aws_error_code", e.Code()))
1313
return
1414

1515
case awserr.BatchedErrors:
16-
c.WithTypes("Transient", "Batch")
16+
c.AddTypes("Transient", "Batch")
1717
return
1818

1919
case awserr.RequestFailure:
20-
c.WithTypes("Transient", "Request").WithTags(
20+
c.AddTypes("Transient", "Request").AddTags(
2121
errors.T("status_code", e.StatusCode()),
2222
errors.T("request_id", e.RequestID()),
2323
)
2424
return
2525

2626
case awserr.Error:
27-
c.WithTypes("General", "Error").WithTags(errors.T("aws_error_code", e.Code()))
27+
c.AddTypes("General", "Error").AddTags(errors.T("aws_error_code", e.Code()))
2828
return
2929
}
3030
return true

helpers/ioerrors/ioerrors.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ import (
1010
func IOErrors(c errors.Chain, err error) (cont bool) {
1111
switch err {
1212
case io.EOF:
13-
c.WithTypes("io")
13+
c.AddTypes("io")
1414
return
1515
case io.ErrClosedPipe:
16-
c.WithTypes("Permanent", "io")
16+
c.AddTypes("Permanent", "io")
1717
return
1818
case io.ErrNoProgress:
19-
c.WithTypes("Permanent", "io")
19+
c.AddTypes("Permanent", "io")
2020
return
2121
case io.ErrShortBuffer:
22-
c.WithTypes("Permanent", "io")
22+
c.AddTypes("Permanent", "io")
2323
return
2424
case io.ErrShortWrite:
25-
c.WithTypes("Permanent", "io")
25+
c.AddTypes("Permanent", "io")
2626
return
2727
case io.ErrUnexpectedEOF:
28-
c.WithTypes("Transient", "io")
28+
c.AddTypes("Transient", "io")
2929
return
3030
}
3131
return true

helpers/neterrors/neterrors.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func NETErrors(c errors.Chain, err error) (cont bool) {
1414
if e.Temporary() {
1515
tp = "Transient"
1616
}
17-
c.WithTypes(tp, "net").WithTags(
17+
c.AddTypes(tp, "net").AddTags(
1818
errors.T("addr", e.Addr),
1919
errors.T("is_timeout", e.Timeout()),
2020
errors.T("is_temporary", e.Temporary()),
@@ -26,7 +26,7 @@ func NETErrors(c errors.Chain, err error) (cont bool) {
2626
if e.Temporary() {
2727
tp = "Transient"
2828
}
29-
c.WithTypes(tp, "net").WithTags(
29+
c.AddTypes(tp, "net").AddTags(
3030
errors.T("name", e.Name),
3131
errors.T("server", e.Server),
3232
errors.T("is_timeout", e.Timeout()),
@@ -35,7 +35,7 @@ func NETErrors(c errors.Chain, err error) (cont bool) {
3535
return false
3636

3737
case *net.ParseError:
38-
c.WithTypes("Permanent", "net").WithTags(
38+
c.AddTypes("Permanent", "net").AddTags(
3939
errors.T("type", e.Type),
4040
errors.T("text", e.Text),
4141
)
@@ -46,7 +46,7 @@ func NETErrors(c errors.Chain, err error) (cont bool) {
4646
if e.Temporary() {
4747
tp = "Transient"
4848
}
49-
c.WithTypes(tp, "net").WithTags(
49+
c.AddTypes(tp, "net").AddTags(
5050
errors.T("op", e.Op),
5151
errors.T("net", e.Net),
5252
errors.T("addr", e.Addr),
@@ -60,15 +60,15 @@ func NETErrors(c errors.Chain, err error) (cont bool) {
6060
if e.Temporary() {
6161
tp = "Transient"
6262
}
63-
c.WithTypes(tp, "net").WithTags(
63+
c.AddTypes(tp, "net").AddTags(
6464
errors.T("is_timeout", e.Timeout()),
6565
errors.T("is_temporary", e.Temporary()),
6666
)
6767
}
6868

6969
switch err {
7070
case net.ErrWriteToConnected:
71-
c.WithTypes("Transient", "net")
71+
c.AddTypes("Transient", "net")
7272
return false
7373
}
7474
return true

0 commit comments

Comments
 (0)