Skip to content

Commit

Permalink
fix(errors): add overlooked validation errors to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Anderson committed Jun 22, 2016
1 parent 1bbb1c7 commit 2eef6c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ 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."
)

var (
Expand All @@ -36,6 +38,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 +68,8 @@ 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)
)

func checkForErrors(res *http.Response) error {
Expand All @@ -88,6 +94,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,6 +138,10 @@ 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
Expand Down
14 changes: 14 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,13 @@ 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: 401,
Expand Down

0 comments on commit 2eef6c5

Please sign in to comment.