Skip to content

Commit

Permalink
Wrap errors where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Sep 16, 2024
1 parent 4b1ab86 commit c781a10
Show file tree
Hide file tree
Showing 16 changed files with 26 additions and 26 deletions.
7 changes: 4 additions & 3 deletions internal/datastore/common/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ func (ch *Changes[R, K]) adjustByteSize(item sized, delta int) error {
return spiceerrors.MustBugf("byte size underflow")
}

// We checked for underflow above, so the current byte size
// should fit in a uint64
currentByteSize, _ := safecast.ToUint64(ch.currentByteSize)
currentByteSize, err := safecast.ToUint64(ch.currentByteSize)
if err != nil {
return spiceerrors.MustBugf("could not cast currentByteSize to uint64: %v", err)
}

if currentByteSize > ch.maxByteSize {
return datastore.NewMaximumChangesSizeExceededError(ch.maxByteSize)
Expand Down
5 changes: 1 addition & 4 deletions internal/datastore/common/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

sq "github.com/Masterminds/squirrel"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/ccoveille/go-safecast"
"github.com/jzelinskie/stringz"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -573,9 +572,7 @@ func (tqs QueryExecutor) ExecuteQuery(
return nil, err
}

// A length shouldn't be non-negative, so we can cast without
// checking here.
lenQueryTuples, _ := safecast.ToUint64(len(queryTuples))
lenQueryTuples := uint64(len(queryTuples))
if lenQueryTuples > limit {
queryTuples = queryTuples[:limit]
}
Expand Down
4 changes: 2 additions & 2 deletions internal/datastore/crdb/crdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,11 @@ func (cds *crdbDatastore) ReadyState(ctx context.Context) (datastore.ReadyState,
}
writeTotal, err := safecast.ToUint32(cds.writePool.Stat().TotalConns())
if err != nil {
return datastore.ReadyState{}, spiceerrors.MustBugf("could not cast writeTotal to uint32")
return datastore.ReadyState{}, spiceerrors.MustBugf("could not cast writeTotal to uint32: %v", err)
}
readTotal, err := safecast.ToUint32(cds.readPool.Stat().TotalConns())
if err != nil {
return datastore.ReadyState{}, spiceerrors.MustBugf("could not cast readTotal to uint32")
return datastore.ReadyState{}, spiceerrors.MustBugf("could not cast readTotal to uint32: %v", err)
}
if writeTotal < writeMin || readTotal < readMin {
return datastore.ReadyState{
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/crdb/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (rwt *crdbReadWriteTXN) DeleteRelationships(ctx context.Context, filter *v1
rwt.relCountChange -= modified.RowsAffected()
rowsAffected, err := safecast.ToUint64(modified.RowsAffected())
if err != nil {
return false, spiceerrors.MustBugf("could not cast RowsAffected to uint64")
return false, spiceerrors.MustBugf("could not cast RowsAffected to uint64: %v", err)
}
if delLimit > 0 && rowsAffected == delLimit {
return true, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/crdb/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (cds *crdbDatastore) Statistics(ctx context.Context) (datastore.Stats, erro

uintRowCount, err := safecast.ToUint64(rowCount)
if err != nil {
return spiceerrors.MustBugf("row count was negative")
return spiceerrors.MustBugf("row count was negative: %v", err)
}
estimatedRelCount = uintRowCount
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/mysql/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (mds *Datastore) TxIDBefore(ctx context.Context, before time.Time) (datasto

uintValue, err := safecast.ToUint64(value.Int64)
if err != nil {
return datastore.NoRevision, spiceerrors.MustBugf("value could not be cast to uint64")
return datastore.NoRevision, spiceerrors.MustBugf("value could not be cast to uint64: %v", err)
}

return revisions.NewForTransactionID(uintValue), nil
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/mysql/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (rwt *mysqlReadWriteTXN) DeleteRelationships(ctx context.Context, filter *v

uintRowsAffected, err := safecast.ToUint64(rowsAffected)
if err != nil {
return false, spiceerrors.MustBugf("rowsAffected was negative")
return false, spiceerrors.MustBugf("rowsAffected was negative: %v", err)
}

if delLimit > 0 && uintRowsAffected == delLimit {
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/mysql/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (mds *Datastore) createNewTransaction(ctx context.Context, tx *sql.Tx) (new

uintLastInsertID, err := safecast.ToUint64(lastInsertID)
if err != nil {
return 0, spiceerrors.MustBugf("lastInsertID was negative")
return 0, spiceerrors.MustBugf("lastInsertID was negative: %v", err)
}

return uintLastInsertID, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/mysql/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (mds *Datastore) Statistics(ctx context.Context) (datastore.Stats, error) {

uintCount, err := safecast.ToUint64(count.Int64)
if err != nil {
return datastore.Stats{}, spiceerrors.MustBugf("could not cast count to uint64")
return datastore.Stats{}, spiceerrors.MustBugf("could not cast count to uint64: %v", err)
}

return datastore.Stats{
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/postgres/common/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func BulkLoad(
copied, err := tx.CopyFrom(ctx, pgx.Identifier{tupleTableName}, colNames, adapter)
uintCopied, castErr := safecast.ToUint64(copied)
if castErr != nil {
return 0, spiceerrors.MustBugf("number copied was negative")
return 0, spiceerrors.MustBugf("number copied was negative: %v", castErr)
}
return uintCopied, err
}
2 changes: 1 addition & 1 deletion internal/datastore/postgres/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (rwt *pgReadWriteTXN) deleteRelationshipsWithLimit(ctx context.Context, fil
// validate the limit
intLimit, err := safecast.ToInt64(limit)
if err != nil {
return false, fmt.Errorf("limit argument could not safely be cast to int64")
return false, fmt.Errorf("limit argument could not safely be cast to int64: %w", err)
}

// Construct a select query for the relationships to be removed.
Expand Down
6 changes: 3 additions & 3 deletions internal/datastore/postgres/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,20 @@ func (pr postgresRevision) OptionalNanosTimestamp() (uint64, bool) {
func (pr postgresRevision) MarshalBinary() ([]byte, error) {
xminInt, err := safecast.ToInt64(pr.snapshot.xmin)
if err != nil {
return nil, spiceerrors.MustBugf("could not safely cast snapshot xip to int64")
return nil, spiceerrors.MustBugf("could not safely cast snapshot xip to int64: %v", err)
}
relativeXips := make([]int64, len(pr.snapshot.xipList))
for i, xip := range pr.snapshot.xipList {
intXip, err := safecast.ToInt64(xip)
if err != nil {
return nil, spiceerrors.MustBugf("could not safely cast snapshot xip to int64")
return nil, spiceerrors.MustBugf("could not safely cast snapshot xip to int64: %v", err)
}
relativeXips[i] = intXip - xminInt
}

relativeXmax, err := safecast.ToInt64(pr.snapshot.xmax)
if err != nil {
return nil, spiceerrors.MustBugf("could not safely cast snapshot xmax to int64")
return nil, spiceerrors.MustBugf("could not safely cast snapshot xmax to int64: %v", err)
}
protoRevision := implv1.PostgresRevision{
Xmin: pr.snapshot.xmin,
Expand Down
6 changes: 4 additions & 2 deletions internal/datastore/revisions/hlcrevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ func parseHLCRevisionString(revisionStr string) (datastore.Revision, error) {
return datastore.NoRevision, fmt.Errorf("invalid revision string: %q", revisionStr)
}

// Because we parsed with a bit size of 32 above, we know this range check should pass.
uintLogicalClock, _ := safecast.ToUint32(logicalclock)
uintLogicalClock, err := safecast.ToUint32(logicalclock)
if err != nil {
return datastore.NoRevision, spiceerrors.MustBugf("could not cast logicalclock to uint32: %v", err)
}
return HLCRevision{timestamp, uintLogicalClock + logicalClockOffset}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/spanner/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func deleteWithFilter(ctx context.Context, rwt *spanner.ReadWriteTransaction, fi

uintNumDeleted, err := safecast.ToUint64(numDeleted)
if err != nil {
return false, spiceerrors.MustBugf("numDeleted was negative")
return false, spiceerrors.MustBugf("numDeleted was negative: %v", err)
}

if delLimit > 0 && uintNumDeleted == delLimit {
Expand Down
4 changes: 2 additions & 2 deletions internal/datastore/spanner/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (sd *spannerDatastore) Statistics(ctx context.Context) (datastore.Stats, er

estimatedBytesPerRelationship, err = safecast.ToUint64(totalByteCount / totalRelationships)
if err != nil {
return datastore.Stats{}, spiceerrors.MustBugf("could not cast estimated bytes to uint64")
return datastore.Stats{}, spiceerrors.MustBugf("could not cast estimated bytes to uint64: %v", err)
}
if estimatedBytesPerRelationship > 0 {
sd.cachedEstimatedBytesPerRelationshipLock.Lock()
Expand Down Expand Up @@ -147,7 +147,7 @@ func (sd *spannerDatastore) Statistics(ctx context.Context) (datastore.Stats, er

uintByteEstimate, err := safecast.ToUint64(byteEstimate.Int64)
if err != nil {
return datastore.Stats{}, spiceerrors.MustBugf("unable to cast byteEstimate to uint64")
return datastore.Stats{}, spiceerrors.MustBugf("unable to cast byteEstimate to uint64: %v", err)
}

return datastore.Stats{
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/cursors.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (ls *parallelLimitedIndexedStream[Q]) completedTaskIndex(index int) error {
// Remove the already emitted data from the overall limits.
publishedCount, err := safecast.ToUint32(ls.countingStream.PublishedCount())
if err != nil {
return spiceerrors.MustBugf("cannot cast published count to uint32")
return spiceerrors.MustBugf("cannot cast published count to uint32: %v", err)
}
if err := ls.ci.limits.markAlreadyPublished(publishedCount); err != nil {
return err
Expand Down

0 comments on commit c781a10

Please sign in to comment.