@@ -18,6 +18,12 @@ type testVldErr struct {
18
18
* defaultAppError
19
19
}
20
20
21
+ type test3rdPartyVldErr struct {
22
+ errStr string
23
+ }
24
+
25
+ func (e * test3rdPartyVldErr ) Error () string { return e .errStr }
26
+
21
27
func (e * testVldErr ) Build (lang Language , options ... InfoBuilderOption ) * InfoBuilderResult {
22
28
var message string
23
29
buildCfg := e .BuildConfig (lang , options ... )
@@ -38,22 +44,26 @@ func Test_ValidationError(t *testing.T) {
38
44
initConfig (okConfig )
39
45
40
46
infoBuilder := func (e AppError , buildCfg * InfoBuilderConfig ) * InfoBuilderResult {
41
- var message string
47
+ vldErr := & test3rdPartyVldErr {}
48
+ errors .As (e , & vldErr )
49
+ message := vldErr .errStr
42
50
if buildCfg .TranslationFunc != nil {
43
51
message , _ = buildCfg .TranslationFunc (buildCfg .Language , e .Error (), nil )
44
52
}
45
53
return & InfoBuilderResult {
46
54
ErrorInfo : & ErrorInfo {
47
55
Status : http .StatusBadRequest ,
48
- Code : "ErrValidationItem" ,
56
+ Code : vldErr . errStr ,
49
57
Message : message ,
50
58
},
51
59
}
52
60
}
53
61
54
62
assert .Nil (t , NewValidationErrorWithInfoBuilder (nil ))
55
63
vldErr := NewValidationErrorWithInfoBuilder (infoBuilder ,
56
- err3rdPartyVld1 , err3rdPartyVld2 , err3rdPartyVld3 )
64
+ & test3rdPartyVldErr {errStr : "3rdPartyVldErr1" },
65
+ & test3rdPartyVldErr {errStr : "3rdPartyVldErr2" },
66
+ & test3rdPartyVldErr {errStr : "3rdPartyVldErr3" })
57
67
58
68
result := vldErr .Build (LanguageEn )
59
69
errInfo := result .ErrorInfo
@@ -64,13 +74,18 @@ func Test_ValidationError(t *testing.T) {
64
74
65
75
inErr0 := errInfo .InnerErrors [0 ]
66
76
assert .Equal (t , http .StatusBadRequest , inErr0 .Status )
67
- assert .Equal (t , "ErrValidationItem " , inErr0 .Code )
68
- assert .Equal (t , "(ErrValidation1 )-in-en" , inErr0 .Message )
77
+ assert .Equal (t , "3rdPartyVldErr1 " , inErr0 .Code )
78
+ assert .Equal (t , "(3rdPartyVldErr1 )-in-en" , inErr0 .Message )
69
79
70
80
inErr1 := errInfo .InnerErrors [1 ]
71
81
assert .Equal (t , http .StatusBadRequest , inErr1 .Status )
72
- assert .Equal (t , "ErrValidationItem" , inErr1 .Code )
73
- assert .Equal (t , "(ErrValidation2)-in-en" , inErr1 .Message )
82
+ assert .Equal (t , "3rdPartyVldErr2" , inErr1 .Code )
83
+ assert .Equal (t , "(3rdPartyVldErr2)-in-en" , inErr1 .Message )
84
+
85
+ inErr2 := errInfo .InnerErrors [2 ]
86
+ assert .Equal (t , http .StatusBadRequest , inErr2 .Status )
87
+ assert .Equal (t , "3rdPartyVldErr3" , inErr2 .Code )
88
+ assert .Equal (t , "(3rdPartyVldErr3)-in-en" , inErr2 .Message )
74
89
})
75
90
76
91
t .Run ("success: implement AppError interface" , func (t * testing.T ) {
@@ -99,5 +114,10 @@ func Test_ValidationError(t *testing.T) {
99
114
assert .Equal (t , http .StatusBadRequest , inErr1 .Status )
100
115
assert .Equal (t , "ErrValidationItem" , inErr1 .Code )
101
116
assert .Equal (t , "(ErrValidation2)-in-en" , inErr1 .Message )
117
+
118
+ inErr2 := errInfo .InnerErrors [2 ]
119
+ assert .Equal (t , http .StatusBadRequest , inErr2 .Status )
120
+ assert .Equal (t , "ErrValidationItem" , inErr2 .Code )
121
+ assert .Equal (t , "(ErrValidation3)-in-en" , inErr2 .Message )
102
122
})
103
123
}
0 commit comments