Skip to content

Commit 3b277af

Browse files
fix(errors): add overlooked validation errors to SDK (#15)
1 parent 1bbb1c7 commit 3b277af

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

errors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const (
2424
invalidDomainMsg = "Hostname does not look valid."
2525
invalidVersionMsg = "version cannot be below 0"
2626
invalidKeyMsg = "Key contains invalid base64 chars"
27+
duplicateUserMsg = "A user with that username already exists."
28+
invalidEmailMsg = "Enter a valid email address."
29+
invalidTagMsg = "No nodes matched the provided labels"
2730
)
2831

2932
var (
@@ -36,6 +39,8 @@ var (
3639
ErrMethodNotAllowed = errors.New("Method Not Allowed")
3740
// ErrInvalidUsername is returned when the user specifies an invalid or missing username.
3841
ErrInvalidUsername = errors.New(invalidUserMsg)
42+
// ErrDuplicateUsername is returned when trying to register a user that already exists.
43+
ErrDuplicateUsername = errors.New(duplicateUserMsg)
3944
// ErrMissingPassword is returned when a password is not sent with the request.
4045
ErrMissingPassword = errors.New("A Password is required")
4146
// ErrLogin is returned when the api cannot login fails with provided username and password
@@ -64,6 +69,10 @@ var (
6469
ErrInvalidVersion = errors.New("The given version is invalid")
6570
// ErrMissingID is returned when a ID is missing
6671
ErrMissingID = errors.New("An id is required")
72+
// ErrInvalidEmail is returned when a user gives an invalid email.
73+
ErrInvalidEmail = errors.New(invalidEmailMsg)
74+
// ErrTagNotFound is returned when no node can be found that matches the tag
75+
ErrTagNotFound = errors.New(invalidTagMsg)
6776
)
6877

6978
func checkForErrors(res *http.Response) error {
@@ -88,6 +97,10 @@ func checkForErrors(res *http.Response) error {
8897
return ErrInvalidUsername
8998
}
9099

100+
if scanResponse(bodyMap, "username", []string{duplicateUserMsg}, true) {
101+
return ErrDuplicateUsername
102+
}
103+
91104
if scanResponse(bodyMap, "password", []string{fieldReqMsg}, true) {
92105
return ErrMissingPassword
93106
}
@@ -128,13 +141,20 @@ func checkForErrors(res *http.Response) error {
128141
return ErrMissingID
129142
}
130143

144+
if scanResponse(bodyMap, "email", []string{invalidEmailMsg}, true) {
145+
return ErrInvalidEmail
146+
}
147+
131148
if v, ok := bodyMap["detail"].(string); ok {
132149
if strings.Contains(v, invalidPodMsg) {
133150
return ErrPodNotFound
134151
}
135152
if strings.Contains(v, invalidVersionMsg) {
136153
return ErrInvalidVersion
137154
}
155+
if strings.Contains(v, invalidTagMsg) {
156+
return ErrTagNotFound
157+
}
138158
}
139159

140160
return unknownError(res.StatusCode, bodyMap)

errors_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ func TestErrors(t *testing.T) {
7575
},
7676
expected: ErrInvalidUsername,
7777
},
78+
errorTest{
79+
res: &http.Response{
80+
StatusCode: 400,
81+
Body: readCloser(`{"username":["A user with that username already exists."]}`),
82+
},
83+
expected: ErrDuplicateUsername,
84+
},
7885
errorTest{
7986
res: &http.Response{
8087
StatusCode: 400,
@@ -180,6 +187,20 @@ func TestErrors(t *testing.T) {
180187
},
181188
expected: ErrMissingID,
182189
},
190+
errorTest{
191+
res: &http.Response{
192+
StatusCode: 400,
193+
Body: readCloser(`{"email":["Enter a valid email address."]}`),
194+
},
195+
expected: ErrInvalidEmail,
196+
},
197+
errorTest{
198+
res: &http.Response{
199+
StatusCode: 400,
200+
Body: readCloser(`{"detail":"No nodes matched the provided labels: foo=bar"}`),
201+
},
202+
expected: ErrTagNotFound,
203+
},
183204
errorTest{
184205
res: &http.Response{
185206
StatusCode: 401,

0 commit comments

Comments
 (0)