Skip to content

Commit

Permalink
improve panic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsl13 committed Jan 31, 2024
1 parent 47e439a commit 1aa505a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (c *Client) all(ctx context.Context) (inside []boundary, err error) {
}

if len(result) != 3 {
panic("database inconsistent")
panic(fmt.Sprintf("database inconsistent: expected 3 result attributes, got %d", len(result)))
}

low := false
Expand Down Expand Up @@ -215,7 +215,7 @@ func (c *Client) all(ctx context.Context) (inside []boundary, err error) {
func (c *Client) vicinity(ctx context.Context, low, high boundary, num int64) (below, inside, above []boundary, err error) {

if num < 0 {
panic("passed num parameter has to be >= 0")
panic(fmt.Sprintf("passed num parameter must be >= 0, got %d", num))
}

below = make([]boundary, 0, num)
Expand Down Expand Up @@ -474,7 +474,7 @@ func (c *Client) Insert(ctx context.Context, ipRange, reason string) error {
}

if len(belowN) == 0 || len(aboveN) == 0 {
panic(ErrDatabaseInconsistent)
panic(fmt.Sprintf("database inconsistent: %d below, %d above", len(belowN), len(aboveN)))
}

// remove inside
Expand Down Expand Up @@ -667,7 +667,7 @@ func (c *Client) Find(ctx context.Context, ip string) (reason string, err error)
if belowNearest.EqualReason(aboveNearest) {
return belowNearest.Reason, nil
}
panic("reasons inconsistent")
panic(fmt.Sprintf("reasons inconsistent: %s != %s", belowNearest.Reason, aboveNearest.Reason))
}

return "", ErrIPNotFound
Expand Down Expand Up @@ -759,7 +759,7 @@ func (c *Client) UpdateReasonOf(ctx context.Context, ip string, fn UpdateFunc) (
aboveNearest.Reason = fn(aboveNearest.Reason)
aboveNearest.Update(ctx, tx)
} else {
panic("database inconsistent")
panic(fmt.Sprintf("database inconsistent: found two lower bounds: %s, %s", found.IP, aboveNearest.IP))
}
} else {
// upperbound
Expand All @@ -772,7 +772,7 @@ func (c *Client) UpdateReasonOf(ctx context.Context, ip string, fn UpdateFunc) (
// upper bound
found.Insert(ctx, tx)
} else {
panic("database inconsistent")
panic(fmt.Sprintf("database inconsistent: found two upper bounds: %s, %s", found.IP, aboveNearest.IP))
}
}

Expand All @@ -794,7 +794,7 @@ func (c *Client) UpdateReasonOf(ctx context.Context, ip string, fn UpdateFunc) (
_, err = tx.Exec(ctx)
return err
}
panic("database reasons inconsistent")
panic(fmt.Sprintf("database reasons inconsistent: %s != %s", belowNearest.Reason, aboveNearest.Reason))
}

return ErrIPNotFound
Expand Down

0 comments on commit 1aa505a

Please sign in to comment.