Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/types: cleanup error.Trace #3113

Merged
merged 2 commits into from
Apr 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions util/types/overflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// AddUint64 adds uint64 a and b if no overflow, else returns error.
func AddUint64(a uint64, b uint64) (uint64, error) {
if math.MaxUint64-a < b {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b))
}
return a + b, nil
}
Expand All @@ -32,7 +32,7 @@ func AddUint64(a uint64, b uint64) (uint64, error) {
func AddInt64(a int64, b int64) (int64, error) {
if (a > 0 && b > 0 && math.MaxInt64-a < b) ||
(a < 0 && b < 0 && math.MinInt64-a > b) {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b))
}

return a + b, nil
Expand All @@ -45,15 +45,15 @@ func AddInteger(a uint64, b int64) (uint64, error) {
}

if uint64(-b) > a {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b))
}
return a - uint64(-b), nil
}

// SubUint64 subtracts uint64 a with b and returns uint64 if no overflow error.
func SubUint64(a uint64, b uint64) (uint64, error) {
if a < b {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b))
}
return a - b, nil
}
Expand All @@ -63,7 +63,7 @@ func SubInt64(a int64, b int64) (int64, error) {
if (a > 0 && b < 0 && math.MaxInt64-a < -b) ||
(a < 0 && b > 0 && math.MinInt64-a > -b) ||
(a == 0 && b == math.MinInt64) {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b))
}
return a - b, nil
}
Expand All @@ -79,15 +79,15 @@ func SubUintWithInt(a uint64, b int64) (uint64, error) {
// SubIntWithUint subtracts int64 a with uint64 b and returns uint64 if no overflow error.
func SubIntWithUint(a int64, b uint64) (uint64, error) {
if a < 0 || uint64(a) < b {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b))
}
return uint64(a) - b, nil
}

// MulUint64 multiplies uint64 a and b and returns uint64 if no overflow error.
func MulUint64(a uint64, b uint64) (uint64, error) {
if b > 0 && a > math.MaxUint64/b {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b))
}
return a * b, nil
}
Expand Down Expand Up @@ -123,15 +123,15 @@ func MulInt64(a int64, b int64) (int64, error) {
if negative {
// negative result
if res > math.MaxInt64+1 {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b))
}

return -int64(res), nil
}

// positive result
if res > math.MaxInt64 {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b))
}

return int64(res), nil
Expand All @@ -144,7 +144,7 @@ func MulInteger(a uint64, b int64) (uint64, error) {
}

if b < 0 {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b))
}

return MulUint64(a, uint64(b))
Expand All @@ -154,7 +154,7 @@ func MulInteger(a uint64, b int64) (uint64, error) {
// It just checks overflow, if b is zero, a "divide by zero" panic throws.
func DivInt64(a int64, b int64) (int64, error) {
if a == math.MinInt64 && b == -1 {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b))
}

return a / b, nil
Expand All @@ -165,7 +165,7 @@ func DivInt64(a int64, b int64) (int64, error) {
func DivUintWithInt(a uint64, b int64) (uint64, error) {
if b < 0 {
if a != 0 && uint64(-b) <= a {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%d, %d)", a, b))
}

return 0, nil
Expand All @@ -179,7 +179,7 @@ func DivUintWithInt(a uint64, b int64) (uint64, error) {
func DivIntWithUint(a int64, b uint64) (uint64, error) {
if a < 0 {
if uint64(-a) >= b {
return 0, errors.Trace(ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b)))
return 0, ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%d, %d)", a, b))
}

return 0, nil
Expand Down