2
0

Use github.com/pkg/errors

This commit is contained in:
Jack Christensen
2017-06-04 21:30:03 -05:00
parent 3ea41e6972
commit 8f4178b3d3
86 changed files with 639 additions and 597 deletions
+7 -7
View File
@@ -2,8 +2,8 @@ package uuid
import (
"database/sql/driver"
"errors"
"fmt"
"github.com/pkg/errors"
"github.com/jackc/pgx/pgtype"
uuid "github.com/satori/go.uuid"
@@ -24,7 +24,7 @@ func (dst *UUID) Set(src interface{}) error {
*dst = UUID{UUID: uuid.UUID(value), Status: pgtype.Present}
case []byte:
if len(value) != 16 {
return fmt.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value))
return errors.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value))
}
*dst = UUID{Status: pgtype.Present}
copy(dst.UUID[:], value)
@@ -38,7 +38,7 @@ func (dst *UUID) Set(src interface{}) error {
// If all else fails see if pgtype.UUID can handle it. If so, translate through that.
pgUUID := &pgtype.UUID{}
if err := pgUUID.Set(value); err != nil {
return fmt.Errorf("cannot convert %v to UUID", value)
return errors.Errorf("cannot convert %v to UUID", value)
}
*dst = UUID{UUID: uuid.UUID(pgUUID.Bytes), Status: pgUUID.Status}
@@ -83,7 +83,7 @@ func (src *UUID) AssignTo(dst interface{}) error {
return pgtype.NullAssignTo(dst)
}
return fmt.Errorf("cannot assign %v into %T", src, dst)
return errors.Errorf("cannot assign %v into %T", src, dst)
}
func (dst *UUID) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
@@ -108,7 +108,7 @@ func (dst *UUID) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
}
if len(src) != 16 {
return fmt.Errorf("invalid length for UUID: %v", len(src))
return errors.Errorf("invalid length for UUID: %v", len(src))
}
*dst = UUID{Status: pgtype.Present}
@@ -152,7 +152,7 @@ func (dst *UUID) Scan(src interface{}) error {
return dst.DecodeText(nil, src)
}
return fmt.Errorf("cannot scan %T", src)
return errors.Errorf("cannot scan %T", src)
}
// Value implements the database/sql/driver Valuer interface.
+26 -26
View File
@@ -2,10 +2,10 @@ package numeric
import (
"database/sql/driver"
"errors"
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/jackc/pgx/pgtype"
"github.com/shopspring/decimal"
)
@@ -70,17 +70,17 @@ func (dst *Numeric) Set(src interface{}) error {
// If all else fails see if pgtype.Numeric can handle it. If so, translate through that.
num := &pgtype.Numeric{}
if err := num.Set(value); err != nil {
return fmt.Errorf("cannot convert %v to Numeric", value)
return errors.Errorf("cannot convert %v to Numeric", value)
}
buf, err := num.EncodeText(nil, nil)
if err != nil {
return fmt.Errorf("cannot convert %v to Numeric", value)
return errors.Errorf("cannot convert %v to Numeric", value)
}
dec, err := decimal.NewFromString(string(buf))
if err != nil {
return fmt.Errorf("cannot convert %v to Numeric", value)
return errors.Errorf("cannot convert %v to Numeric", value)
}
*dst = Numeric{Decimal: dec, Status: pgtype.Present}
}
@@ -113,92 +113,92 @@ func (src *Numeric) AssignTo(dst interface{}) error {
*v = f
case *int:
if src.Decimal.Exponent() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseInt(src.Decimal.String(), 10, strconv.IntSize)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = int(n)
case *int8:
if src.Decimal.Exponent() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseInt(src.Decimal.String(), 10, 8)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = int8(n)
case *int16:
if src.Decimal.Exponent() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseInt(src.Decimal.String(), 10, 16)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = int16(n)
case *int32:
if src.Decimal.Exponent() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseInt(src.Decimal.String(), 10, 32)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = int32(n)
case *int64:
if src.Decimal.Exponent() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseInt(src.Decimal.String(), 10, 64)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = int64(n)
case *uint:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseUint(src.Decimal.String(), 10, strconv.IntSize)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = uint(n)
case *uint8:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseUint(src.Decimal.String(), 10, 8)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = uint8(n)
case *uint16:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseUint(src.Decimal.String(), 10, 16)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = uint16(n)
case *uint32:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseUint(src.Decimal.String(), 10, 32)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = uint32(n)
case *uint64:
if src.Decimal.Exponent() < 0 || src.Decimal.Sign() < 0 {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
n, err := strconv.ParseUint(src.Decimal.String(), 10, 64)
if err != nil {
return fmt.Errorf("cannot convert %v to %T", dst, *v)
return errors.Errorf("cannot convert %v to %T", dst, *v)
}
*v = uint64(n)
default:
@@ -301,7 +301,7 @@ func (dst *Numeric) Scan(src interface{}) error {
return dst.DecodeText(nil, src)
}
return fmt.Errorf("cannot scan %T", src)
return errors.Errorf("cannot scan %T", src)
}
// Value implements the database/sql/driver Valuer interface.