Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
api: improve error handling for removeUser handler
Browse files Browse the repository at this point in the history
Related to #183.
  • Loading branch information
Francisco Souza committed Feb 19, 2015
1 parent e33c216 commit da23a77
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,11 @@ func newUser(w http.ResponseWriter, r *http.Request) {
func removeUser(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
if err := user.Remove(name); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
status := http.StatusInternalServerError
if err == user.ErrUserNotFound {
status = http.StatusNotFound
}
http.Error(w, err.Error(), status)
return
}
fmt.Fprintf(w, "User \"%s\" successfully removed\n", name)
Expand Down
9 changes: 9 additions & 0 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,15 @@ func (s *S) TestRemoveUserShouldRemoveFromDB(c *check.C) {
c.Assert(lenght, check.Equals, 0)
}

func (s *S) TestRemoveUserNotFound(c *check.C) {
request, err := http.NewRequest("DELETE", "/user/unknown-user", nil)
c.Assert(err, check.IsNil)
recorder := httptest.NewRecorder()
s.router.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusNotFound)
c.Assert(recorder.Body.String(), check.Equals, "user not found\n")
}

func (s *S) TestRemoveRepository(c *check.C) {
r, err := repository.New("myRepo", []string{"pippin"}, []string{""}, true)
c.Assert(err, check.IsNil)
Expand Down

0 comments on commit da23a77

Please sign in to comment.