From eab3361ad7490087e899f126ec0661501f487844 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 24 Jun 2015 17:29:00 -0800 Subject: [PATCH] 3) list services JSON errors --- characteristic_types.go | 8 ++++---- characteristics.go | 8 ++++---- entities.go | 2 +- handlers.go | 26 +++++++++++++------------- helpers.go | 11 ++++++++++- measurements.go | 8 ++++---- species.go | 8 ++++---- strains.go | 8 ++++---- users.go | 8 ++++---- 9 files changed, 48 insertions(+), 39 deletions(-) diff --git a/characteristic_types.go b/characteristic_types.go index b1d2088..42caf66 100644 --- a/characteristic_types.go +++ b/characteristic_types.go @@ -58,13 +58,13 @@ func (c *CharacteristicTypes) marshal() ([]byte, error) { return json.Marshal(&CharacteristicTypesJSON{CharacteristicTypes: c}) } -func (c CharacteristicTypeService) list(val *url.Values) (entity, error) { +func (c CharacteristicTypeService) list(val *url.Values) (entity, *appError) { if val == nil { - return nil, errors.New("must provide options") + return nil, ErrMustProvideOptionsJSON } var opt ListOptions if err := schemaDecoder.Decode(&opt, *val); err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } var vals []interface{} @@ -91,7 +91,7 @@ func (c CharacteristicTypeService) list(val *url.Values) (entity, error) { characteristic_types := make(CharacteristicTypes, 0) err := DBH.Select(&characteristic_types, sql, vals...) if err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } return &characteristic_types, nil } diff --git a/characteristics.go b/characteristics.go index 2457534..9f5a88c 100644 --- a/characteristics.go +++ b/characteristics.go @@ -60,13 +60,13 @@ func (c *Characteristics) marshal() ([]byte, error) { return json.Marshal(&CharacteristicsJSON{Characteristics: c}) } -func (c CharacteristicService) list(val *url.Values) (entity, error) { +func (c CharacteristicService) list(val *url.Values) (entity, *appError) { if val == nil { - return nil, errors.New("must provide options") + return nil, ErrMustProvideOptionsJSON } var opt ListOptions if err := schemaDecoder.Decode(&opt, *val); err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } var vals []interface{} @@ -93,7 +93,7 @@ func (c CharacteristicService) list(val *url.Values) (entity, error) { characteristics := make(Characteristics, 0) err := DBH.Select(&characteristics, sql, vals...) if err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } return &characteristics, nil } diff --git a/entities.go b/entities.go index 1af85c0..e85381e 100644 --- a/entities.go +++ b/entities.go @@ -11,7 +11,7 @@ type getter interface { } type lister interface { - list(*url.Values) (entity, error) + list(*url.Values) (entity, *appError) } type updater interface { diff --git a/handlers.go b/handlers.go index 80b6bb1..f1f8aaa 100644 --- a/handlers.go +++ b/handlers.go @@ -79,7 +79,7 @@ func Handler() http.Handler { m.Handle("/authenticate", tokenHandler(j.GenerateToken())).Methods("POST") // Auth routes - m.Handle("/users", j.Secure(http.HandlerFunc(handleLister(userService)), verifyClaims)).Methods("GET") + m.Handle("/users", j.Secure(errorHandler(handleLister(userService)), verifyClaims)).Methods("GET") m.Handle("/users", j.Secure(http.HandlerFunc(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") @@ -94,19 +94,19 @@ func Handler() http.Handler { } routes := []r{ - // r{handleLister(speciesService), "GET", "/species"}, + r{handleLister(speciesService), "GET", "/species"}, // r{handleCreater(speciesService), "POST", "/species"}, r{handleGetter(speciesService), "GET", "/species/{Id:.+}"}, // r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"}, - // r{handleLister(strainService), "GET", "/strains"}, + r{handleLister(strainService), "GET", "/strains"}, // r{handleCreater(strainService), "POST", "/strains"}, r{handleGetter(strainService), "GET", "/strains/{Id:.+}"}, // r{handleUpdater(strainService), "PUT", "/strains/{Id:.+}"}, - // r{handleLister(characteristicService), "GET", "/characteristics"}, + r{handleLister(characteristicService), "GET", "/characteristics"}, r{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"}, - // r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"}, + r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"}, r{handleGetter(characteristicTypeService), "GET", "/characteristicTypes/{Id:.+}"}, - // r{handleLister(measurementService), "GET", "/measurements"}, + r{handleLister(measurementService), "GET", "/measurements"}, r{handleGetter(measurementService), "GET", "/measurements/{Id:.+}"}, } @@ -138,21 +138,21 @@ func handleGetter(g getter) errorHandler { } } -func handleLister(l lister) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { +func handleLister(l lister) errorHandler { + return func(w http.ResponseWriter, r *http.Request) *appError { opt := r.URL.Query() opt.Add("Genus", mux.Vars(r)["genus"]) - es, err := l.list(&opt) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + es, appErr := l.list(&opt) + if appErr != nil { + return appErr } data, err := es.marshal() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + return newJSONError(err, http.StatusInternalServerError) } w.Write(data) + return nil } } diff --git a/helpers.go b/helpers.go index b7c7795..4711c7a 100644 --- a/helpers.go +++ b/helpers.go @@ -1,6 +1,15 @@ package main -import "fmt" +import ( + "errors" + "fmt" + "net/http" +) + +var ( + ErrMustProvideOptions = errors.New("Must provide necessary options") + ErrMustProvideOptionsJSON = newJSONError(ErrMustProvideOptions, http.StatusBadRequest) +) // ListOptions specifies general pagination options for fetching a list of results type ListOptions struct { diff --git a/measurements.go b/measurements.go index 2f3d888..a83cb1d 100644 --- a/measurements.go +++ b/measurements.go @@ -67,9 +67,9 @@ func (m *Measurements) marshal() ([]byte, error) { return json.Marshal(&MeasurementsJSON{Measurements: m}) } -func (m MeasurementService) list(val *url.Values) (entity, error) { +func (m MeasurementService) list(val *url.Values) (entity, *appError) { if val == nil { - return nil, errors.New("must provide options") + return nil, ErrMustProvideOptionsJSON } var opt struct { ListOptions @@ -77,7 +77,7 @@ func (m MeasurementService) list(val *url.Values) (entity, error) { Characteristics []int64 `schema:"characteristic[]"` } if err := schemaDecoder.Decode(&opt, *val); err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } var vals []interface{} @@ -134,7 +134,7 @@ func (m MeasurementService) list(val *url.Values) (entity, error) { measurements := make(Measurements, 0) err := DBH.Select(&measurements, sql, vals...) if err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } return &measurements, nil } diff --git a/species.go b/species.go index ff7fa38..80c2089 100644 --- a/species.go +++ b/species.go @@ -72,13 +72,13 @@ func (s SpeciesService) unmarshal(b []byte) (entity, error) { return sj.Species, err } -func (s SpeciesService) list(val *url.Values) (entity, error) { +func (s SpeciesService) list(val *url.Values) (entity, *appError) { if val == nil { - return nil, errors.New("must provide options") + return nil, ErrMustProvideOptionsJSON } var opt ListOptions if err := schemaDecoder.Decode(&opt, *val); err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } var vals []interface{} @@ -107,7 +107,7 @@ func (s SpeciesService) list(val *url.Values) (entity, error) { species := make(ManySpecies, 0) err := DBH.Select(&species, sql, vals...) if err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } return &species, nil } diff --git a/strains.go b/strains.go index cb88da3..94b133a 100644 --- a/strains.go +++ b/strains.go @@ -74,13 +74,13 @@ func (s StrainService) unmarshal(b []byte) (entity, error) { return sj.Strain, err } -func (s StrainService) list(val *url.Values) (entity, error) { +func (s StrainService) list(val *url.Values) (entity, *appError) { if val == nil { - return nil, errors.New("must provide options") + return nil, ErrMustProvideOptionsJSON } var opt ListOptions if err := schemaDecoder.Decode(&opt, *val); err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } var vals []interface{} @@ -109,7 +109,7 @@ func (s StrainService) list(val *url.Values) (entity, error) { strains := make(Strains, 0) err := DBH.Select(&strains, sql, vals...) if err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } return &strains, nil } diff --git a/users.go b/users.go index ea9e8c0..d1d2114 100644 --- a/users.go +++ b/users.go @@ -89,19 +89,19 @@ func (u *User) validate() error { return nil } -func (u UserService) list(val *url.Values) (entity, error) { +func (u UserService) list(val *url.Values) (entity, *appError) { if val == nil { - return nil, errors.New("must provide options") + return nil, ErrMustProvideOptionsJSON } var opt ListOptions if err := schemaDecoder.Decode(&opt, *val); err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } users := make(Users, 0) sql := `SELECT * FROM users;` if err := DBH.Select(&users, sql); err != nil { - return nil, err + return nil, newJSONError(err, http.StatusInternalServerError) } return &users, nil }