...
1 package smithy
2
3 import (
4 "bytes"
5 "fmt"
6 "strings"
7 )
8
9
10
11 type InvalidParamsError struct {
12
13 Context string
14 errs []InvalidParamError
15 }
16
17
18
19
20 func (e *InvalidParamsError) Add(err InvalidParamError) {
21 err.SetContext(e.Context)
22 e.errs = append(e.errs, err)
23 }
24
25
26
27
28
29
30 func (e *InvalidParamsError) AddNested(nestedCtx string, nested InvalidParamsError) {
31 for _, err := range nested.errs {
32 err.SetContext(e.Context)
33 err.AddNestedContext(nestedCtx)
34 e.errs = append(e.errs, err)
35 }
36 }
37
38
39 func (e *InvalidParamsError) Len() int {
40 return len(e.errs)
41 }
42
43
44 func (e InvalidParamsError) Error() string {
45 w := &bytes.Buffer{}
46 fmt.Fprintf(w, "%d validation error(s) found.\n", len(e.errs))
47
48 for _, err := range e.errs {
49 fmt.Fprintf(w, "- %s\n", err.Error())
50 }
51
52 return w.String()
53 }
54
55
56 func (e InvalidParamsError) Errs() []error {
57 errs := make([]error, len(e.errs))
58 for i := 0; i < len(errs); i++ {
59 errs[i] = e.errs[i]
60 }
61
62 return errs
63 }
64
65
66 type InvalidParamError interface {
67 error
68
69
70 Field() string
71
72
73 SetContext(string)
74
75
76 AddNestedContext(string)
77 }
78
79 type invalidParamError struct {
80 context string
81 nestedContext string
82 field string
83 reason string
84 }
85
86
87 func (e invalidParamError) Error() string {
88 return fmt.Sprintf("%s, %s.", e.reason, e.Field())
89 }
90
91
92 func (e invalidParamError) Field() string {
93 sb := &strings.Builder{}
94 sb.WriteString(e.context)
95 if sb.Len() > 0 {
96 if len(e.nestedContext) == 0 || (len(e.nestedContext) > 0 && e.nestedContext[:1] != "[") {
97 sb.WriteRune('.')
98 }
99 }
100 if len(e.nestedContext) > 0 {
101 sb.WriteString(e.nestedContext)
102 sb.WriteRune('.')
103 }
104 sb.WriteString(e.field)
105 return sb.String()
106 }
107
108
109 func (e *invalidParamError) SetContext(ctx string) {
110 e.context = ctx
111 }
112
113
114 func (e *invalidParamError) AddNestedContext(ctx string) {
115 if len(e.nestedContext) == 0 {
116 e.nestedContext = ctx
117 return
118 }
119
120 if e.nestedContext[:1] != "[" {
121 e.nestedContext = fmt.Sprintf("%s.%s", ctx, e.nestedContext)
122 return
123 }
124 e.nestedContext = ctx + e.nestedContext
125 }
126
127
128 type ParamRequiredError struct {
129 invalidParamError
130 }
131
132
133 func NewErrParamRequired(field string) *ParamRequiredError {
134 return &ParamRequiredError{
135 invalidParamError{
136 field: field,
137 reason: fmt.Sprintf("missing required field"),
138 },
139 }
140 }
141
View as plain text