-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_error_test.go
123 lines (101 loc) · 3.68 KB
/
validation_error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package goapperrors
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
var (
err3rdPartyVld1 = errors.New("ErrValidation1")
err3rdPartyVld2 = errors.New("ErrValidation2")
err3rdPartyVld3 = errors.New("ErrValidation3")
)
type testVldErr struct {
*defaultAppError
}
type test3rdPartyVldErr struct {
errStr string
}
func (e *test3rdPartyVldErr) Error() string { return e.errStr }
func (e *testVldErr) Build(lang Language, options ...InfoBuilderOption) *InfoBuilderResult {
var message string
buildCfg := e.BuildConfig(lang, options...)
if buildCfg.TranslationFunc != nil {
message, _ = buildCfg.TranslationFunc(lang, e.Error(), nil)
}
return &InfoBuilderResult{
ErrorInfo: &ErrorInfo{
Status: http.StatusBadRequest,
Code: "ErrValidationItem",
Message: message,
},
}
}
func Test_ValidationError(t *testing.T) {
t.Run("success: provides custom builder", func(t *testing.T) {
initConfig(okConfig)
infoBuilder := func(e AppError, buildCfg *InfoBuilderConfig) *InfoBuilderResult {
vldErr := &test3rdPartyVldErr{}
errors.As(e, &vldErr)
message := vldErr.errStr
if buildCfg.TranslationFunc != nil {
message, _ = buildCfg.TranslationFunc(buildCfg.Language, e.Error(), nil)
}
return &InfoBuilderResult{
ErrorInfo: &ErrorInfo{
Status: http.StatusBadRequest,
Code: vldErr.errStr,
Message: message,
},
}
}
assert.Nil(t, NewValidationErrorWithInfoBuilder(nil))
vldErr := NewValidationErrorWithInfoBuilder(infoBuilder,
&test3rdPartyVldErr{errStr: "3rdPartyVldErr1"},
&test3rdPartyVldErr{errStr: "3rdPartyVldErr2"},
&test3rdPartyVldErr{errStr: "3rdPartyVldErr3"})
result := vldErr.Build(LanguageEn)
errInfo := result.ErrorInfo
assert.Equal(t, 0, len(result.TransMissingKeys))
assert.Equal(t, "(ErrValidation)-in-en", errInfo.Message)
assert.Equal(t, 3, len(errInfo.InnerErrors))
inErr0 := errInfo.InnerErrors[0]
assert.Equal(t, http.StatusBadRequest, inErr0.Status)
assert.Equal(t, "3rdPartyVldErr1", inErr0.Code)
assert.Equal(t, "(3rdPartyVldErr1)-in-en", inErr0.Message)
inErr1 := errInfo.InnerErrors[1]
assert.Equal(t, http.StatusBadRequest, inErr1.Status)
assert.Equal(t, "3rdPartyVldErr2", inErr1.Code)
assert.Equal(t, "(3rdPartyVldErr2)-in-en", inErr1.Message)
inErr2 := errInfo.InnerErrors[2]
assert.Equal(t, http.StatusBadRequest, inErr2.Status)
assert.Equal(t, "3rdPartyVldErr3", inErr2.Code)
assert.Equal(t, "(3rdPartyVldErr3)-in-en", inErr2.Message)
})
t.Run("success: implement AppError interface", func(t *testing.T) {
initConfig(okConfig)
assert.Nil(t, NewValidationError())
vldErr := NewValidationError(
&testVldErr{defaultAppError: New(err3rdPartyVld1).(*defaultAppError)},
&testVldErr{defaultAppError: New(err3rdPartyVld2).(*defaultAppError)},
&testVldErr{defaultAppError: New(err3rdPartyVld3).(*defaultAppError)},
)
result := vldErr.Build(LanguageEn)
errInfo := result.ErrorInfo
assert.Equal(t, 0, len(result.TransMissingKeys))
assert.Equal(t, "(ErrValidation)-in-en", errInfo.Message)
assert.Equal(t, 3, len(errInfo.InnerErrors))
inErr0 := errInfo.InnerErrors[0]
assert.Equal(t, http.StatusBadRequest, inErr0.Status)
assert.Equal(t, "ErrValidationItem", inErr0.Code)
assert.Equal(t, "(ErrValidation1)-in-en", inErr0.Message)
inErr1 := errInfo.InnerErrors[1]
assert.Equal(t, http.StatusBadRequest, inErr1.Status)
assert.Equal(t, "ErrValidationItem", inErr1.Code)
assert.Equal(t, "(ErrValidation2)-in-en", inErr1.Message)
inErr2 := errInfo.InnerErrors[2]
assert.Equal(t, http.StatusBadRequest, inErr2.Status)
assert.Equal(t, "ErrValidationItem", inErr2.Code)
assert.Equal(t, "(ErrValidation3)-in-en", inErr2.Message)
})
}