Skip to content

Commit 025b350

Browse files
authored
Refactor: rename NewAppError (#10)
1 parent 9ee8b9f commit 025b350

8 files changed

+33
-33
lines changed

app_error.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ func newDefaultAppError(err error) *defaultAppError {
286286
}
287287
}
288288

289-
// NewAppError creates an AppError containing the given error
290-
func NewAppError(err error) AppError {
289+
// New creates an AppError containing the given error
290+
func New(err error) AppError {
291291
if err == nil {
292292
return nil
293293
}

app_error_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
func Test_NewAppError(t *testing.T) {
1212
initConfig(okConfig)
1313

14-
ae1 := NewAppError(nil)
15-
ae2 := NewAppError(errTest1)
16-
ae3 := NewAppError(ae2)
14+
ae1 := New(nil)
15+
ae2 := New(errTest1)
16+
ae3 := New(ae2)
1717
ae4 := fmt.Errorf("%w", ae3)
1818
ae5 := errors.Join(ae4, ae3, errTest2)
1919

@@ -35,7 +35,7 @@ func Test_AppError_Common(t *testing.T) {
3535
t.Run("success", func(t *testing.T) {
3636
initConfig(okConfig)
3737

38-
ae := NewAppError(errTest1).
38+
ae := New(errTest1).
3939
WithDebug("debug: %v", 123).
4040
WithParam("k1", "v1").
4141
WithParam("k2", "v2").
@@ -54,7 +54,7 @@ func Test_AppError_Common(t *testing.T) {
5454
t.Run("success: extending debug message", func(t *testing.T) {
5555
initConfig(okConfig)
5656

57-
ae := NewAppError(errTest1).
57+
ae := New(errTest1).
5858
WithDebug("debug: %v", 123).
5959
WithDebug("blah blah")
6060

@@ -66,7 +66,7 @@ func Test_AppError_Common(t *testing.T) {
6666
t.Run("success: non-debug mode", func(t *testing.T) {
6767
initConfig(nonDebugConfig)
6868

69-
ae := NewAppError(errTest1).
69+
ae := New(errTest1).
7070
WithDebug("debug: %v", 123).
7171
WithCause(errTest3)
7272

@@ -81,7 +81,7 @@ func Test_AppError_Build(t *testing.T) {
8181
t.Run("success", func(t *testing.T) {
8282
initConfig(okConfig)
8383

84-
ae := NewAppError(errTest1).
84+
ae := New(errTest1).
8585
WithDebug("debug: %v", 123).
8686
WithParam("k1", "v1").
8787
WithParam("k2", "v2").
@@ -112,13 +112,13 @@ func Test_AppError_Build(t *testing.T) {
112112
Code: "ErrCustom",
113113
})()
114114

115-
ae1 := NewAppError(errTest1).
115+
ae1 := New(errTest1).
116116
WithDebug("debug: %v", 123).
117117
WithParam("k1", "v1").
118118
WithParam("k2", "v2").
119119
WithTransParam("kk1", "vv1").
120120
WithCause(errTest2)
121-
ae2 := NewAppError(errTest2).
121+
ae2 := New(errTest2).
122122
WithParam("k1", "v1").
123123
WithParam("k2", "v2").
124124
WithTransParam("kk1", "vv1")
@@ -157,7 +157,7 @@ func Test_AppError_Build(t *testing.T) {
157157
TransKey: "Trans123",
158158
})()
159159

160-
ae := NewAppError(errTest1).
160+
ae := New(errTest1).
161161
WithDebug("debug: %v", 123).
162162
WithParam("k1", "v1").
163163
WithParam("k2", "v2").
@@ -197,7 +197,7 @@ func Test_AppError_Build(t *testing.T) {
197197
}
198198
}
199199

200-
ae := NewAppError(errTest1).
200+
ae := New(errTest1).
201201
WithDebug("debug: %v", 123).
202202
WithParam("k1", "v1").
203203
WithParam("k2", "v2").
@@ -240,7 +240,7 @@ func Test_AppError_Build(t *testing.T) {
240240
t.Run("success: fails to translate but no fallback to error string", func(t *testing.T) {
241241
initConfig(failedTransConfig)
242242

243-
ae := NewAppError(errTest1).
243+
ae := New(errTest1).
244244
WithDebug("debug: %v", 123).
245245
WithParam("k1", "v1").
246246
WithParam("k2", "v2").
@@ -262,7 +262,7 @@ func Test_AppError_Build(t *testing.T) {
262262
t.Run("success: fails to translate but fallback to error string", func(t *testing.T) {
263263
initConfig(failedTransConfig)
264264

265-
ae := NewAppError(errTest1).
265+
ae := New(errTest1).
266266
WithDebug("debug: %v", 123).
267267
WithParam("k1", "v1").
268268
WithParam("k2", "v2").
@@ -284,7 +284,7 @@ func Test_AppError_Build(t *testing.T) {
284284
t.Run("success: translation function unset", func(t *testing.T) {
285285
initConfig(notransConfig)
286286

287-
ae := NewAppError(errTest1).
287+
ae := New(errTest1).
288288
WithParam("k1", "v1").
289289
WithParam("k2", "v2").
290290
WithTransParam("kk1", "vv1")

app_errors_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func Test_AppErrorSlice(t *testing.T) {
1111
initConfig(okConfig)
1212

13-
ae1 := NewAppError(errTest1)
14-
ae2 := NewAppError(ae1)
13+
ae1 := New(errTest1)
14+
ae2 := New(ae1)
1515
aeSlice := AppErrors{ae1, ae2}
1616

1717
assert.ErrorIs(t, aeSlice, errTest1)

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ func Build(err error, lang Language, options ...InfoBuilderOption) *InfoBuilderR
8383
break
8484
}
8585
}
86-
return NewAppError(err).Build(lang, options...)
86+
return New(err).Build(lang, options...)
8787
}

main_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func Test_Build(t *testing.T) {
6060
t.Run("builds direct app errors", func(t *testing.T) {
6161
initConfig(okConfig)
6262

63-
ae1 := NewAppError(errTest1)
64-
ae2 := NewMultiError(NewAppError(errTest1), NewAppError(errTest2)).
63+
ae1 := New(errTest1)
64+
ae2 := NewMultiError(New(errTest1), New(errTest2)).
6565
WithCustomConfig(&ErrorConfig{
6666
Status: 1234,
6767
Code: "Err1234",
@@ -86,8 +86,8 @@ func Test_Build(t *testing.T) {
8686
t.Run("builds indirect app errors", func(t *testing.T) {
8787
initConfig(okConfig)
8888

89-
ae1 := NewAppError(errTest1)
90-
ae2 := NewMultiError(NewAppError(errTest1), NewAppError(errTest2)).
89+
ae1 := New(errTest1)
90+
ae2 := NewMultiError(New(errTest1), New(errTest2)).
9191
WithCustomConfig(&ErrorConfig{
9292
Status: 1234,
9393
Code: "Err1234",

multi_error_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ func Test_MultiError_Common(t *testing.T) {
1010
t.Run("success", func(t *testing.T) {
1111
initConfig(okConfig)
1212

13-
ae1 := NewAppError(errTest1)
14-
ae2 := NewAppError(errTest2)
13+
ae1 := New(errTest1)
14+
ae2 := New(errTest2)
1515
me1 := NewMultiError()
1616
me2 := AsMultiError(NewMultiError(ae1).
1717
WithParam("k1", "v1").
@@ -50,8 +50,8 @@ func Test_MultiError_Build(t *testing.T) {
5050
t.Run("success", func(t *testing.T) {
5151
initConfig(okConfig)
5252

53-
ae1 := NewAppError(errTest1)
54-
ae2 := NewAppError(errTest2)
53+
ae1 := New(errTest1)
54+
ae2 := New(errTest2)
5555
me1 := AsMultiError(NewMultiError(ae1))
5656
me2 := AsMultiError(NewMultiError(ae1, ae2).
5757
WithCustomConfig(&ErrorConfig{
@@ -87,8 +87,8 @@ func Test_MultiError_Build(t *testing.T) {
8787
Code: "Err1234",
8888
})()
8989

90-
ae1 := NewAppError(errTest1)
91-
ae2 := NewAppError(errTest2)
90+
ae1 := New(errTest1)
91+
ae2 := New(errTest2)
9292
me1 := AsMultiError(NewMultiError(ae1, ae2).
9393
WithCustomConfig(&ErrorConfig{
9494
Status: 1234,

validation_error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func NewValidationErrorWithInfoBuilder(infoBuilder InfoBuilderFunc, errs ...erro
2121
}
2222
appErrs := make(AppErrors, 0, len(errs))
2323
for _, e := range errs {
24-
appErrs = append(appErrs, NewAppError(e).WithCustomBuilder(infoBuilder))
24+
appErrs = append(appErrs, New(e).WithCustomBuilder(infoBuilder))
2525
}
2626
return NewValidationError(appErrs...)
2727
}

validation_error_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ func Test_ValidationError(t *testing.T) {
7878

7979
assert.Nil(t, NewValidationError())
8080
vldErr := NewValidationError(
81-
&testVldErr{defaultAppError: NewAppError(err3rdPartyVld1).(*defaultAppError)},
82-
&testVldErr{defaultAppError: NewAppError(err3rdPartyVld2).(*defaultAppError)},
83-
&testVldErr{defaultAppError: NewAppError(err3rdPartyVld3).(*defaultAppError)},
81+
&testVldErr{defaultAppError: New(err3rdPartyVld1).(*defaultAppError)},
82+
&testVldErr{defaultAppError: New(err3rdPartyVld2).(*defaultAppError)},
83+
&testVldErr{defaultAppError: New(err3rdPartyVld3).(*defaultAppError)},
8484
)
8585

8686
result := vldErr.Build(LanguageEn)

0 commit comments

Comments
 (0)