Skip to content

Commit

Permalink
fix(errors): add overlooked validation errors to SDK (deis#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Anderson authored Jun 22, 2016
1 parent 71f1212 commit 8ba4159
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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

0 comments on commit 8ba4159

Please sign in to comment.