|
9 | 9 | "go.uber.org/multierr" |
10 | 10 |
|
11 | 11 | stderrors "errors" |
| 12 | + "log/slog" |
12 | 13 | ) |
13 | 14 |
|
14 | 15 | // what are these tests ? |
@@ -126,3 +127,41 @@ func TestErrorString(t *testing.T) { |
126 | 127 | x.Error(), |
127 | 128 | ) |
128 | 129 | } |
| 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 | +} |
0 commit comments