3) list services JSON errors
This commit is contained in:
parent
4a1d968539
commit
eab3361ad7
9 changed files with 48 additions and 39 deletions
|
@ -58,13 +58,13 @@ func (c *CharacteristicTypes) marshal() ([]byte, error) {
|
||||||
return json.Marshal(&CharacteristicTypesJSON{CharacteristicTypes: c})
|
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 {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, ErrMustProvideOptionsJSON
|
||||||
}
|
}
|
||||||
var opt ListOptions
|
var opt ListOptions
|
||||||
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
var vals []interface{}
|
var vals []interface{}
|
||||||
|
@ -91,7 +91,7 @@ func (c CharacteristicTypeService) list(val *url.Values) (entity, error) {
|
||||||
characteristic_types := make(CharacteristicTypes, 0)
|
characteristic_types := make(CharacteristicTypes, 0)
|
||||||
err := DBH.Select(&characteristic_types, sql, vals...)
|
err := DBH.Select(&characteristic_types, sql, vals...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return &characteristic_types, nil
|
return &characteristic_types, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,13 +60,13 @@ func (c *Characteristics) marshal() ([]byte, error) {
|
||||||
return json.Marshal(&CharacteristicsJSON{Characteristics: c})
|
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 {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, ErrMustProvideOptionsJSON
|
||||||
}
|
}
|
||||||
var opt ListOptions
|
var opt ListOptions
|
||||||
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
var vals []interface{}
|
var vals []interface{}
|
||||||
|
@ -93,7 +93,7 @@ func (c CharacteristicService) list(val *url.Values) (entity, error) {
|
||||||
characteristics := make(Characteristics, 0)
|
characteristics := make(Characteristics, 0)
|
||||||
err := DBH.Select(&characteristics, sql, vals...)
|
err := DBH.Select(&characteristics, sql, vals...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return &characteristics, nil
|
return &characteristics, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ type getter interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type lister interface {
|
type lister interface {
|
||||||
list(*url.Values) (entity, error)
|
list(*url.Values) (entity, *appError)
|
||||||
}
|
}
|
||||||
|
|
||||||
type updater interface {
|
type updater interface {
|
||||||
|
|
26
handlers.go
26
handlers.go
|
@ -79,7 +79,7 @@ func Handler() http.Handler {
|
||||||
m.Handle("/authenticate", tokenHandler(j.GenerateToken())).Methods("POST")
|
m.Handle("/authenticate", tokenHandler(j.GenerateToken())).Methods("POST")
|
||||||
|
|
||||||
// Auth routes
|
// 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", 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(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(http.HandlerFunc(handleUpdater(userService)), verifyClaims)).Methods("PUT")
|
||||||
|
@ -94,19 +94,19 @@ func Handler() http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
routes := []r{
|
routes := []r{
|
||||||
// r{handleLister(speciesService), "GET", "/species"},
|
r{handleLister(speciesService), "GET", "/species"},
|
||||||
// r{handleCreater(speciesService), "POST", "/species"},
|
// r{handleCreater(speciesService), "POST", "/species"},
|
||||||
r{handleGetter(speciesService), "GET", "/species/{Id:.+}"},
|
r{handleGetter(speciesService), "GET", "/species/{Id:.+}"},
|
||||||
// r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"},
|
// r{handleUpdater(speciesService), "PUT", "/species/{Id:.+}"},
|
||||||
// r{handleLister(strainService), "GET", "/strains"},
|
r{handleLister(strainService), "GET", "/strains"},
|
||||||
// r{handleCreater(strainService), "POST", "/strains"},
|
// r{handleCreater(strainService), "POST", "/strains"},
|
||||||
r{handleGetter(strainService), "GET", "/strains/{Id:.+}"},
|
r{handleGetter(strainService), "GET", "/strains/{Id:.+}"},
|
||||||
// r{handleUpdater(strainService), "PUT", "/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{handleGetter(characteristicService), "GET", "/characteristics/{Id:.+}"},
|
||||||
// r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"},
|
r{handleLister(characteristicTypeService), "GET", "/characteristicTypes"},
|
||||||
r{handleGetter(characteristicTypeService), "GET", "/characteristicTypes/{Id:.+}"},
|
r{handleGetter(characteristicTypeService), "GET", "/characteristicTypes/{Id:.+}"},
|
||||||
// r{handleLister(measurementService), "GET", "/measurements"},
|
r{handleLister(measurementService), "GET", "/measurements"},
|
||||||
r{handleGetter(measurementService), "GET", "/measurements/{Id:.+}"},
|
r{handleGetter(measurementService), "GET", "/measurements/{Id:.+}"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,21 +138,21 @@ func handleGetter(g getter) errorHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLister(l lister) http.HandlerFunc {
|
func handleLister(l lister) errorHandler {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
opt := r.URL.Query()
|
opt := r.URL.Query()
|
||||||
opt.Add("Genus", mux.Vars(r)["genus"])
|
opt.Add("Genus", mux.Vars(r)["genus"])
|
||||||
|
|
||||||
es, err := l.list(&opt)
|
es, appErr := l.list(&opt)
|
||||||
if err != nil {
|
if appErr != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return appErr
|
||||||
return
|
|
||||||
}
|
}
|
||||||
data, err := es.marshal()
|
data, err := es.marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
return newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
helpers.go
11
helpers.go
|
@ -1,6 +1,15 @@
|
||||||
package main
|
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
|
// ListOptions specifies general pagination options for fetching a list of results
|
||||||
type ListOptions struct {
|
type ListOptions struct {
|
||||||
|
|
|
@ -67,9 +67,9 @@ func (m *Measurements) marshal() ([]byte, error) {
|
||||||
return json.Marshal(&MeasurementsJSON{Measurements: m})
|
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 {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, ErrMustProvideOptionsJSON
|
||||||
}
|
}
|
||||||
var opt struct {
|
var opt struct {
|
||||||
ListOptions
|
ListOptions
|
||||||
|
@ -77,7 +77,7 @@ func (m MeasurementService) list(val *url.Values) (entity, error) {
|
||||||
Characteristics []int64 `schema:"characteristic[]"`
|
Characteristics []int64 `schema:"characteristic[]"`
|
||||||
}
|
}
|
||||||
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
var vals []interface{}
|
var vals []interface{}
|
||||||
|
@ -134,7 +134,7 @@ func (m MeasurementService) list(val *url.Values) (entity, error) {
|
||||||
measurements := make(Measurements, 0)
|
measurements := make(Measurements, 0)
|
||||||
err := DBH.Select(&measurements, sql, vals...)
|
err := DBH.Select(&measurements, sql, vals...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return &measurements, nil
|
return &measurements, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,13 +72,13 @@ func (s SpeciesService) unmarshal(b []byte) (entity, error) {
|
||||||
return sj.Species, err
|
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 {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, ErrMustProvideOptionsJSON
|
||||||
}
|
}
|
||||||
var opt ListOptions
|
var opt ListOptions
|
||||||
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
var vals []interface{}
|
var vals []interface{}
|
||||||
|
@ -107,7 +107,7 @@ func (s SpeciesService) list(val *url.Values) (entity, error) {
|
||||||
species := make(ManySpecies, 0)
|
species := make(ManySpecies, 0)
|
||||||
err := DBH.Select(&species, sql, vals...)
|
err := DBH.Select(&species, sql, vals...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return &species, nil
|
return &species, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,13 +74,13 @@ func (s StrainService) unmarshal(b []byte) (entity, error) {
|
||||||
return sj.Strain, err
|
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 {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, ErrMustProvideOptionsJSON
|
||||||
}
|
}
|
||||||
var opt ListOptions
|
var opt ListOptions
|
||||||
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
var vals []interface{}
|
var vals []interface{}
|
||||||
|
@ -109,7 +109,7 @@ func (s StrainService) list(val *url.Values) (entity, error) {
|
||||||
strains := make(Strains, 0)
|
strains := make(Strains, 0)
|
||||||
err := DBH.Select(&strains, sql, vals...)
|
err := DBH.Select(&strains, sql, vals...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return &strains, nil
|
return &strains, nil
|
||||||
}
|
}
|
||||||
|
|
8
users.go
8
users.go
|
@ -89,19 +89,19 @@ func (u *User) validate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u UserService) list(val *url.Values) (entity, error) {
|
func (u UserService) list(val *url.Values) (entity, *appError) {
|
||||||
if val == nil {
|
if val == nil {
|
||||||
return nil, errors.New("must provide options")
|
return nil, ErrMustProvideOptionsJSON
|
||||||
}
|
}
|
||||||
var opt ListOptions
|
var opt ListOptions
|
||||||
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
if err := schemaDecoder.Decode(&opt, *val); err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
users := make(Users, 0)
|
users := make(Users, 0)
|
||||||
sql := `SELECT * FROM users;`
|
sql := `SELECT * FROM users;`
|
||||||
if err := DBH.Select(&users, sql); err != nil {
|
if err := DBH.Select(&users, sql); err != nil {
|
||||||
return nil, err
|
return nil, newJSONError(err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return &users, nil
|
return &users, nil
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue