Errors package

This commit is contained in:
Matthew Dillon 2015-10-01 14:45:36 -07:00
parent 0eec85ed08
commit a880fdea82
24 changed files with 215 additions and 169 deletions

View file

@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/models"
"github.com/thermokarst/bactdb/payloads"
@ -21,46 +22,46 @@ func (c CharacteristicService) Unmarshal(b []byte) (types.Entity, error) {
func (c CharacteristicService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
if val == nil {
return nil, helpers.ErrMustProvideOptionsJSON
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
}
var opt helpers.ListOptions
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
characteristics, err := models.ListCharacteristics(opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strains_opt, err := models.StrainOptsFromCharacteristics(opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strains, err := models.ListStrains(*strains_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
species_opt, err := models.SpeciesOptsFromStrains(*strains_opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
species, err := models.ListSpecies(*species_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
measurements_opt, err := models.MeasurementOptsFromCharacteristics(opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
measurements, err := models.ListMeasurements(*measurements_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
payload := payloads.Characteristics{
@ -79,27 +80,27 @@ func (c CharacteristicService) List(val *url.Values, claims *types.Claims) (type
func (c CharacteristicService) Get(id int64, genus string, claims *types.Claims) (types.Entity, *types.AppError) {
characteristic, err := models.GetCharacteristic(id, genus, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strains, strain_opts, err := models.StrainsFromCharacteristicId(id, genus, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
species_opt, err := models.SpeciesOptsFromStrains(*strain_opts)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
species, err := models.ListSpecies(*species_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
measurements, _, err := models.MeasurementsFromCharacteristicId(id, genus, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
payload := payloads.Characteristic{
@ -120,7 +121,7 @@ func (c CharacteristicService) Update(id int64, e *types.Entity, genus string, c
// First, handle Characteristic Type
id, err := models.InsertOrGetCharacteristicType(payload.Characteristic.CharacteristicType, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Characteristic.CanEdit = helpers.CanEdit(claims, payload.Characteristic.CreatedBy)
@ -129,26 +130,26 @@ func (c CharacteristicService) Update(id int64, e *types.Entity, genus string, c
// TODO: fix this
count, err := models.DBH.Update(payload.Characteristic.CharacteristicBase)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
// TODO: fix this
return types.NewJSONError(models.ErrCharacteristicNotUpdated, http.StatusBadRequest)
return NewJSONError(errors.CharacteristicNotUpdated, http.StatusBadRequest)
}
strains, strain_opts, err := models.StrainsFromCharacteristicId(id, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
species_opt, err := models.SpeciesOptsFromStrains(*strain_opts)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
species, err := models.ListSpecies(*species_opt, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Strains = strains
@ -166,19 +167,19 @@ func (c CharacteristicService) Create(e *types.Entity, genus string, claims *typ
id, err := models.InsertOrGetCharacteristicType(payload.Characteristic.CharacteristicType, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Characteristic.CharacteristicTypeId = id
// TODO: fix this
err = models.DBH.Insert(payload.Characteristic.CharacteristicBase)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
characteristic, err := models.GetCharacteristic(payload.Characteristic.Id, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Characteristic = characteristic

10
api/helpers.go Normal file
View file

@ -0,0 +1,10 @@
package api
import "github.com/thermokarst/bactdb/types"
func NewJSONError(err error, status int) *types.AppError {
return &types.AppError{
Error: types.ErrorJSON{Err: err},
Status: status,
}
}

View file

@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/models"
"github.com/thermokarst/bactdb/payloads"
@ -21,36 +22,36 @@ func (s MeasurementService) Unmarshal(b []byte) (types.Entity, error) {
func (m MeasurementService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
if val == nil {
return nil, helpers.ErrMustProvideOptionsJSON
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
}
var opt helpers.MeasurementListOptions
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
measurements, err := models.ListMeasurements(opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
char_opts, err := models.CharacteristicOptsFromMeasurements(opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
characteristics, err := models.ListCharacteristics(*char_opts, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strain_opts, err := models.StrainOptsFromMeasurements(opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strains, err := models.ListStrains(*strain_opts, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
payload := payloads.Measurements{
@ -65,7 +66,7 @@ func (m MeasurementService) List(val *url.Values, claims *types.Claims) (types.E
func (m MeasurementService) Get(id int64, genus string, claims *types.Claims) (types.Entity, *types.AppError) {
measurement, err := models.GetMeasurement(id, genus, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
payload := payloads.Measurement{
@ -83,7 +84,7 @@ func (s MeasurementService) Update(id int64, e *types.Entity, genus string, clai
if payload.Measurement.TextMeasurementType.Valid {
id, err := models.GetTextMeasurementTypeId(payload.Measurement.TextMeasurementType.String)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Measurement.TextMeasurementTypeId.Int64 = id
payload.Measurement.TextMeasurementTypeId.Valid = true
@ -92,16 +93,16 @@ func (s MeasurementService) Update(id int64, e *types.Entity, genus string, clai
// TODO: fix this
count, err := models.DBH.Update(payload.Measurement.MeasurementBase)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
// TODO: fix this
return types.NewJSONError(models.ErrStrainNotUpdated, http.StatusBadRequest)
return NewJSONError(errors.StrainNotUpdated, http.StatusBadRequest)
}
measurement, err := models.GetMeasurement(id, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Measurement = measurement
@ -114,7 +115,7 @@ func (m MeasurementService) Delete(id int64, genus string, claims *types.Claims)
// TODO: fix this
_, err := models.DBH.Exec(q, id)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
return nil
}
@ -126,7 +127,7 @@ func (m MeasurementService) Create(e *types.Entity, genus string, claims *types.
// TODO: fix this
if err := models.DBH.Insert(payload.Measurement.MeasurementBase); err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
return nil

View file

@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/models"
"github.com/thermokarst/bactdb/payloads"
@ -21,26 +22,26 @@ func (s SpeciesService) Unmarshal(b []byte) (types.Entity, error) {
func (s SpeciesService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
if val == nil {
return nil, helpers.ErrMustProvideOptionsJSON
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
}
var opt helpers.ListOptions
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
species, err := models.ListSpecies(opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strains_opt, err := models.StrainOptsFromSpecies(opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strains, err := models.ListStrains(*strains_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
payload := payloads.ManySpecies{
@ -57,12 +58,12 @@ func (s SpeciesService) List(val *url.Values, claims *types.Claims) (types.Entit
func (s SpeciesService) Get(id int64, genus string, claims *types.Claims) (types.Entity, *types.AppError) {
species, err := models.GetSpecies(id, genus, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strains, err := models.StrainsFromSpeciesId(id, genus, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
payload := payloads.Species{
@ -83,29 +84,29 @@ func (s SpeciesService) Update(id int64, e *types.Entity, genus string, claims *
genus_id, err := models.GenusIdFromName(genus)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Species.SpeciesBase.GenusID = genus_id
// TODO: fix this
count, err := models.DBH.Update(payload.Species.SpeciesBase)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
// TODO: fix this
return types.NewJSONError(models.ErrSpeciesNotUpdated, http.StatusBadRequest)
return NewJSONError(errors.SpeciesNotUpdated, http.StatusBadRequest)
}
// Reload to send back down the wire
species, err := models.GetSpecies(id, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
strains, err := models.StrainsFromSpeciesId(id, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Species = species
@ -124,20 +125,20 @@ func (s SpeciesService) Create(e *types.Entity, genus string, claims *types.Clai
genus_id, err := models.GenusIdFromName(genus)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
payload.Species.SpeciesBase.GenusID = genus_id
// TODO: fix this
err = models.DBH.Insert(payload.Species.SpeciesBase)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
// Reload to send back down the wire
species, err := models.GetSpecies(payload.Species.Id, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
// Note, no strains when new species

View file

@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/models"
"github.com/thermokarst/bactdb/payloads"
@ -21,36 +22,36 @@ func (s StrainService) Unmarshal(b []byte) (types.Entity, error) {
func (s StrainService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
if val == nil {
return nil, helpers.ErrMustProvideOptionsJSON
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
}
var opt helpers.ListOptions
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
strains, err := models.ListStrains(opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
species_opt, err := models.SpeciesOptsFromStrains(opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
species, err := models.ListSpecies(*species_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
characteristics_opt, err := models.CharacteristicsOptsFromStrains(opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
characteristics, err := models.ListCharacteristics(*characteristics_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
characteristic_ids := []int64{}
@ -73,7 +74,7 @@ func (s StrainService) List(val *url.Values, claims *types.Claims) (types.Entity
measurements, err := models.ListMeasurements(measurement_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
payload := payloads.Strains{
@ -92,23 +93,23 @@ func (s StrainService) List(val *url.Values, claims *types.Claims) (types.Entity
func (s StrainService) Get(id int64, genus string, claims *types.Claims) (types.Entity, *types.AppError) {
strain, err := models.GetStrain(id, genus, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
species, err := models.GetSpecies(strain.SpeciesId, genus, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
opt := helpers.ListOptions{Genus: genus, Ids: []int64{id}}
characteristics_opt, err := models.CharacteristicsOptsFromStrains(opt)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
characteristics, err := models.ListCharacteristics(*characteristics_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
characteristic_ids := []int64{}
@ -126,7 +127,7 @@ func (s StrainService) Get(id int64, genus string, claims *types.Claims) (types.
measurements, err := models.ListMeasurements(measurement_opt, claims)
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
var many_species models.ManySpecies = []*models.Species{species}
@ -152,21 +153,21 @@ func (s StrainService) Update(id int64, e *types.Entity, genus string, claims *t
// TODO: fix this
count, err := models.DBH.Update(payload.Strain.StrainBase)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
// TODO: fix this
return types.NewJSONError(models.ErrStrainNotUpdated, http.StatusBadRequest)
return NewJSONError(errors.StrainNotUpdated, http.StatusBadRequest)
}
strain, err := models.GetStrain(id, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
species, err := models.GetSpecies(strain.SpeciesId, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
var many_species models.ManySpecies = []*models.Species{species}
@ -187,17 +188,17 @@ func (s StrainService) Create(e *types.Entity, genus string, claims *types.Claim
// TODO: fix this
if err := models.DBH.Insert(payload.Strain.StrainBase); err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
strain, err := models.GetStrain(payload.Strain.Id, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
species, err := models.GetSpecies(strain.SpeciesId, genus, claims)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
var many_species models.ManySpecies = []*models.Species{species}

View file

@ -2,7 +2,6 @@ package api
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
@ -13,6 +12,7 @@ import (
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/mailgun/mailgun-go"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/golang.org/x/crypto/bcrypt"
"github.com/thermokarst/bactdb/auth"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/models"
"github.com/thermokarst/bactdb/payloads"
@ -20,11 +20,7 @@ import (
)
var (
// TODO: fix this
ErrUserNotFoundJSON = types.NewJSONError(models.ErrUserNotFound, http.StatusNotFound)
ErrUserNotUpdatedJSON = types.NewJSONError(models.ErrUserNotUpdated, http.StatusBadRequest)
ErrEmailAddressTakenJSON = types.NewJSONError(models.ErrEmailAddressTaken, http.StatusBadRequest)
MgAccts = make(map[string]mailgun.Mailgun)
MgAccts = make(map[string]mailgun.Mailgun)
)
type UserService struct{}
@ -37,11 +33,11 @@ func (u UserService) Unmarshal(b []byte) (types.Entity, error) {
func (u UserService) List(val *url.Values, claims *types.Claims) (types.Entity, *types.AppError) {
if val == nil {
return nil, helpers.ErrMustProvideOptionsJSON
return nil, NewJSONError(errors.MustProvideOptions, http.StatusInternalServerError)
}
var opt helpers.ListOptions
if err := helpers.SchemaDecoder.Decode(&opt, *val); err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
// TODO: fix this
@ -52,7 +48,7 @@ func (u UserService) List(val *url.Values, claims *types.Claims) (types.Entity,
WHERE verified IS TRUE
AND deleted_at IS NULL;`
if err := models.DBH.Select(&users, sql); err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
return &users, nil
}
@ -61,7 +57,7 @@ func (u UserService) Get(id int64, dummy string, claims *types.Claims) (types.En
user, err := models.DbGetUserById(id)
user.Password = ""
if err != nil {
return nil, types.NewJSONError(err, http.StatusInternalServerError)
return nil, NewJSONError(err, http.StatusInternalServerError)
}
user.CanEdit = claims.Role == "A" || id == claims.Sub
@ -80,7 +76,7 @@ func (u UserService) Update(id int64, e *types.Entity, dummy string, claims *typ
original_user, err := models.DbGetUserById(id)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
user.Id = id
@ -96,10 +92,10 @@ func (u UserService) Update(id int64, e *types.Entity, dummy string, claims *typ
count, err := models.DBH.Update(user)
user.Password = ""
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
return ErrUserNotUpdatedJSON
return NewJSONError(errors.UserNotUpdated, http.StatusInternalServerError)
}
return nil
@ -115,7 +111,7 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims)
user.UpdatedAt = ct
hash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 12)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
user.Password = string(hash)
user.Role = "R"
@ -125,10 +121,10 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims)
if err := models.DBH.Insert(user); err != nil {
if err, ok := err.(*pq.Error); ok {
if err.Code == "23505" {
return ErrEmailAddressTakenJSON
return NewJSONError(errors.EmailAddressTaken, http.StatusInternalServerError)
}
}
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
user.Password = "password" // don't want to send the hashed PW back to the client
@ -137,12 +133,12 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims)
// TODO: move helpers.GenerateNonce
nonce, err := helpers.GenerateNonce()
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
// TODO: fix this
_, err = models.DBH.Exec(q, user.Id, nonce, claims.Ref, ct)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
// Send out confirmation email
@ -161,7 +157,7 @@ func (u UserService) Create(e *types.Entity, dummy string, claims *types.Claims)
_, _, err := mg.Send(m)
if err != nil {
log.Printf("%+v\n", err)
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
}
@ -179,16 +175,16 @@ func HandleUserVerify(w http.ResponseWriter, r *http.Request) *types.AppError {
}
if err := models.DBH.SelectOne(&ver, q, nonce); err != nil {
log.Print(err)
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
if ver.User_id == 0 {
return types.NewJSONError(errors.New("No user found"), http.StatusInternalServerError)
return NewJSONError(errors.UserNotFound, http.StatusInternalServerError)
}
var user models.User
if err := models.DBH.Get(&user, ver.User_id); err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
user.UpdatedAt = helpers.CurrentTime()
@ -196,16 +192,16 @@ func HandleUserVerify(w http.ResponseWriter, r *http.Request) *types.AppError {
count, err := models.DBH.Update(&user)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
if count != 1 {
return types.NewJSONError(errors.New("Count 0"), http.StatusInternalServerError)
return NewJSONError(errors.UserNotUpdated, http.StatusInternalServerError)
}
q = `DELETE FROM verification WHERE user_id=$1;`
_, err = models.DBH.Exec(q, user.Id)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
fmt.Fprintln(w, `{"msg":"All set! Please log in."}`)
return nil
@ -214,16 +210,16 @@ func HandleUserVerify(w http.ResponseWriter, r *http.Request) *types.AppError {
func HandleUserLockout(w http.ResponseWriter, r *http.Request) *types.AppError {
email := r.FormValue("email")
if email == "" {
return types.NewJSONError(errors.New("missing email"), http.StatusInternalServerError)
return NewJSONError(errors.UserMissingEmail, http.StatusInternalServerError)
}
token, err := auth.Middleware.CreateToken(email)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
origin := r.Header.Get("Origin")
hostUrl, err := url.Parse(origin)
if err != nil {
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
hostUrl.Path += "/users/lockoutauthenticate"
params := url.Values{}
@ -246,7 +242,7 @@ func HandleUserLockout(w http.ResponseWriter, r *http.Request) *types.AppError {
_, _, err := mg.Send(m)
if err != nil {
log.Printf("%+v\n", err)
return types.NewJSONError(err, http.StatusInternalServerError)
return NewJSONError(err, http.StatusInternalServerError)
}
}