Skip to content

Commit b594e83

Browse files
authored
Merge pull request #669 from projectdiscovery/feat-657-set-attr
Adding withAttr
2 parents bc0b0d6 + 322e1e2 commit b594e83

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

errkit/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ with attributes support you can do following
4747
err := errkit.New("i/o timeout")
4848

4949
// xyz.go
50-
err = errkit.WithAttr(err,slog.Any("resource",domain))
50+
err = errkit.WithAttr(err, slog.String("resource", domain))
5151

5252
// abc.go
53-
err = errkit.WithAttr(err,slog.Any("action","download"))
53+
err = errkit.WithAttr(err, slog.String("action", "download"))
5454
```
5555

5656
## Note

errkit/errors.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func (e *ErrorX) ResetKind() *ErrorX {
311311
return e
312312
}
313313

314-
// Deprecated: use Attrs instead
314+
// Deprecated: use WithAttr instead
315315
//
316316
// SetAttr sets additional attributes to a given error
317317
// it only adds unique attributes and ignores duplicates
@@ -321,6 +321,9 @@ func (e *ErrorX) ResetKind() *ErrorX {
321321
//
322322
// this is correct (√)
323323
// myError.SetAttr(slog.String("address",host))
324+
//
325+
// Recommended replacement:
326+
// errkit.WithAttr(myError, slog.String("address", host))
324327
func (e *ErrorX) SetAttr(s ...slog.Attr) *ErrorX {
325328
e.init()
326329
for _, attr := range s {

errkit/errors_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"go.uber.org/multierr"
1010

1111
stderrors "errors"
12+
"log/slog"
1213
)
1314

1415
// what are these tests ?
@@ -126,3 +127,41 @@ func TestErrorString(t *testing.T) {
126127
x.Error(),
127128
)
128129
}
130+
131+
func TestWithAttr(t *testing.T) {
132+
// Test WithAttr function
133+
originalErr := New("connection failed")
134+
135+
// Add attributes using WithAttr
136+
err := WithAttr(originalErr, slog.String("resource", "database"), slog.Int("port", 5432))
137+
138+
// Verify the error can be unwrapped to ErrorX
139+
var errx *ErrorX
140+
require.True(t, errors.As(err, &errx), "expected to be able to unwrap to ErrorX")
141+
142+
// Check that attributes were added
143+
attrs := errx.Attrs()
144+
require.Len(t, attrs, 2, "expected 2 attributes")
145+
146+
// Verify specific attributes
147+
foundResource := false
148+
foundPort := false
149+
for _, attr := range attrs {
150+
if attr.Key == "resource" && attr.Value.String() == "database" {
151+
foundResource = true
152+
}
153+
if attr.Key == "port" && attr.Value.Int64() == 5432 {
154+
foundPort = true
155+
}
156+
}
157+
require.True(t, foundResource, "expected to find resource attribute")
158+
require.True(t, foundPort, "expected to find port attribute")
159+
160+
// Test that WithAttr works with nil error
161+
nilErr := WithAttr(nil, slog.String("test", "value"))
162+
require.Nil(t, nilErr, "expected nil error to return nil")
163+
164+
// Test that WithAttr works with empty attrs
165+
emptyErr := WithAttr(originalErr)
166+
require.Equal(t, originalErr, emptyErr, "expected original error when no attrs provided")
167+
}

errkit/helpers.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ func With(err error, args ...any) error {
210210
return x
211211
}
212212

213+
// WithAttr adds extra attributes to the error using slog.Attr
214+
//
215+
// err = errkit.WithAttr(err, slog.String("resource", domain))
216+
// err = errkit.WithAttr(err, slog.Int("port", 80), slog.String("protocol", "tcp"))
217+
func WithAttr(err error, attrs ...slog.Attr) error {
218+
if err == nil {
219+
return nil
220+
}
221+
if len(attrs) == 0 {
222+
return err
223+
}
224+
x := &ErrorX{}
225+
x.init()
226+
parseError(x, err)
227+
for _, attr := range attrs {
228+
x.record.Add(attr)
229+
}
230+
return x
231+
}
232+
213233
// GetAttr returns all attributes of given error if it has any
214234
func GetAttr(err error) []slog.Attr {
215235
if err == nil {

0 commit comments

Comments
 (0)