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

refactor: Remove all errors.Wrap and update them with fmt.Errorf. #41

Merged
merged 4 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/crdt/lwwreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// "time"

"bytes"
"fmt"

"github.com/pkg/errors"

Expand Down Expand Up @@ -135,7 +136,7 @@ func (reg LWWRegister) Merge(delta core.Delta, id string) error {
func (reg LWWRegister) setValue(val []byte, priority uint64) error {
curPrio, err := reg.getPriority(reg.key)
if err != nil {
return errors.Wrap(err, "Failed to get priority for Set")
return fmt.Errorf("Failed to get priority for Set : %w", err)
}

// if the current priority is higher ignore put
Expand All @@ -155,7 +156,7 @@ func (reg LWWRegister) setValue(val []byte, priority uint64) error {
buf := append([]byte{byte(core.LWW_REGISTER)}, val...)
err = reg.store.Put(valueK, buf)
if err != nil {
return errors.Wrap(err, "Failed to store new value")
return fmt.Errorf("Failed to store new value : %w", err)
}

return reg.setPriority(reg.key, priority)
Expand Down
4 changes: 2 additions & 2 deletions core/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
package core

import (
"fmt"
"strconv"

ds "github.com/ipfs/go-datastore"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -62,7 +62,7 @@ func (k Key) FieldID() (uint32, error) {
fieldIDStr := k.Type()
fieldID, err := strconv.Atoi(fieldIDStr)
if err != nil {
return 0, errors.Wrap(err, "Failed to get FieldID of Key")
return 0, fmt.Errorf("Failed to get FieldID of Key: %w", err)
}
return uint32(fieldID), nil
}
Expand Down
2 changes: 1 addition & 1 deletion db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (c *Collection) create(txn *Txn, doc *document.Document) error {
// fmt.Println(c)
dockey := key.NewDocKeyV0(doccid)
if !dockey.Key.Equal(doc.Key().Key) {
return errors.Wrap(ErrDocVerification, fmt.Sprintf("Expected %s, got %s", doc.Key().UUID(), dockey.UUID()))
return fmt.Errorf("Expected %s, got %s : %w", doc.Key().UUID(), dockey.UUID(), ErrDocVerification)
}

// check if doc already exists
Expand Down
4 changes: 2 additions & 2 deletions db/fetcher/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ func (hh *heads) List() ([]cid.Cid, uint64, error) {
var maxHeight uint64
for r := range results.Next() {
if r.Error != nil {
return nil, 0, errors.Wrap(r.Error, "Failed to get next query result")
return nil, 0, fmt.Errorf("Failed to get next query result : %w", err)
}
// fmt.Println(r.Key, hh.namespace.String())
headKey := ds.NewKey(strings.TrimPrefix(r.Key, hh.namespace.String()))
headCid, err := dshelp.DsKeyToCid(headKey)
if err != nil {
return nil, 0, errors.Wrap(err, "Failed to get CID from key")
return nil, 0, fmt.Errorf("Failed to get CID from key : %w", err)
}
height, n := binary.Uvarint(r.Value)
if n <= 0 {
Expand Down
24 changes: 12 additions & 12 deletions merkle/clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package clock

import (
"github.com/pkg/errors"
"fmt"

"github.com/sourcenetwork/defradb/core"

Expand Down Expand Up @@ -52,7 +52,7 @@ func (mc *MerkleClock) putBlock(heads []cid.Cid, height uint64, delta core.Delta

node, err := makeNode(delta, heads)
if err != nil {
return nil, errors.Wrap(err, "error creating block")
return nil, fmt.Errorf("error creating block : %w", err)
}

// @todo Add a DagSyncer instance to the MerkleCRDT structure
Expand All @@ -63,11 +63,11 @@ func (mc *MerkleClock) putBlock(heads []cid.Cid, height uint64, delta core.Delta
// ctx := context.Background()
// err = mc.store.dagSyncer.Add(ctx, node)
// if err != nil {
// return nil, errors.Wrapf(err, "error writing new block %s", node.Cid())
// return nil, fmt.Errorf("error writing new block %s : %w", node.Cid(), err)
// }
err = mc.dagstore.Put(node)
if err != nil {
return nil, errors.Wrapf(err, "error writing new block %s", node.Cid())
return nil, fmt.Errorf("error writing new block %s : %w", node.Cid(), err)
}

return node, nil
Expand All @@ -81,7 +81,7 @@ func (mc *MerkleClock) putBlock(heads []cid.Cid, height uint64, delta core.Delta
func (mc *MerkleClock) AddDAGNode(delta core.Delta) (cid.Cid, error) {
heads, height, err := mc.headset.List()
if err != nil {
return cid.Undef, errors.Wrap(err, "error getting heads")
return cid.Undef, fmt.Errorf("error getting heads : %w", err)
}
height = height + 1

Expand All @@ -90,7 +90,7 @@ func (mc *MerkleClock) AddDAGNode(delta core.Delta) (cid.Cid, error) {
// write the delta and heads to a new block
nd, err := mc.putBlock(heads, height, delta)
if err != nil {
return cid.Undef, errors.Wrap(err, "Error adding block")
return cid.Undef, fmt.Errorf("Error adding block : %w", err)
}

// apply the new node and merge the delta with state
Expand All @@ -104,7 +104,7 @@ func (mc *MerkleClock) AddDAGNode(delta core.Delta) (cid.Cid, error) {
)

if err != nil {
return cid.Undef, errors.Wrap(err, "error processing new block")
return cid.Undef, fmt.Errorf("error processing new block : %w", err)
}
return nd.Cid(), nil
}
Expand All @@ -115,7 +115,7 @@ func (mc *MerkleClock) ProcessNode(ng core.NodeGetter, root cid.Cid, rootPrio ui
current := node.Cid()
err := mc.crdt.Merge(delta, dshelp.CidToDsKey(current).String())
if err != nil {
return nil, errors.Wrapf(err, "error merging delta from %s", current)
return nil, fmt.Errorf("error merging delta from %s : %w", current, err)
}

// if prio := delta.GetPriority(); prio%10 == 0 {
Expand All @@ -136,7 +136,7 @@ func (mc *MerkleClock) ProcessNode(ng core.NodeGetter, root cid.Cid, rootPrio ui
if !hasHeads { // reached the bottom, at a leaf
err := mc.headset.Add(root, rootPrio)
if err != nil {
return nil, errors.Wrapf(err, "error adding head %s", root)
return nil, fmt.Errorf("error adding head %s : %w", root, err)
}
return nil, nil
}
Expand All @@ -147,23 +147,23 @@ func (mc *MerkleClock) ProcessNode(ng core.NodeGetter, root cid.Cid, rootPrio ui
child := l.Cid
isHead, _, err := mc.headset.IsHead(child)
if err != nil {
return nil, errors.Wrapf(err, "error checking if %s is head", child)
return nil, fmt.Errorf("error checking if %s is head : %w", child, err)
}

if isHead {
// reached one of the current heads, replace it with the tip
// of current branch
err := mc.headset.Replace(child, root, rootPrio)
if err != nil {
return nil, errors.Wrapf(err, "error replacing head: %s->%s", child, root)
return nil, fmt.Errorf("error replacing head: %s->%s : %w", child, root, err)
}

continue
}

known, err := mc.dagstore.Has(child)
if err != nil {
return nil, errors.Wrapf(err, "error checking for know block %s", child)
return nil, fmt.Errorf("error checking for know block %s : %w", child, err)
}
if known {
// we reached a non-head node in the known tree.
Expand Down
5 changes: 3 additions & 2 deletions merkle/clock/heads.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package clock
import (
"bytes"
"encoding/binary"
"fmt"
"sort"
"strings"

Expand Down Expand Up @@ -141,13 +142,13 @@ func (hh *heads) List() ([]cid.Cid, uint64, error) {
var maxHeight uint64
for r := range results.Next() {
if r.Error != nil {
return nil, 0, errors.Wrap(r.Error, "Failed to get next query result")
return nil, 0, fmt.Errorf("Failed to get next query result : %w", r.Error)
}
// fmt.Println(r.Key, hh.namespace.String())
headKey := ds.NewKey(strings.TrimPrefix(r.Key, hh.namespace.String()))
headCid, err := dshelp.DsKeyToCid(headKey)
if err != nil {
return nil, 0, errors.Wrap(err, "Failed to get CID from key")
return nil, 0, fmt.Errorf("Failed to get CID from key : %w", err)
}
height, n := binary.Uvarint(r.Value)
if n <= 0 {
Expand Down
2 changes: 1 addition & 1 deletion merkle/crdt/merklecrdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (base *baseMerkleCRDT) Value() ([]byte, error) {
// current := node.Cid()
// err := base.Merge(delta, dshelp.CidToDsKey(current).String())
// if err != nil {
// return nil, errors.Wrapf(eff, "error merging delta from %s", current)
// return nil, fmt.Errorf("error merging delta from %s : %w", current, err)
// }

// return base.clock.ProcessNode(ng, root, rootPrio, delta, node)
Expand Down
8 changes: 4 additions & 4 deletions query/graphql/schema/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,14 @@ func runTestConfigForbuildTypesFromASTSuite(t *testing.T, schema string, typeDef
// Source: source,
// })
// if err != nil {
// return errors.Wrap(err, "Failed to parse schema string")
// return fmt.Errorf("Failed to parse schema string : %w", err)
// }
// // assert.NoError(t, err, "Failed to parse schema string")

// err = g.buildTypesFromAST(doc)
_, _, err := g.FromSDL(schema)
if err != nil {
return errors.Wrap(err, "Failed to build types from AST")
return fmt.Errorf("Failed to build types from AST : %w", err)
}
// assert.NoError(t, err, "Failed to build types from AST")

Expand All @@ -497,7 +497,7 @@ func runTestConfigForbuildTypesFromASTSuite(t *testing.T, schema string, typeDef
return errors.New(fmt.Sprintf("%s type doesn't exist in the schema manager TypeMap", objName))
}
if myObject.Error() != nil {
return errors.Wrapf(myObject.Error(), "%s contains an internal error", objName)
return fmt.Errorf("%s contains an internal error : %w", objName, myObject.Error())
}
if !reflect.DeepEqual(myObject, g.typeDefs[i]) {
// add the assert here for its object diff output
Expand All @@ -511,7 +511,7 @@ func runTestConfigForbuildTypesFromASTSuite(t *testing.T, schema string, typeDef
// to resolve the FieldsThunker

if myObject.Error() != nil {
return errors.Wrap(myObject.Error(), fmt.Sprintf("%s contains an internal error from the Fields() > definFields() call", objName))
return fmt.Errorf("%s contains an internal error from the Fields() > definFields() call : %w", objName, myObject.Error())
}
// assert.NoErrorf(t, myObjectActual.Error(), "%s contains an internal error from the defineFields() call", objName)

Expand Down