From eb92319bdc29047806161bc2bc59db532afa089f Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 24 Jun 2015 18:05:50 -0800 Subject: [PATCH] 5) update services JSON errors --- entities.go | 2 +- handlers.go | 30 +++++++++++++----------------- species.go | 13 +++++++------ strains.go | 13 +++++++------ users.go | 8 +++++--- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/entities.go b/entities.go index 65141f3..55eeb45 100644 --- a/entities.go +++ b/entities.go @@ -15,7 +15,7 @@ type lister interface { } type updater interface { - update(int64, *entity, Claims) error + update(int64, *entity, Claims) *appError unmarshal([]byte) (entity, error) } diff --git a/handlers.go b/handlers.go index 0ca835a..aedae2a 100644 --- a/handlers.go +++ b/handlers.go @@ -82,7 +82,7 @@ func Handler() http.Handler { m.Handle("/users", j.Secure(errorHandler(handleLister(userService)), verifyClaims)).Methods("GET") m.Handle("/users", j.Secure(errorHandler(handleCreater(userService)), verifyClaims)).Methods("POST") m.Handle("/users/{Id:.+}", j.Secure(errorHandler(handleGetter(userService)), verifyClaims)).Methods("GET") - m.Handle("/users/{Id:.+}", j.Secure(http.HandlerFunc(handleUpdater(userService)), verifyClaims)).Methods("PUT") + m.Handle("/users/{Id:.+}", j.Secure(errorHandler(handleUpdater(userService)), verifyClaims)).Methods("PUT") // Path-based pattern matching subrouter s := m.PathPrefix("/{genus}").Subrouter() @@ -97,11 +97,11 @@ func Handler() http.Handler { r{handleLister(speciesService), "GET", "/species"}, r{handleCreater(speciesService), "POST", "/species"}, r{handleGetter(speciesService), "GET", "/species/{Id:.+}"}, - // r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"}, + r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"}, r{handleLister(strainService), "GET", "/strains"}, r{handleCreater(strainService), "POST", "/strains"}, r{handleGetter(strainService), "GET", "/strains/{Id:.+}"}, - // r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"}, + r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"}, r{handleLister(characteristicService), "GET", "/characteristics"}, r{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"}, r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"}, @@ -156,41 +156,37 @@ func handleLister(l lister) errorHandler { } } -func handleUpdater(u updater) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func handleUpdater(u updater) errorHandler { + return func(w http.ResponseWriter, r *http.Request) *appError { id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + return newJSONError(err, http.StatusInternalServerError) } bodyBytes, err := ioutil.ReadAll(r.Body) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + return newJSONError(err, http.StatusInternalServerError) } e, err := u.unmarshal(bodyBytes) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + return newJSONError(err, http.StatusInternalServerError) } c := context.Get(r, "claims") var claims Claims = c.(Claims) - err = u.update(id, &e, claims) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + appErr := u.update(id, &e, claims) + if appErr != nil { + return appErr } data, err := e.marshal() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + return newJSONError(err, http.StatusInternalServerError) } w.Write(data) + return nil } } diff --git a/species.go b/species.go index 406bb01..5b9507d 100644 --- a/species.go +++ b/species.go @@ -12,9 +12,10 @@ import ( ) var ( - ErrSpeciesNotFound = errors.New("Species not found") - ErrSpeciesNotFoundJSON = newJSONError(ErrSpeciesNotFound, http.StatusNotFound) - ErrSpeciesNotUpdated = errors.New("Species not updated") + ErrSpeciesNotFound = errors.New("Species not found") + ErrSpeciesNotFoundJSON = newJSONError(ErrSpeciesNotFound, http.StatusNotFound) + ErrSpeciesNotUpdated = errors.New("Species not updated") + ErrSpeciesNotUpdatedJSON = newJSONError(ErrSpeciesNotUpdated, http.StatusBadRequest) ) func init() { @@ -130,7 +131,7 @@ func (s SpeciesService) get(id int64, genus string) (entity, *appError) { return &species, nil } -func (s SpeciesService) update(id int64, e *entity, claims Claims) error { +func (s SpeciesService) update(id int64, e *entity, claims Claims) *appError { species := (*e).(*Species) species.UpdatedBy = claims.Sub species.UpdatedAt = time.Now() @@ -138,10 +139,10 @@ func (s SpeciesService) update(id int64, e *entity, claims Claims) error { count, err := DBH.Update(species.SpeciesBase) if err != nil { - return err + return newJSONError(err, http.StatusInternalServerError) } if count != 1 { - return ErrSpeciesNotUpdated + return ErrSpeciesNotUpdatedJSON } return nil } diff --git a/strains.go b/strains.go index 03f6bd3..da04940 100644 --- a/strains.go +++ b/strains.go @@ -12,9 +12,10 @@ import ( ) var ( - ErrStrainNotFound = errors.New("Strain not found") - ErrStrainNotFoundJSON = newJSONError(ErrStrainNotFound, http.StatusNotFound) - ErrStrainNotUpdated = errors.New("Strain not updated") + ErrStrainNotFound = errors.New("Strain not found") + ErrStrainNotFoundJSON = newJSONError(ErrStrainNotFound, http.StatusNotFound) + ErrStrainNotUpdated = errors.New("Strain not updated") + ErrStrainNotUpdatedJSON = newJSONError(ErrStrainNotUpdated, http.StatusBadRequest) ) func init() { @@ -133,7 +134,7 @@ func (s StrainService) get(id int64, genus string) (entity, *appError) { return &strain, nil } -func (s StrainService) update(id int64, e *entity, claims Claims) error { +func (s StrainService) update(id int64, e *entity, claims Claims) *appError { strain := (*e).(*Strain) strain.UpdatedBy = claims.Sub strain.UpdatedAt = time.Now() @@ -141,10 +142,10 @@ func (s StrainService) update(id int64, e *entity, claims Claims) error { count, err := DBH.Update(strain.StrainBase) if err != nil { - return err + return newJSONError(err, http.StatusInternalServerError) } if count != 1 { - return ErrStrainNotUpdated + return ErrStrainNotUpdatedJSON } return nil } diff --git a/users.go b/users.go index 72e9270..daf329a 100644 --- a/users.go +++ b/users.go @@ -14,6 +14,8 @@ import ( var ( ErrUserNotFound = errors.New("User not found") ErrUserNotFoundJSON = newJSONError(ErrUserNotFound, http.StatusNotFound) + ErrUserNotUpdated = errors.New("User not updated") + ErrUserNotUpdatedJSON = newJSONError(ErrUserNotUpdated, http.StatusBadRequest) ErrInvalidEmailOrPassword = errors.New("Invalid email or password") ) @@ -118,17 +120,17 @@ func (u UserService) get(id int64, genus string) (entity, *appError) { return &user, nil } -func (u UserService) update(id int64, e *entity, claims Claims) error { +func (u UserService) update(id int64, e *entity, claims Claims) *appError { user := (*e).(*User) user.UpdatedAt = time.Now() user.Id = id count, err := DBH.Update(user) if err != nil { - return err + return newJSONError(err, http.StatusInternalServerError) } if count != 1 { - return ErrStrainNotUpdated + return ErrUserNotUpdatedJSON } return nil }