Skip to content
This repository was archived by the owner on Jun 12, 2026. It is now read-only.

Commit 77b70a4

Browse files
lvan100lianghuan
authored andcommitted
refactor(error): check errors.New & fmt.Errorf
1 parent 17c1169 commit 77b70a4

6 files changed

Lines changed: 52 additions & 48 deletions

File tree

check.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ if ! command_exists modernize; then
6363
install_modernize
6464
fi
6565

66+
# Find fmt.Errorf and errors.New function calls, excluding vendor directory
67+
find . -type f -name '*.go' ! -path './vendor/*' -exec grep -Hn 'fmt\.Errorf' {} \;
68+
find . -type f -name '*.go' ! -path './vendor/*' -exec grep -Hn 'errors\.New' {} \;
69+
6670
print_separator
6771
echo "Step 1/3: Running go fix..."
6872
print_separator

ctxcache/ctxcache.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ package ctxcache
4646

4747
import (
4848
"context"
49-
"errors"
5049
"fmt"
5150
"sync"
51+
52+
"github.com/go-spring/stdlib/errutil"
5253
)
5354

5455
var (
55-
ErrCacheNotInitialized = errors.New("cache not initialized")
56-
ErrCacheAlreadyCleared = errors.New("cache already cleared")
57-
ErrKeyNotSet = errors.New("key not set")
58-
ErrKeyAlreadySet = errors.New("key already set")
56+
ErrCacheNotInitialized = errutil.Explain(nil, "cache not initialized")
57+
ErrCacheAlreadyCleared = errutil.Explain(nil, "cache already cleared")
58+
ErrKeyNotSet = errutil.Explain(nil, "key not set")
59+
ErrKeyAlreadySet = errutil.Explain(nil, "key already set")
5960
)
6061

6162
type cacheKeyType struct{}
@@ -150,19 +151,19 @@ func Get[T any](ctx context.Context, key string) (T, error) {
150151
k := TypedKey[T]{Key: key}
151152
cache, ok := getCache(ctx)
152153
if !ok {
153-
return zero, fmt.Errorf("%s: %w", k, ErrCacheNotInitialized)
154+
return zero, errutil.Explain(ErrCacheNotInitialized, "%s", k)
154155
}
155156

156157
cache.mutex.Lock()
157158
defer cache.mutex.Unlock()
158159

159160
if cache.cleared {
160-
return zero, fmt.Errorf("%s: %w", k, ErrCacheAlreadyCleared)
161+
return zero, errutil.Explain(ErrCacheAlreadyCleared, "%s", k)
161162
}
162163

163164
v, ok := cache.values[k]
164165
if !ok {
165-
return zero, fmt.Errorf("%s: %w", k, ErrKeyNotSet)
166+
return zero, errutil.Explain(ErrKeyNotSet, "%s", k)
166167
}
167168

168169
return v.(T), nil
@@ -180,18 +181,18 @@ func Set[T any](ctx context.Context, key string, value T) error {
180181
k := TypedKey[T]{Key: key}
181182
cache, ok := getCache(ctx)
182183
if !ok {
183-
return fmt.Errorf("%s: %w", k, ErrCacheNotInitialized)
184+
return errutil.Explain(ErrCacheNotInitialized, "%s", k)
184185
}
185186

186187
cache.mutex.Lock()
187188
defer cache.mutex.Unlock()
188189

189190
if cache.cleared {
190-
return fmt.Errorf("%s: %w", k, ErrCacheAlreadyCleared)
191+
return errutil.Explain(ErrCacheAlreadyCleared, "%s", k)
191192
}
192193

193194
if _, ok = cache.values[k]; ok {
194-
return fmt.Errorf("%s: %w", k, ErrKeyAlreadySet)
195+
return errutil.Explain(ErrKeyAlreadySet, "%s", k)
195196
}
196197

197198
cache.values[k] = value

goutil/goutil_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package goutil_test
1818

1919
import (
2020
"context"
21-
"errors"
2221
"testing"
2322
"time"
2423

24+
"github.com/go-spring/stdlib/errutil"
2525
"github.com/go-spring/stdlib/goutil"
2626
"github.com/go-spring/stdlib/testing/assert"
2727
)
@@ -125,7 +125,7 @@ func TestGoValue(t *testing.T) {
125125
})
126126

127127
t.Run("error return", func(t *testing.T) {
128-
expectedErr := errors.New("expected error")
128+
expectedErr := errutil.Explain(nil, "expected error")
129129
_, err := goutil.GoValue(t.Context(), func(ctx context.Context) (string, error) {
130130
return "", expectedErr
131131
}, goutil.InheritCancel).Wait()

testing/testcase/assert_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ package testcase_test
1818

1919
import (
2020
"bytes"
21-
"errors"
2221
"fmt"
2322
"io"
2423
"slices"
2524
"testing"
2625

26+
"github.com/go-spring/stdlib/errutil"
2727
"github.com/go-spring/stdlib/testing/assert"
2828
"github.com/go-spring/stdlib/testing/internal"
2929
"github.com/go-spring/stdlib/testing/require"
@@ -180,7 +180,7 @@ func TestPanic(t *testing.T) {
180180

181181
// Test panic with different types of values
182182
m.Reset()
183-
assert.Panic(m, func() { panic(errors.New("there's no error")) }, "an error")
183+
assert.Panic(m, func() { panic(errutil.Explain(nil, "there's no error")) }, "an error")
184184
assert.String(t, m.String()).Equal(`error# Assertion failed: got "there's no error" which does not match "an error"`)
185185

186186
m.Reset()
@@ -707,7 +707,7 @@ expected: fmt.Stringer`)
707707
// Test with interface implementation
708708
m.Reset()
709709
var err error
710-
assert.That(m, errors.New("test")).TypeOf(&err)
710+
assert.That(m, errutil.Explain(nil, "test")).TypeOf(&err)
711711
assert.String(t, m.String()).Equal("")
712712

713713
// Test with slice types
@@ -760,7 +760,7 @@ func TestThat_Implements(t *testing.T) {
760760

761761
// Test successful interface implementation
762762
m.Reset()
763-
assert.That(m, errors.New("error")).Implements((*error)(nil))
763+
assert.That(m, errutil.Explain(nil, "error")).Implements((*error)(nil))
764764
assert.String(t, m.String()).Equal("")
765765

766766
// Test non-interface target

testing/testcase/error_test.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
package testcase_test
1818

1919
import (
20-
"errors"
21-
"fmt"
2220
"testing"
2321

22+
"github.com/go-spring/stdlib/errutil"
2423
"github.com/go-spring/stdlib/testing/assert"
2524
"github.com/go-spring/stdlib/testing/internal"
2625
"github.com/go-spring/stdlib/testing/require"
@@ -44,20 +43,20 @@ func TestError_Nil(t *testing.T) {
4443

4544
// Test with non-nil error - should fail
4645
m.Reset()
47-
assert.Error(m, errors.New("this is an error")).Nil()
46+
assert.Error(m, errutil.Explain(nil, "this is an error")).Nil()
4847
assert.String(t, m.String()).Equal(`error# Assertion failed: expected error to be nil, but it is not
4948
actual: (*errors.errorString) "this is an error"`)
5049

5150
// Test with Require mode - should fatal
5251
m.Reset()
53-
require.Error(m, errors.New("this is an error")).Nil("index is 0")
52+
require.Error(m, errutil.Explain(nil, "this is an error")).Nil("index is 0")
5453
assert.String(t, m.String()).Equal(`fatal# Assertion failed: expected error to be nil, but it is not
5554
actual: (*errors.errorString) "this is an error"
5655
message: "index is 0"`)
5756

5857
// Test with custom message
5958
m.Reset()
60-
assert.Error(m, errors.New("test error")).Nil("expected no error in this operation")
59+
assert.Error(m, errutil.Explain(nil, "test error")).Nil("expected no error in this operation")
6160
assert.String(t, m.String()).Equal(`error# Assertion failed: expected error to be nil, but it is not
6261
actual: (*errors.errorString) "test error"
6362
message: "expected no error in this operation"`)
@@ -68,7 +67,7 @@ func TestError_NotNil(t *testing.T) {
6867

6968
// Test with non-nil error - should pass
7069
m.Reset()
71-
assert.Error(m, errors.New("this is an error")).NotNil()
70+
assert.Error(m, errutil.Explain(nil, "this is an error")).NotNil()
7271
assert.String(t, m.String()).Equal("")
7372

7473
// Test with nil error - should fail
@@ -91,7 +90,7 @@ func TestError_NotNil(t *testing.T) {
9190

9291
func TestError_Is(t *testing.T) {
9392
m := new(internal.MockTestingT)
94-
err := errors.New("this is an error")
93+
err := errutil.Explain(nil, "this is an error")
9594

9695
// Test successful case - error is the same as target
9796
m.Reset()
@@ -100,23 +99,23 @@ func TestError_Is(t *testing.T) {
10099

101100
// Test failed case - different errors
102101
m.Reset()
103-
assert.Error(m, err).Is(errors.New("another error"))
102+
assert.Error(m, err).Is(errutil.Explain(nil, "another error"))
104103
assert.String(t, m.String()).Equal(`error# Assertion failed: expected error to be target (according to errors.Is), but they are different
105104
actual: this is an error
106105
expected: another error`)
107106

108107
// Test failed case with Require - should fatal
109108
m.Reset()
110-
require.Error(m, err).Is(errors.New("another error"), "index is 0")
109+
require.Error(m, err).Is(errutil.Explain(nil, "another error"), "index is 0")
111110
assert.String(t, m.String()).Equal(`fatal# Assertion failed: expected error to be target (according to errors.Is), but they are different
112111
actual: this is an error
113112
expected: another error
114113
message: "index is 0"`)
115114

116115
// Test with wrapped error - should not match the root error (because we're checking Is in wrong direction)
117116
m.Reset()
118-
rootErr := errors.New("root error")
119-
wrappedErr := fmt.Errorf("level 1: %w", fmt.Errorf("level 2: %w", rootErr))
117+
rootErr := errutil.Explain(nil, "root error")
118+
wrappedErr := errutil.Explain(errutil.Explain(rootErr, "level 2"), "level 1")
120119
assert.Error(m, wrappedErr).Is(rootErr)
121120
assert.String(t, m.String()).Equal("")
122121

@@ -135,7 +134,7 @@ expected: this is an error`)
135134

136135
// Test with custom message on failure
137136
m.Reset()
138-
assert.Error(m, errors.New("some error")).Is(errors.New("other error"), "expected errors to match")
137+
assert.Error(m, errutil.Explain(nil, "some error")).Is(errutil.Explain(nil, "other error"), "expected errors to match")
139138
assert.String(t, m.String()).Equal(`error# Assertion failed: expected error to be target (according to errors.Is), but they are different
140139
actual: some error
141140
expected: other error
@@ -144,11 +143,11 @@ expected: other error
144143

145144
func TestError_NotIs(t *testing.T) {
146145
m := new(internal.MockTestingT)
147-
err := errors.New("this is an error")
146+
err := errutil.Explain(nil, "this is an error")
148147

149148
// Test successful case - different errors
150149
m.Reset()
151-
assert.Error(m, err).NotIs(errors.New("another error"))
150+
assert.Error(m, err).NotIs(errutil.Explain(nil, "another error"))
152151
assert.String(t, m.String()).Equal("")
153152

154153
// Test failed case - same errors
@@ -168,8 +167,8 @@ expected: this is an error
168167

169168
// Test with wrapped error - wrapped error contains root error, so NotIs should fail
170169
m.Reset()
171-
rootErr := errors.New("root error")
172-
wrappedErr := fmt.Errorf("level 1: %w", fmt.Errorf("level 2: %w", rootErr))
170+
rootErr := errutil.Explain(nil, "root error")
171+
wrappedErr := errutil.Explain(errutil.Explain(rootErr, "level 2"), "level 1")
173172
assert.Error(m, rootErr).NotIs(wrappedErr)
174173
assert.String(t, m.String()).Equal("")
175174

@@ -195,7 +194,7 @@ expected: this is an error
195194

196195
func TestError_String(t *testing.T) {
197196
m := new(internal.MockTestingT)
198-
err := errors.New("this is an error")
197+
err := errutil.Explain(nil, "this is an error")
199198

200199
// Test successful case - error is the same as target
201200
m.Reset()
@@ -219,8 +218,8 @@ expected: "another error"
219218

220219
// Test with wrapped error - should not match the root error (because we're checking Is in wrong direction)
221220
m.Reset()
222-
rootErr := errors.New("root error")
223-
wrappedErr := fmt.Errorf("level 1: %w", fmt.Errorf("level 2: %w", rootErr))
221+
rootErr := errutil.Explain(nil, "root error")
222+
wrappedErr := errutil.Explain(errutil.Explain(rootErr, "level 2"), "level 1")
224223
assert.Error(m, wrappedErr).String("level 1: level 2: root error")
225224
assert.String(t, m.String()).Equal("")
226225

@@ -237,7 +236,7 @@ expected: "another error"
237236

238237
// Test with custom message on failure
239238
m.Reset()
240-
assert.Error(m, errors.New("some error")).String("other error", "expected errors to match")
239+
assert.Error(m, errutil.Explain(nil, "some error")).String("other error", "expected errors to match")
241240
assert.String(t, m.String()).Equal(`error# Assertion failed: expected strings to be equal, but they are not
242241
actual: "some error"
243242
expected: "other error"
@@ -249,12 +248,12 @@ func TestError_Matches(t *testing.T) {
249248

250249
// Test successful case - simple string match
251250
m.Reset()
252-
assert.Error(m, errors.New("this is an error")).Matches("an error")
251+
assert.Error(m, errutil.Explain(nil, "this is an error")).Matches("an error")
253252
assert.String(t, m.String()).Equal("")
254253

255254
// Test invalid regex pattern
256255
m.Reset()
257-
assert.Error(m, errors.New("there's no error")).Matches(`an error \`)
256+
assert.Error(m, errutil.Explain(nil, "there's no error")).Matches(`an error \`)
258257
assert.String(t, m.String()).Equal("error# Assertion failed: invalid pattern")
259258

260259
// Test with nil error - should fail
@@ -270,28 +269,28 @@ func TestError_Matches(t *testing.T) {
270269

271270
// Test failed match with Require - should fatal
272271
m.Reset()
273-
require.Error(m, errors.New("there's no error")).Matches("an error")
272+
require.Error(m, errutil.Explain(nil, "there's no error")).Matches("an error")
274273
assert.String(t, m.String()).Equal(`fatal# Assertion failed: got "there's no error" which does not match "an error"`)
275274

276275
// Test failed match with Require and custom message
277276
m.Reset()
278-
require.Error(m, errors.New("there's no error")).Matches("an error", "index is 0")
277+
require.Error(m, errutil.Explain(nil, "there's no error")).Matches("an error", "index is 0")
279278
assert.String(t, m.String()).Equal(`fatal# Assertion failed: got "there's no error" which does not match "an error"
280279
message: "index is 0"`)
281280

282281
// Test with regex pattern that matches
283282
m.Reset()
284-
assert.Error(m, errors.New("error code 123")).Matches(`error code \d+`)
283+
assert.Error(m, errutil.Explain(nil, "error code 123")).Matches(`error code \d+`)
285284
assert.String(t, m.String()).Equal("")
286285

287286
// Test with regex pattern that does not match
288287
m.Reset()
289-
assert.Error(m, errors.New("error code abc")).Matches(`error code \d+`)
288+
assert.Error(m, errutil.Explain(nil, "error code abc")).Matches(`error code \d+`)
290289
assert.String(t, m.String()).Equal(`error# Assertion failed: got "error code abc" which does not match "error code \\d+"`)
291290

292291
// Test with complex error message
293292
m.Reset()
294-
assert.Error(m, fmt.Errorf("database connection failed: %w", errors.New("timeout"))).Matches("connection failed")
293+
assert.Error(m, errutil.Explain(errutil.Explain(nil, "timeout"), "database connection failed")).Matches("connection failed")
295294
assert.String(t, m.String()).Equal("")
296295

297296
// Test with custom error type
@@ -301,7 +300,7 @@ func TestError_Matches(t *testing.T) {
301300

302301
// Test with custom message on failure
303302
m.Reset()
304-
assert.Error(m, errors.New("some error")).Matches("nonexistent", "expected error to match pattern")
303+
assert.Error(m, errutil.Explain(nil, "some error")).Matches("nonexistent", "expected error to match pattern")
305304
assert.String(t, m.String()).Equal(`error# Assertion failed: got "some error" which does not match "nonexistent"
306305
message: "expected error to match pattern"`)
307306
}

typeutil/typeutil_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
package typeutil_test
1818

1919
import (
20-
"errors"
2120
"fmt"
2221
"os"
2322
"reflect"
2423
"testing"
2524
"unsafe"
2625

26+
"github.com/go-spring/stdlib/errutil"
2727
"github.com/go-spring/stdlib/testing/assert"
2828
"github.com/go-spring/stdlib/typeutil"
2929
)
3030

3131
func TestIsErrorType(t *testing.T) {
32-
err := errors.New("error")
32+
err := errutil.Explain(nil, "error")
3333
assert.That(t, typeutil.IsErrorType(reflect.TypeOf(err))).True()
3434

3535
err = os.ErrClosed
@@ -191,7 +191,7 @@ func TestIsBeanType(t *testing.T) {
191191
func TestIsBeanInjectionTarget(t *testing.T) {
192192
assert.That(t, typeutil.IsBeanInjectionTarget(reflect.TypeFor[string]())).False()
193193
assert.That(t, typeutil.IsBeanInjectionTarget(reflect.TypeFor[*string]())).False()
194-
assert.That(t, typeutil.IsBeanInjectionTarget(reflect.TypeOf(errors.New("abc")))).True()
194+
assert.That(t, typeutil.IsBeanInjectionTarget(reflect.TypeOf(errutil.Explain(nil, "abc")))).True()
195195
assert.That(t, typeutil.IsBeanInjectionTarget(reflect.TypeFor[[]string]())).False()
196196
assert.That(t, typeutil.IsBeanInjectionTarget(reflect.TypeFor[[]*string]())).False()
197197
assert.That(t, typeutil.IsBeanInjectionTarget(reflect.TypeFor[[]fmt.Stringer]())).True()

0 commit comments

Comments
 (0)