Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(errors): add overlooked validation errors to SDK #15

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const (
invalidDomainMsg = "Hostname does not look valid."
invalidVersionMsg = "version cannot be below 0"
invalidKeyMsg = "Key contains invalid base64 chars"
duplicateUserMsg = "A user with that username already exists."
invalidEmailMsg = "Enter a valid email address."
invalidTagMsg = "No nodes matched the provided labels"
)

var (
Expand All @@ -36,6 +39,8 @@ var (
ErrMethodNotAllowed = errors.New("Method Not Allowed")
// ErrInvalidUsername is returned when the user specifies an invalid or missing username.
ErrInvalidUsername = errors.New(invalidUserMsg)
// ErrDuplicateUsername is returned when trying to register a user that already exists.
ErrDuplicateUsername = errors.New(duplicateUserMsg)
// ErrMissingPassword is returned when a password is not sent with the request.
ErrMissingPassword = errors.New("A Password is required")
// ErrLogin is returned when the api cannot login fails with provided username and password
Expand Down Expand Up @@ -64,6 +69,10 @@ var (
ErrInvalidVersion = errors.New("The given version is invalid")
// ErrMissingID is returned when a ID is missing
ErrMissingID = errors.New("An id is required")
// ErrInvalidEmail is returned when a user gives an invalid email.
ErrInvalidEmail = errors.New(invalidEmailMsg)
// ErrTagNotFound is returned when no node can be found that matches the tag
ErrTagNotFound = errors.New(invalidTagMsg)
)

func checkForErrors(res *http.Response) error {
Expand All @@ -88,6 +97,10 @@ func checkForErrors(res *http.Response) error {
return ErrInvalidUsername
}

if scanResponse(bodyMap, "username", []string{duplicateUserMsg}, true) {
return ErrDuplicateUsername
}

if scanResponse(bodyMap, "password", []string{fieldReqMsg}, true) {
return ErrMissingPassword
}
Expand Down Expand Up @@ -128,13 +141,20 @@ func checkForErrors(res *http.Response) error {
return ErrMissingID
}

if scanResponse(bodyMap, "email", []string{invalidEmailMsg}, true) {
return ErrInvalidEmail
}

if v, ok := bodyMap["detail"].(string); ok {
if strings.Contains(v, invalidPodMsg) {
return ErrPodNotFound
}
if strings.Contains(v, invalidVersionMsg) {
return ErrInvalidVersion
}
if strings.Contains(v, invalidTagMsg) {
return ErrTagNotFound
}
}

return unknownError(res.StatusCode, bodyMap)
Expand Down
21 changes: 21 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ func TestErrors(t *testing.T) {
},
expected: ErrInvalidUsername,
},
errorTest{
res: &http.Response{
StatusCode: 400,
Body: readCloser(`{"username":["A user with that username already exists."]}`),
},
expected: ErrDuplicateUsername,
},
errorTest{
res: &http.Response{
StatusCode: 400,
Expand Down Expand Up @@ -180,6 +187,20 @@ func TestErrors(t *testing.T) {
},
expected: ErrMissingID,
},
errorTest{
res: &http.Response{
StatusCode: 400,
Body: readCloser(`{"email":["Enter a valid email address."]}`),
},
expected: ErrInvalidEmail,
},
errorTest{
res: &http.Response{
StatusCode: 400,
Body: readCloser(`{"detail":"No nodes matched the provided labels: foo=bar"}`),
},
expected: ErrTagNotFound,
},
errorTest{
res: &http.Response{
StatusCode: 401,
Expand Down