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

@ -2,19 +2,14 @@ package models
import (
"database/sql"
"errors"
"fmt"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/jmoiron/modl"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/types"
)
var (
ErrCharacteristicNotFound = errors.New("Characteristic not found")
ErrCharacteristicNotUpdated = errors.New("Characteristic not updated")
)
func init() {
DB.AddTableWithName(CharacteristicBase{}, "characteristics").SetKeys(true, "Id")
}
@ -199,7 +194,7 @@ func GetCharacteristic(id int64, genus string, claims *types.Claims) (*Character
GROUP BY c.id, ct.characteristic_type_name;`
if err := DBH.SelectOne(&characteristic, q, genus, id); err != nil {
if err == sql.ErrNoRows {
return nil, ErrCharacteristicNotFound
return nil, errors.CharacteristicNotFound
}
return nil, err
}

View file

@ -3,20 +3,14 @@ package models
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/jmoiron/modl"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/types"
)
var (
ErrMeasurementNotFound = errors.New("Measurement not found")
ErrMeasurementNotFoundJSON = types.NewJSONError(ErrMeasurementNotFound, http.StatusNotFound)
)
func init() {
DB.AddTableWithName(MeasurementBase{}, "measurements").SetKeys(true, "Id")
}
@ -205,7 +199,7 @@ func GetMeasurement(id int64, genus string, claims *types.Claims) (*Measurement,
WHERE m.id=$2;`
if err := DBH.SelectOne(&measurement, q, genus, id); err != nil {
if err == sql.ErrNoRows {
return nil, ErrMeasurementNotFound
return nil, errors.MeasurementNotFound
}
return nil, err
}

View file

@ -2,20 +2,15 @@ package models
import (
"database/sql"
"errors"
"fmt"
"strings"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/jmoiron/modl"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/types"
)
var (
ErrSpeciesNotFound = errors.New("Species not found")
ErrSpeciesNotUpdated = errors.New("Species not updated")
)
func init() {
DB.AddTableWithName(SpeciesBase{}, "species").SetKeys(true, "Id")
}
@ -163,7 +158,7 @@ func GetSpecies(id int64, genus string, claims *types.Claims) (*Species, error)
GROUP BY sp.id, g.genus_name;`
if err := DBH.SelectOne(&species, q, genus, id); err != nil {
if err == sql.ErrNoRows {
return nil, ErrSpeciesNotFound
return nil, errors.SpeciesNotFound
}
return nil, err
}

View file

@ -2,20 +2,15 @@ package models
import (
"database/sql"
"errors"
"fmt"
"strings"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/jmoiron/modl"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/types"
)
var (
ErrStrainNotFound = errors.New("Strain not found")
ErrStrainNotUpdated = errors.New("Strain not updated")
)
func init() {
DB.AddTableWithName(StrainBase{}, "strains").SetKeys(true, "Id")
}
@ -126,7 +121,7 @@ func GetStrain(id int64, genus string, claims *types.Claims) (*Strain, error) {
GROUP BY st.id;`
if err := DBH.SelectOne(&strain, q, genus, id); err != nil {
if err == sql.ErrNoRows {
return nil, ErrStrainNotFound
return nil, errors.StrainNotFound
}
return nil, err
}

View file

@ -3,21 +3,14 @@ package models
import (
"database/sql"
"encoding/json"
"errors"
"regexp"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/golang.org/x/crypto/bcrypt"
"github.com/thermokarst/bactdb/errors"
"github.com/thermokarst/bactdb/helpers"
"github.com/thermokarst/bactdb/types"
)
var (
ErrUserNotFound = errors.New("User not found")
ErrUserNotUpdated = errors.New("User not updated")
ErrInvalidEmailOrPassword = errors.New("Invalid email or password")
ErrEmailAddressTaken = errors.New("Email address already registered")
)
func init() {
DB.AddTableWithName(UserBase{}, "users").SetKeys(true, "Id")
}
@ -114,10 +107,10 @@ func DbAuthenticate(email string, password string) error {
AND verified IS TRUE
AND deleted_at IS NULL;`
if err := DBH.SelectOne(&user, q, email); err != nil {
return ErrInvalidEmailOrPassword
return errors.InvalidEmailOrPassword
}
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
return ErrInvalidEmailOrPassword
return errors.InvalidEmailOrPassword
}
return nil
}
@ -131,7 +124,7 @@ func DbGetUserById(id int64) (*User, error) {
AND deleted_at IS NULL;`
if err := DBH.SelectOne(&user, q, id); err != nil {
if err == sql.ErrNoRows {
return nil, ErrUserNotFound
return nil, errors.UserNotFound
}
return nil, err
}
@ -148,7 +141,7 @@ func DbGetUserByEmail(email string) (*User, error) {
AND deleted_at IS NULL;`
if err := DBH.SelectOne(&user, q, email); err != nil {
if err == sql.ErrNoRows {
return nil, ErrUserNotFound
return nil, errors.UserNotFound
}
return nil, err
}