From d8b2b24972ac06349c6d444a4af28b257e537b73 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Fri, 31 May 2019 11:58:22 -0700 Subject: [PATCH] Remove DebugMode option. (#3441) This alpha option is not doing too much so it's being removed and replaced with calls to glog.V(2) where appropriate. Since the change made x.Errorf and x.Wrapf just wrappers of the existing methods in the errors package, this PR also removes these methods and replaces them with the methods in the errors package. --- chunker/json/parse.go | 17 +++---- chunker/rdf/parse.go | 50 ++++++++++---------- compose/compose.go | 3 +- conn/node.go | 17 +++---- conn/raft_server.go | 5 +- dgraph/cmd/alpha/run.go | 3 -- dgraph/cmd/cert/create.go | 6 +-- dgraph/cmd/cert/info.go | 4 +- dgraph/cmd/root.go | 3 +- dgraph/cmd/zero/assign.go | 11 ++--- dgraph/cmd/zero/oracle.go | 14 +++--- dgraph/cmd/zero/raft.go | 18 ++++---- dgraph/cmd/zero/tablet.go | 15 +++--- dgraph/cmd/zero/zero.go | 38 ++++++++-------- edgraph/server.go | 33 +++++++------- ee/acl/utils.go | 5 +- ee/backup/file_handler.go | 15 +++--- ee/backup/handler.go | 10 ++-- ee/backup/run.go | 7 +-- ee/backup/s3_handler.go | 25 +++++----- gql/math.go | 33 +++++++------- gql/mutation.go | 8 ++-- gql/parser.go | 55 +++++++++++----------- gql/parser_mutation.go | 22 ++++----- lex/lexer.go | 10 ++-- posting/index.go | 7 +-- posting/list.go | 4 +- posting/mvcc.go | 7 +-- query/aggregator.go | 39 ++++++++-------- query/groupby.go | 8 ++-- query/math.go | 18 ++++---- query/mutation.go | 10 ++-- query/outputnode.go | 9 ++-- query/query.go | 64 +++++++++++++------------- query/recurse.go | 10 ++-- query/shortest.go | 13 +++--- raftwal/storage.go | 15 +++--- schema/parse.go | 21 +++++---- schema/schema.go | 5 +- systest/cluster_test.go | 11 +++-- tok/tok.go | 9 ++-- tok/tokens.go | 8 ++-- types/conversion.go | 41 ++++++++--------- types/facets/utils.go | 8 ++-- types/geofilter.go | 29 ++++++------ types/geofilter_test.go | 4 +- types/password.go | 6 +-- types/s2.go | 23 +++++----- types/s2index.go | 9 ++-- types/sort.go | 9 ++-- worker/backup_ee.go | 7 +-- worker/draft.go | 9 ++-- worker/export.go | 5 +- worker/groups.go | 5 +- worker/mutation.go | 48 ++++++++++---------- worker/predicate_move.go | 15 +++--- worker/predicate_test.go | 6 +-- worker/proposal.go | 6 +-- worker/schema.go | 3 +- worker/sort.go | 37 ++++++++------- worker/task.go | 96 +++++++++++++++++++-------------------- worker/tokens.go | 7 +-- x/config.go | 1 - x/error.go | 32 ++----------- x/keys.go | 24 +++------- 65 files changed, 553 insertions(+), 562 deletions(-) diff --git a/chunker/json/parse.go b/chunker/json/parse.go index 590f9e64ad6..c71f5f70dae 100644 --- a/chunker/json/parse.go +++ b/chunker/json/parse.go @@ -28,6 +28,7 @@ import ( "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/types/facets" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" geom "github.com/twpayne/go-geom" "github.com/twpayne/go-geom/encoding/geojson" ) @@ -87,7 +88,7 @@ func parseFacets(m map[string]interface{}, prefix string) ([]*api.Facet, error) jsonValue = v valueType = api.Facet_BOOL default: - return nil, x.Errorf("Facet value for key: %s can only be string/float64/bool.", + return nil, errors.Errorf("Facet value for key: %s can only be string/float64/bool.", fname) } @@ -153,7 +154,7 @@ func handleBasicType(k string, v interface{}, op int, nq *api.NQuad) error { nq.ObjectValue = &api.Value{Val: &api.Value_BoolVal{BoolVal: v}} default: - return x.Errorf("Unexpected type for val for attr: %s while converting to nquad", k) + return errors.Errorf("Unexpected type for val for attr: %s while converting to nquad", k) } return nil @@ -176,7 +177,7 @@ func handleGeoType(val map[string]interface{}, nq *api.NQuad) (bool, error) { if len(val) == 2 && hasType && hasCoordinates { b, err := json.Marshal(val) if err != nil { - return false, x.Errorf("Error while trying to parse value: %+v as geo val", val) + return false, errors.Errorf("Error while trying to parse value: %+v as geo val", val) } ok, err := tryParseAsGeo(b, nq) if err != nil && ok { @@ -195,7 +196,7 @@ func tryParseAsGeo(b []byte, nq *api.NQuad) (bool, error) { if err == nil { geo, err := types.ObjectValue(types.GeoID, g) if err != nil { - return false, x.Errorf("Couldn't convert value: %s to geo type", string(b)) + return false, errors.Errorf("Couldn't convert value: %s to geo type", string(b)) } nq.ObjectValue = geo @@ -238,7 +239,7 @@ func mapToNquads(m map[string]interface{}, idx *int, op int, parentPred string) if len(mr.uid) == 0 { if op == DeleteNquads { // Delete operations with a non-nil value must have a uid specified. - return mr, x.Errorf("UID must be present and non-zero while deleting edges.") + return mr, errors.Errorf("UID must be present and non-zero while deleting edges.") } mr.uid = fmt.Sprintf("_:blank-%d", *idx) @@ -358,11 +359,11 @@ func mapToNquads(m map[string]interface{}, idx *int, op int, parentPred string) mr.nquads = append(mr.nquads, cr.nquads...) default: return mr, - x.Errorf("Got unsupported type for list: %s", pred) + errors.Errorf("Got unsupported type for list: %s", pred) } } default: - return mr, x.Errorf("Unexpected type for val for attr: %s while converting to nquad", pred) + return mr, errors.Errorf("Unexpected type for val for attr: %s while converting to nquad", pred) } } @@ -401,7 +402,7 @@ func Parse(b []byte, op int) ([]*api.NQuad, error) { if len(list) > 0 { for _, obj := range list { if _, ok := obj.(map[string]interface{}); !ok { - return nil, x.Errorf("Only array of map allowed at root.") + return nil, errors.Errorf("Only array of map allowed at root.") } mr, err := mapToNquads(obj.(map[string]interface{}), &idx, op, "") if err != nil { diff --git a/chunker/rdf/parse.go b/chunker/rdf/parse.go index 2f99ff32947..788608a53bb 100644 --- a/chunker/rdf/parse.go +++ b/chunker/rdf/parse.go @@ -17,7 +17,6 @@ package rdf import ( - "errors" "strconv" "strings" "unicode" @@ -27,6 +26,7 @@ import ( "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/types/facets" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) var ( @@ -80,11 +80,11 @@ L: case itemVarKeyword: it.Next() if item = it.Item(); item.Typ != itemLeftRound { - return rnq, x.Errorf("Expected '(', found: %s", item.Val) + return rnq, errors.Errorf("Expected '(', found: %s", item.Val) } it.Next() if item = it.Item(); item.Typ != itemVarName { - return rnq, x.Errorf("Expected variable name, found: %s", item.Val) + return rnq, errors.Errorf("Expected variable name, found: %s", item.Val) } it.Next() // parse ')' @@ -109,7 +109,7 @@ L: var err error oval, err = strconv.Unquote(item.Val) if err != nil { - return rnq, x.Wrapf(err, "while unquoting") + return rnq, errors.Wrapf(err, "while unquoting") } seenOval = true @@ -118,21 +118,21 @@ L: case itemObjectType: if rnq.Predicate == x.Star || rnq.Subject == x.Star { - return rnq, x.Errorf("If predicate/subject is *, value should be * as well") + return rnq, errors.Errorf("If predicate/subject is *, value should be * as well") } val := strings.Trim(item.Val, " ") // TODO: Check if this condition is required. if strings.Trim(val, " ") == "*" { - return rnq, x.Errorf("itemObject can't be *") + return rnq, errors.Errorf("itemObject can't be *") } // Lets find out the storage type from the type map. t, ok := typeMap[val] if !ok { - return rnq, x.Errorf("Unrecognized rdf type %s", val) + return rnq, errors.Errorf("Unrecognized rdf type %s", val) } if oval == "" && t != types.StringID { - return rnq, x.Errorf("Invalid ObjectValue") + return rnq, errors.Errorf("Invalid ObjectValue") } src := types.ValueForType(types.StringID) src.Value = []byte(oval) @@ -155,13 +155,13 @@ L: case itemValidEnd: vend = true if !it.Next() { - return rnq, x.Errorf("Invalid end of input. Input: [%s]", line) + return rnq, errors.Errorf("Invalid end of input. Input: [%s]", line) } // RDF spec says N-Quads should be terminated with a newline. Since we break the input // by newline already. We should get EOF or # after dot(.) item = it.Item() if !(item.Typ == lex.ItemEOF || item.Typ == itemComment) { - return rnq, x.Errorf("Invalid end of input. Expected newline or # after ."+ + return rnq, errors.Errorf("Invalid end of input. Expected newline or # after ."+ " Input: [%s]", line) } break L @@ -172,13 +172,13 @@ L: case itemLeftRound: it.Prev() // backup '(' if err := parseFacets(it, &rnq); err != nil { - return rnq, x.Errorf(err.Error()) + return rnq, errors.Wrap(err, "could not parse facet") } } } if !vend { - return rnq, x.Errorf("Invalid end of input. Input: [%s]", line) + return rnq, errors.Errorf("Invalid end of input. Input: [%s]", line) } if isCommentLine { return rnq, ErrEmpty @@ -189,14 +189,14 @@ L: rnq.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{DefaultVal: oval}} } if len(rnq.Subject) == 0 || len(rnq.Predicate) == 0 { - return rnq, x.Errorf("Empty required fields in NQuad. Input: [%s]", line) + return rnq, errors.Errorf("Empty required fields in NQuad. Input: [%s]", line) } if len(rnq.ObjectId) == 0 && rnq.ObjectValue == nil { - return rnq, x.Errorf("No Object in NQuad. Input: [%s]", line) + return rnq, errors.Errorf("No Object in NQuad. Input: [%s]", line) } if !sane(rnq.Subject) || !sane(rnq.Predicate) || !sane(rnq.ObjectId) || !sane(rnq.Label) { - return rnq, x.Errorf("NQuad failed sanity check:%+v", rnq) + return rnq, errors.Errorf("NQuad failed sanity check:%+v", rnq) } return rnq, nil @@ -204,34 +204,34 @@ L: func parseFacets(it *lex.ItemIterator, rnq *api.NQuad) error { if !it.Next() { - return x.Errorf("Unexpected end of facets.") + return errors.Errorf("Unexpected end of facets.") } item := it.Item() if item.Typ != itemLeftRound { - return x.Errorf("Expected '(' but found %v at Facet.", item.Val) + return errors.Errorf("Expected '(' but found %v at Facet.", item.Val) } for it.Next() { // parse one key value pair // parse key item = it.Item() if item.Typ != itemText { - return x.Errorf("Expected key but found %v.", item.Val) + return errors.Errorf("Expected key but found %v.", item.Val) } facetKey := strings.TrimSpace(item.Val) if len(facetKey) == 0 { - return x.Errorf("Empty facetKeys not allowed.") + return errors.Errorf("Empty facetKeys not allowed.") } // parse = if !it.Next() { - return x.Errorf("Unexpected end of facets.") + return errors.Errorf("Unexpected end of facets.") } item = it.Item() if item.Typ != itemEqual { - return x.Errorf("Expected = after facetKey. Found %v", item.Val) + return errors.Errorf("Expected = after facetKey. Found %v", item.Val) } // parse value or empty value if !it.Next() { - return x.Errorf("Unexpected end of facets.") + return errors.Errorf("Unexpected end of facets.") } item = it.Item() facetVal := "" @@ -252,11 +252,11 @@ func parseFacets(it *lex.ItemIterator, rnq *api.NQuad) error { continue } if item.Typ != itemText { - return x.Errorf("Expected , or ) or text but found %s", item.Val) + return errors.Errorf("Expected , or ) or text but found %s", item.Val) } // value was present.. if !it.Next() { // get either ')' or ',' - return x.Errorf("Unexpected end of facets.") + return errors.Errorf("Unexpected end of facets.") } item = it.Item() if item.Typ == itemRightRound { @@ -265,7 +265,7 @@ func parseFacets(it *lex.ItemIterator, rnq *api.NQuad) error { if item.Typ == itemComma { continue } - return x.Errorf("Expected , or ) after facet. Received %s", item.Val) + return errors.Errorf("Expected , or ) after facet. Received %s", item.Val) } return nil diff --git a/compose/compose.go b/compose/compose.go index a12e207defb..c85fbf12c8e 100644 --- a/compose/compose.go +++ b/compose/compose.go @@ -21,6 +21,7 @@ import ( "os" "os/user" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" "gopkg.in/yaml.v2" @@ -150,7 +151,7 @@ func initService(basename string, idx, grpcPort int) Service { if opts.UserOwnership { user, err := user.Current() if err != nil { - x.CheckfNoTrace(x.Errorf("unable to get current user: %v", err)) + x.CheckfNoTrace(errors.Wrap(err, "unable to get current user")) } svc.User = fmt.Sprintf("${UID:-%s}", user.Uid) svc.WorkingDir = fmt.Sprintf("/working/%s", svc.name) diff --git a/conn/node.go b/conn/node.go index cb7d7aafbf2..e58f22b5d74 100644 --- a/conn/node.go +++ b/conn/node.go @@ -19,7 +19,6 @@ package conn import ( "bytes" "encoding/binary" - "errors" "fmt" "math/rand" "strings" @@ -33,6 +32,7 @@ import ( "github.com/dgraph-io/dgraph/raftwal" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" "go.etcd.io/etcd/raft" "go.etcd.io/etcd/raft/raftpb" otrace "go.opencensus.io/trace" @@ -41,7 +41,7 @@ import ( var ( // ErrNoNode is returned when no node has been set up. - ErrNoNode = x.Errorf("No node has been set up yet") + ErrNoNode = errors.Errorf("No node has been set up yet") ) // Node represents a node participating in the RAFT protocol. @@ -425,7 +425,7 @@ func (n *Node) streamMessages(to uint64, s *stream) { func (n *Node) doSendMessage(to uint64, msgCh chan []byte) error { addr, has := n.Peer(to) if !has { - return x.Errorf("Do not have address of peer %#x", to) + return errors.Errorf("Do not have address of peer %#x", to) } pool, err := GetPools().Get(addr) if err != nil { @@ -591,7 +591,7 @@ func (n *Node) ProposePeerRemoval(ctx context.Context, id uint64) error { return ErrNoNode } if _, ok := n.Peer(id); !ok && id != n.RaftContext.Id { - return x.Errorf("Node %#x not part of group", id) + return errors.Errorf("Node %#x not part of group", id) } cc := raftpb.ConfChange{ Type: raftpb.ConfChangeRemoveNode, @@ -609,7 +609,8 @@ type linReadReq struct { indexCh chan<- uint64 } -var errReadIndex = x.Errorf("Cannot get linearized read (time expired or no configured leader)") +var errReadIndex = errors.Errorf( + "Cannot get linearized read (time expired or no configured leader)") // WaitLinearizableRead waits until a linearizable read can be performed. func (n *Node) WaitLinearizableRead(ctx context.Context) error { @@ -722,18 +723,18 @@ func (n *Node) joinCluster(ctx context.Context, rc *pb.RaftContext) (*api.Payloa // Check that the new node is from the same group as me. if rc.Group != n.RaftContext.Group { - return nil, x.Errorf("Raft group mismatch") + return nil, errors.Errorf("Raft group mismatch") } // Also check that the new node is not me. if rc.Id == n.RaftContext.Id { - return nil, x.Errorf("REUSE_RAFTID: Raft ID duplicates mine: %+v", rc) + return nil, errors.Errorf("REUSE_RAFTID: Raft ID duplicates mine: %+v", rc) } // Check that the new node is not already part of the group. if addr, ok := n.Peer(rc.Id); ok && rc.Addr != addr { // There exists a healthy connection to server with same id. if _, err := GetPools().Get(addr); err == nil { - return &api.Payload{}, x.Errorf( + return &api.Payload{}, errors.Errorf( "REUSE_ADDR: IP Address same as existing peer: %s", addr) } } diff --git a/conn/raft_server.go b/conn/raft_server.go index 28c53268044..1ee33ffdb25 100644 --- a/conn/raft_server.go +++ b/conn/raft_server.go @@ -28,6 +28,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" "go.etcd.io/etcd/raft/raftpb" otrace "go.opencensus.io/trace" ) @@ -211,7 +212,7 @@ func (w *RaftServer) RaftMessage(server pb.Raft_RaftMessageServer) error { idx += 4 msg := raftpb.Message{} if idx+sz > len(data) { - return x.Errorf( + return errors.Errorf( "Invalid query. Specified size %v overflows slice [%v,%v)\n", sz, idx, len(data)) } @@ -236,7 +237,7 @@ func (w *RaftServer) RaftMessage(server pb.Raft_RaftMessageServer) error { if err := raft.Step(ctx, msg); err != nil { glog.Warningf("Error while raft.Step from %#x: %v. Closing RaftMessage stream.", rc.GetId(), err) - return x.Errorf("Error while raft.Step from %#x: %v", rc.GetId(), err) + return errors.Wrapf(err, "error while raft.Step from %#x", rc.GetId()) } idx += sz } diff --git a/dgraph/cmd/alpha/run.go b/dgraph/cmd/alpha/run.go index 5c943ec8ef0..7cf10ac5737 100644 --- a/dgraph/cmd/alpha/run.go +++ b/dgraph/cmd/alpha/run.go @@ -150,8 +150,6 @@ they form a Raft group and provide synchronous replication. flag.Float64P("lru_mb", "l", -1, "Estimated memory the LRU cache can take. "+ "Actual usage by the process would be more than specified here.") - flag.Bool("debugmode", false, - "Enable debug mode for more debug information.") flag.String("mutations", "allow", "Set mutation mode to allow, disallow, or strict.") @@ -477,7 +475,6 @@ func run() { setupCustomTokenizers() x.Init() - x.Config.DebugMode = Alpha.Conf.GetBool("debugmode") x.Config.PortOffset = Alpha.Conf.GetInt("port_offset") x.Config.QueryEdgeLimit = cast.ToUint64(Alpha.Conf.GetString("query_edge_limit")) x.Config.NormalizeNodeLimit = cast.ToInt(Alpha.Conf.GetString("normalize_node_limit")) diff --git a/dgraph/cmd/cert/create.go b/dgraph/cmd/cert/create.go index df7bc6a7dab..d68ee579a02 100644 --- a/dgraph/cmd/cert/create.go +++ b/dgraph/cmd/cert/create.go @@ -24,14 +24,12 @@ import ( "crypto/rsa" "crypto/x509" "encoding/pem" - "errors" "fmt" + "github.com/pkg/errors" "io/ioutil" "os" "path" "path/filepath" - - "github.com/dgraph-io/dgraph/x" ) const ( @@ -97,7 +95,7 @@ func makeKey(keyFile string, c *certConfig) (crypto.PrivateKey, error) { Bytes: x509.MarshalPKCS1PrivateKey(k), }) } - return nil, x.Errorf("Unsupported key type: %T", key) + return nil, errors.Errorf("Unsupported key type: %T", key) } // readKey tries to read and decode the contents of a private key file. diff --git a/dgraph/cmd/cert/info.go b/dgraph/cmd/cert/info.go index fe344bc6d6f..2e70679a704 100644 --- a/dgraph/cmd/cert/info.go +++ b/dgraph/cmd/cert/info.go @@ -29,7 +29,7 @@ import ( "strings" "time" - "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) type certInfo struct { @@ -125,7 +125,7 @@ func getFileInfo(file string) *certInfo { } key, ok := priv.(crypto.Signer) if !ok { - info.err = x.Errorf("Unknown private key type: %T", key) + info.err = errors.Errorf("Unknown private key type: %T", key) } switch k := key.(type) { case *ecdsa.PrivateKey: diff --git a/dgraph/cmd/root.go b/dgraph/cmd/root.go index 16759c45d81..6048719a502 100644 --- a/dgraph/cmd/root.go +++ b/dgraph/cmd/root.go @@ -33,6 +33,7 @@ import ( "github.com/dgraph-io/dgraph/dgraph/cmd/version" "github.com/dgraph-io/dgraph/dgraph/cmd/zero" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -132,7 +133,7 @@ func initCmds() { } for _, sc := range subcommands { sc.Conf.SetConfigFile(cfg) - x.Check(x.Wrapf(sc.Conf.ReadInConfig(), "reading config")) + x.Check(errors.Wrapf(sc.Conf.ReadInConfig(), "reading config")) setGlogFlags(sc.Conf) } }) diff --git a/dgraph/cmd/zero/assign.go b/dgraph/cmd/zero/assign.go index c4c691c06d6..830346cd98b 100644 --- a/dgraph/cmd/zero/assign.go +++ b/dgraph/cmd/zero/assign.go @@ -17,14 +17,13 @@ package zero import ( - "errors" - otrace "go.opencensus.io/trace" "golang.org/x/net/context" "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" ) var emptyAssignedIds pb.AssignedIds @@ -68,11 +67,11 @@ func (s *Server) lease(ctx context.Context, num *pb.Num, txn bool) (*pb.Assigned // based on leader leases. If this node gets partitioned and unless checkquorum is enabled, this // node would still think that it's the leader. if !node.AmLeader() { - return &emptyAssignedIds, x.Errorf("Assigning IDs is only allowed on leader.") + return &emptyAssignedIds, errors.Errorf("Assigning IDs is only allowed on leader.") } if num.Val == 0 && !num.ReadOnly { - return &emptyAssignedIds, x.Errorf("Nothing to be leased") + return &emptyAssignedIds, errors.Errorf("Nothing to be leased") } if glog.V(3) { glog.Infof("Got lease request for txn: %v. Num: %+v\n", txn, num) @@ -175,12 +174,12 @@ func (s *Server) AssignUids(ctx context.Context, num *pb.Num) (*pb.AssignedIds, // I'm not the leader and this request was forwarded to me by a peer, who thought I'm the // leader. if num.Forwarded { - return x.Errorf("Invalid Zero received AssignUids request forward. Please retry") + return errors.Errorf("Invalid Zero received AssignUids request forward. Please retry") } // This is an original request. Forward it to the leader. pl := s.Leader(0) if pl == nil { - return x.Errorf("No healthy connection found to Leader of group zero") + return errors.Errorf("No healthy connection found to Leader of group zero") } span.Annotatef(nil, "Sending request to %v", pl.Addr) zc := pb.NewZeroClient(pl.Get()) diff --git a/dgraph/cmd/zero/oracle.go b/dgraph/cmd/zero/oracle.go index 9bfd2e5fd74..6c8398af864 100644 --- a/dgraph/cmd/zero/oracle.go +++ b/dgraph/cmd/zero/oracle.go @@ -17,7 +17,6 @@ package zero import ( - "errors" "math/rand" "strconv" "strings" @@ -28,6 +27,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" otrace "go.opencensus.io/trace" "golang.org/x/net/context" ) @@ -344,23 +344,23 @@ func (s *Server) commit(ctx context.Context, src *api.TxnContext) error { for pkey := range preds { splits := strings.Split(pkey, "-") if len(splits) < 2 { - return x.Errorf("Unable to find group id in %s", pkey) + return errors.Errorf("Unable to find group id in %s", pkey) } gid, err := strconv.Atoi(splits[0]) if err != nil { - return x.Errorf("Unable to parse group id from %s. Error: %v", pkey, err) + return errors.Wrapf(err, "unable to parse group id from %s", pkey) } pred := strings.Join(splits[1:], "-") tablet := s.ServingTablet(pred) if tablet == nil { - return x.Errorf("Tablet for %s is nil", pred) + return errors.Errorf("Tablet for %s is nil", pred) } if tablet.GroupId != uint32(gid) { - return x.Errorf("Mutation done in group: %d. Predicate %s assigned to %d", + return errors.Errorf("Mutation done in group: %d. Predicate %s assigned to %d", gid, pred, tablet.GroupId) } if s.isBlocked(pred) { - return x.Errorf("Commits on predicate %s are blocked due to predicate move", pred) + return errors.Errorf("Commits on predicate %s are blocked due to predicate move", pred) } } return nil @@ -402,7 +402,7 @@ func (s *Server) CommitOrAbort(ctx context.Context, src *api.TxnContext) (*api.T defer span.End() if !s.Node.AmLeader() { - return nil, x.Errorf("Only leader can decide to commit or abort") + return nil, errors.Errorf("Only leader can decide to commit or abort") } err := s.commit(ctx, src) if err != nil { diff --git a/dgraph/cmd/zero/raft.go b/dgraph/cmd/zero/raft.go index 0ecb04c8ed3..5d8db721ae9 100644 --- a/dgraph/cmd/zero/raft.go +++ b/dgraph/cmd/zero/raft.go @@ -17,7 +17,6 @@ package zero import ( - "errors" "fmt" "log" "math" @@ -35,6 +34,7 @@ import ( farm "github.com/dgryski/go-farm" "github.com/golang/glog" "github.com/google/uuid" + "github.com/pkg/errors" "go.etcd.io/etcd/raft" "go.etcd.io/etcd/raft/raftpb" "golang.org/x/net/context" @@ -80,12 +80,12 @@ var errInternalRetry = errors.New("Retry Raft proposal internally") func (n *node) proposeAndWait(ctx context.Context, proposal *pb.ZeroProposal) error { switch { case n.Raft() == nil: - return x.Errorf("Raft isn't initialized yet.") + return errors.Errorf("Raft isn't initialized yet.") case ctx.Err() != nil: return ctx.Err() case !n.AmLeader(): // Do this check upfront. Don't do this inside propose for reasons explained below. - return x.Errorf("Not Zero leader. Aborting proposal: %+v", proposal) + return errors.Errorf("Not Zero leader. Aborting proposal: %+v", proposal) } // We could consider adding a wrapper around the user proposal, so we can access any key-values. @@ -126,7 +126,7 @@ func (n *node) proposeAndWait(ctx context.Context, proposal *pb.ZeroProposal) er // Propose the change. if err := n.Raft().Propose(cctx, data); err != nil { span.Annotatef(nil, "Error while proposing via Raft: %v", err) - return x.Wrapf(err, "While proposing") + return errors.Wrapf(err, "While proposing") } // Wait for proposal to be applied or timeout. @@ -175,7 +175,7 @@ func (n *node) handleMemberProposal(member *pb.Member) error { m := n.server.member(member.Addr) // Ensures that different nodes don't have same address. if m != nil && (m.Id != member.Id || m.GroupId != member.GroupId) { - return x.Errorf("Found another member %d with same address: %v", m.Id, m.Addr) + return errors.Errorf("Found another member %d with same address: %v", m.Id, m.Addr) } if member.GroupId == 0 { state.Zeros[member.Id] = member @@ -210,7 +210,7 @@ func (n *node) handleMemberProposal(member *pb.Member) error { } if !has && len(group.Members) >= n.server.NumReplicas { // We shouldn't allow more members than the number of replicas. - return x.Errorf("Group reached replication level. Can't add another member: %+v", member) + return errors.Errorf("Group reached replication level. Can't add another member: %+v", member) } // Create a connection to this server. @@ -263,7 +263,7 @@ func (n *node) handleTabletProposal(tablet *pb.Tablet) error { }() if tablet.GroupId == 0 { - return x.Errorf("Tablet group id is zero: %+v", tablet) + return errors.Errorf("Tablet group id is zero: %+v", tablet) } group := state.Groups[tablet.GroupId] if tablet.Remove { @@ -396,7 +396,7 @@ func (n *node) applyConfChange(e raftpb.Entry) { for _, member := range n.server.membershipState().Removed { // It is not recommended to reuse RAFT ids. if member.GroupId == 0 && m.Id == member.Id { - err := x.Errorf("REUSE_RAFTID: Reusing removed id: %d.\n", m.Id) + err := errors.Errorf("REUSE_RAFTID: Reusing removed id: %d.\n", m.Id) n.DoneConfChange(cc.ID, err) // Cancel configuration change. cc.NodeID = raft.None @@ -450,7 +450,7 @@ func (n *node) initAndStartNode() error { } else if len(opts.peer) > 0 { p := conn.GetPools().Connect(opts.peer) if p == nil { - return x.Errorf("Unhealthy connection to %v", opts.peer) + return errors.Errorf("Unhealthy connection to %v", opts.peer) } gconn := p.Get() diff --git a/dgraph/cmd/zero/tablet.go b/dgraph/cmd/zero/tablet.go index 034260c1a92..0faa676c2ba 100644 --- a/dgraph/cmd/zero/tablet.go +++ b/dgraph/cmd/zero/tablet.go @@ -25,6 +25,7 @@ import ( "github.com/dgraph-io/dgraph/x" humanize "github.com/dustin/go-humanize" "github.com/golang/glog" + "github.com/pkg/errors" otrace "go.opencensus.io/trace" "golang.org/x/net/context" ) @@ -91,19 +92,19 @@ func (s *Server) movePredicate(predicate string, srcGroup, dstGroup uint32) erro // Ensure that reserved predicates cannot be moved. if x.IsReservedPredicate(predicate) { - return x.Errorf("Unable to move reserved predicate %s", predicate) + return errors.Errorf("Unable to move reserved predicate %s", predicate) } // Ensure that I'm connected to the rest of the Zero group, and am the leader. if _, err := s.latestMembershipState(ctx); err != nil { - return x.Errorf("Unable to reach quorum: %v", err) + return errors.Wrapf(err, "unable to reach quorum") } if !s.Node.AmLeader() { - return x.Errorf("I am not the Zero leader") + return errors.Errorf("I am not the Zero leader") } tab := s.ServingTablet(predicate) if tab == nil { - return x.Errorf("Tablet to be moved: [%v] is not being served", predicate) + return errors.Errorf("Tablet to be moved: [%v] is not being served", predicate) } msg := fmt.Sprintf("Going to move predicate: [%v], size: [%v] from group %d to %d\n", predicate, humanize.Bytes(uint64(tab.Space)), srcGroup, dstGroup) @@ -118,13 +119,13 @@ func (s *Server) movePredicate(predicate string, srcGroup, dstGroup uint32) erro // predicate. Source Alpha leader must reach this timestamp before streaming the data. ids, err := s.Timestamps(ctx, &pb.Num{Val: 1}) if err != nil || ids.StartId == 0 { - return x.Errorf("While leasing txn timestamp. Id: %+v Error: %v", ids, err) + return errors.Wrapf(err, "while leasing txn timestamp. Id: %+v", ids) } // Get connection to leader of source group. pl := s.Leader(srcGroup) if pl == nil { - return x.Errorf("No healthy connection found to leader of group %d", srcGroup) + return errors.Errorf("No healthy connection found to leader of group %d", srcGroup) } wc := pb.NewWorkerClient(pl.Get()) in := &pb.MovePredicatePayload{ @@ -150,7 +151,7 @@ func (s *Server) movePredicate(predicate string, srcGroup, dstGroup uint32) erro span.Annotate(nil, msg) glog.Info(msg) if err := s.Node.proposeAndWait(ctx, p); err != nil { - return x.Errorf("While proposing tablet reassignment. Proposal: %+v Error: %v", p, err) + return errors.Wrapf(err, "while proposing tablet reassignment. Proposal: %+v", p) } msg = fmt.Sprintf("Predicate move done for: [%v] from group %d to %d\n", predicate, srcGroup, dstGroup) diff --git a/dgraph/cmd/zero/zero.go b/dgraph/cmd/zero/zero.go index ae271c87b53..3d9389ec2e3 100644 --- a/dgraph/cmd/zero/zero.go +++ b/dgraph/cmd/zero/zero.go @@ -17,7 +17,6 @@ package zero import ( - "errors" "math" "sync" "time" @@ -32,6 +31,7 @@ import ( "github.com/dgraph-io/dgraph/x" "github.com/gogo/protobuf/proto" "github.com/golang/glog" + "github.com/pkg/errors" ) var ( @@ -312,7 +312,7 @@ func (s *Server) servingTablet(tablet string) *pb.Tablet { func (s *Server) createProposals(dst *pb.Group) ([]*pb.ZeroProposal, error) { var res []*pb.ZeroProposal if len(dst.Members) > 1 { - return res, x.Errorf("Create Proposal: Invalid group: %+v", dst) + return res, errors.Errorf("Create Proposal: Invalid group: %+v", dst) } s.RLock() @@ -321,11 +321,11 @@ func (s *Server) createProposals(dst *pb.Group) ([]*pb.ZeroProposal, error) { for mid, dstMember := range dst.Members { group, has := s.state.Groups[dstMember.GroupId] if !has { - return res, x.Errorf("Unknown group for member: %+v", dstMember) + return res, errors.Errorf("Unknown group for member: %+v", dstMember) } srcMember, has := group.Members[mid] if !has { - return res, x.Errorf("Unknown member: %+v", dstMember) + return res, errors.Errorf("Unknown member: %+v", dstMember) } if srcMember.Addr != dstMember.Addr || srcMember.Leader != dstMember.Leader { @@ -348,7 +348,7 @@ func (s *Server) createProposals(dst *pb.Group) ([]*pb.ZeroProposal, error) { for key, dstTablet := range dst.Tablets { group, has := s.state.Groups[dstTablet.GroupId] if !has { - return res, x.Errorf("Unknown group for tablet: %+v", dstTablet) + return res, errors.Errorf("Unknown group for tablet: %+v", dstTablet) } srcTablet, has := group.Tablets[key] if !has { @@ -377,13 +377,13 @@ func (s *Server) removeNode(ctx context.Context, nodeId uint64, groupId uint32) zp := &pb.ZeroProposal{} zp.Member = &pb.Member{Id: nodeId, GroupId: groupId, AmDead: true} if _, ok := s.state.Groups[groupId]; !ok { - return x.Errorf("No group with groupId %d found", groupId) + return errors.Errorf("No group with groupId %d found", groupId) } if _, ok := s.state.Groups[groupId].Members[nodeId]; !ok { - return x.Errorf("No node with nodeId %d found in group %d", nodeId, groupId) + return errors.Errorf("No node with nodeId %d found in group %d", nodeId, groupId) } if len(s.state.Groups[groupId].Members) == 1 && len(s.state.Groups[groupId].Tablets) > 0 { - return x.Errorf("Move all tablets from group %d before removing the last node", groupId) + return errors.Errorf("Move all tablets from group %d before removing the last node", groupId) } return s.Node.proposeAndWait(ctx, zp) @@ -399,8 +399,8 @@ func (s *Server) Connect(ctx context.Context, defer glog.Infof("Connected: %+v\n", m) if ctx.Err() != nil { - x.Errorf("Context has error: %v\n", ctx.Err()) - return &emptyConnectionState, ctx.Err() + err := errors.Errorf("Context has error: %v\n", ctx.Err()) + return &emptyConnectionState, err } ms, err := s.latestMembershipState(ctx) if err != nil { @@ -416,13 +416,13 @@ func (s *Server) Connect(ctx context.Context, return cs, err } if len(m.Addr) == 0 { - return &emptyConnectionState, x.Errorf("NO_ADDR: No address provided: %+v", m) + return &emptyConnectionState, errors.Errorf("NO_ADDR: No address provided: %+v", m) } for _, member := range ms.Removed { // It is not recommended to reuse RAFT ids. if member.GroupId != 0 && m.Id == member.Id { - return &emptyConnectionState, x.Errorf( + return &emptyConnectionState, errors.Errorf( "REUSE_RAFTID: Duplicate Raft ID %d to removed member: %+v", m.Id, member) } } @@ -441,14 +441,14 @@ func (s *Server) Connect(ctx context.Context, case member.Addr == m.Addr && member.Id != m.Id: // Same address. Different Id. If Id is zero, then it might be trying to connect for // the first time. We can just directly return the membership information. - return nil, x.Errorf("REUSE_ADDR: Duplicate address to existing member: %+v."+ + return nil, errors.Errorf("REUSE_ADDR: Duplicate address to existing member: %+v."+ " Self: +%v", member, m) case member.Addr != m.Addr && member.Id == m.Id: // Same Id. Different address. if pl, err := conn.GetPools().Get(member.Addr); err == nil && pl.IsHealthy() { // Found a healthy connection. - return nil, x.Errorf("REUSE_RAFTID: Healthy connection to a member"+ + return nil, errors.Errorf("REUSE_RAFTID: Healthy connection to a member"+ " with same ID: %+v", member) } } @@ -531,10 +531,10 @@ func (s *Server) ShouldServe( defer span.End() if len(tablet.Predicate) == 0 { - return resp, x.Errorf("Tablet predicate is empty in %+v", tablet) + return resp, errors.Errorf("Tablet predicate is empty in %+v", tablet) } if tablet.GroupId == 0 && !tablet.ReadOnly { - return resp, x.Errorf("Group ID is Zero in %+v", tablet) + return resp, errors.Errorf("Group ID is Zero in %+v", tablet) } // Check who is serving this tablet. @@ -634,7 +634,7 @@ func (s *Server) deletePredicates(ctx context.Context, group *pb.Group) error { break } if gid == 0 { - return x.Errorf("Unable to find group") + return errors.Errorf("Unable to find group") } state, err := s.latestMembershipState(ctx) if err != nil { @@ -642,12 +642,12 @@ func (s *Server) deletePredicates(ctx context.Context, group *pb.Group) error { } sg, ok := state.Groups[gid] if !ok { - return x.Errorf("Unable to find group: %d", gid) + return errors.Errorf("Unable to find group: %d", gid) } pl := s.Leader(gid) if pl == nil { - return x.Errorf("Unable to reach leader of group: %d", gid) + return errors.Errorf("Unable to reach leader of group: %d", gid) } wc := pb.NewWorkerClient(pl.Get()) diff --git a/edgraph/server.go b/edgraph/server.go index 5955d44bd3c..d5a64a6d309 100644 --- a/edgraph/server.go +++ b/edgraph/server.go @@ -44,6 +44,7 @@ import ( "github.com/dgraph-io/dgraph/worker" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" "golang.org/x/net/context" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" @@ -286,7 +287,7 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er if op.Schema == "" && op.DropAttr == "" && !op.DropAll && op.DropOp == api.Operation_NONE { // Must have at least one field set. This helps users if they attempt // to set a field but use the wrong name (could be decoded from JSON). - return nil, x.Errorf("Operation must have at least one field set") + return nil, errors.Errorf("Operation must have at least one field set") } empty := &api.Payload{} if err := x.HealthCheck(); err != nil { @@ -294,11 +295,11 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er } if isDropAll(op) && op.DropOp == api.Operation_DATA { - return nil, x.Errorf("Only one of DropAll and DropData can be true") + return nil, errors.Errorf("Only one of DropAll and DropData can be true") } if !isMutationAllowed(ctx) { - return nil, x.Errorf("No mutations allowed by server.") + return nil, errors.Errorf("No mutations allowed by server.") } if err := isAlterAllowed(ctx); err != nil { glog.Warningf("Alter denied with error: %v\n", err) @@ -458,7 +459,7 @@ func (s *Server) doMutate(ctx context.Context, mu *api.Mutation) (resp *api.Assi } if !isMutationAllowed(ctx) { - return nil, x.Errorf("No mutations allowed.") + return nil, errors.Errorf("No mutations allowed.") } if mu.StartTs == 0 { mu.StartTs = State.getTimestamp(false) @@ -633,7 +634,7 @@ func (s *Server) doQuery(ctx context.Context, req *api.Request) (resp *api.Respo if req.BestEffort { // Sanity: check that request is read-only too. if !req.ReadOnly { - return resp, x.Errorf("A best effort query must be read-only.") + return resp, errors.Errorf("A best effort query must be read-only.") } if req.StartTs == 0 { req.StartTs = posting.Oracle().MaxAssigned() @@ -650,7 +651,7 @@ func (s *Server) doQuery(ctx context.Context, req *api.Request) (resp *api.Respo // Core processing happens here. var er query.ExecutionResult if er, err = queryRequest.Process(ctx); err != nil { - return resp, x.Wrap(err) + return resp, errors.Wrap(err, "") } var js []byte if len(er.SchemaNode) > 0 || len(er.Types) > 0 { @@ -738,7 +739,7 @@ func isMutationAllowed(ctx context.Context) bool { return true } -var errNoAuth = x.Errorf("No Auth Token found. Token needed for Alter operations.") +var errNoAuth = errors.Errorf("No Auth Token found. Token needed for Alter operations.") func isAlterAllowed(ctx context.Context) error { p, ok := peer.FromContext(ctx) @@ -757,7 +758,7 @@ func isAlterAllowed(ctx context.Context) error { return errNoAuth } if tokens[0] != Config.AuthToken { - return x.Errorf("Provided auth token [%s] does not match. Permission denied.", tokens[0]) + return errors.Errorf("Provided auth token [%s] does not match. Permission denied.", tokens[0]) } return nil } @@ -859,10 +860,10 @@ func validateNQuads(set, del []*api.NQuad) error { ostar = o.DefaultVal == x.Star } if nq.Subject == x.Star || nq.Predicate == x.Star || ostar { - return x.Errorf("Cannot use star in set n-quad: %+v", nq) + return errors.Errorf("Cannot use star in set n-quad: %+v", nq) } if err := validateKeys(nq); err != nil { - return x.Errorf("Key error: %s: %+v", err, nq) + return errors.Wrapf(err, "key error: %+v", nq) } } for _, nq := range del { @@ -874,7 +875,7 @@ func validateNQuads(set, del []*api.NQuad) error { ostar = o.DefaultVal == x.Star } if nq.Subject == x.Star || (nq.Predicate == x.Star && !ostar) { - return x.Errorf("Only valid wildcard delete patterns are 'S * *' and 'S P *': %v", nq) + return errors.Errorf("Only valid wildcard delete patterns are 'S * *' and 'S P *': %v", nq) } // NOTE: we dont validateKeys() with delete to let users fix existing mistakes // with bad predicate forms. ex: foo@bar ~something @@ -885,11 +886,11 @@ func validateNQuads(set, del []*api.NQuad) error { func validateKey(key string) error { switch { case len(key) == 0: - return x.Errorf("Has zero length") + return errors.Errorf("Has zero length") case strings.ContainsAny(key, "~@"): - return x.Errorf("Has invalid characters") + return errors.Errorf("Has invalid characters") case strings.IndexFunc(key, unicode.IsSpace) != -1: - return x.Errorf("Must not contain spaces") + return errors.Errorf("Must not contain spaces") } return nil } @@ -897,14 +898,14 @@ func validateKey(key string) error { // validateKeys checks predicate and facet keys in N-Quad for syntax errors. func validateKeys(nq *api.NQuad) error { if err := validateKey(nq.Predicate); err != nil { - return x.Errorf("predicate %q %s", nq.Predicate, err) + return errors.Wrapf(err, "predicate %q", nq.Predicate) } for i := range nq.Facets { if nq.Facets[i] == nil { continue } if err := validateKey(nq.Facets[i].Key); err != nil { - return x.Errorf("Facet %q, %s", nq.Facets[i].Key, err) + return errors.Errorf("Facet %q, %s", nq.Facets[i].Key, err) } } return nil diff --git a/ee/acl/utils.go b/ee/acl/utils.go index 37b06cf3e7f..640d0a9c1f8 100644 --- a/ee/acl/utils.go +++ b/ee/acl/utils.go @@ -24,6 +24,7 @@ import ( "github.com/dgraph-io/dgo/protos/api" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" "github.com/spf13/viper" "golang.org/x/crypto/ssh/terminal" ) @@ -82,7 +83,7 @@ func UnmarshalUser(resp *api.Response, userKey string) (user *User, err error) { err = json.Unmarshal(resp.GetJson(), &m) if err != nil { - return nil, fmt.Errorf("unable to unmarshal the query user response:%v", err) + return nil, errors.Wrapf(err, "unable to unmarshal the query user response") } users := m[userKey] if len(users) == 0 { @@ -90,7 +91,7 @@ func UnmarshalUser(resp *api.Response, userKey string) (user *User, err error) { return nil, nil } if len(users) > 1 { - return nil, x.Errorf("Found multiple users: %s", resp.GetJson()) + return nil, errors.Errorf("Found multiple users: %s", resp.GetJson()) } return &users[0], nil } diff --git a/ee/backup/file_handler.go b/ee/backup/file_handler.go index a4cca71f981..eecc5483013 100644 --- a/ee/backup/file_handler.go +++ b/ee/backup/file_handler.go @@ -25,6 +25,7 @@ import ( "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" ) // fileHandler is used for 'file:' URI scheme. @@ -49,7 +50,7 @@ func (h *fileHandler) Create(uri *url.URL, req *Request) error { // check that the path exists and we can access it. if !pathExist(uri.Path) { - return x.Errorf("The path %q does not exist or it is inaccessible.", uri.Path) + return errors.Errorf("The path %q does not exist or it is inaccessible.", uri.Path) } // Find the max version from the latest backup. This is done only when starting a new @@ -106,7 +107,7 @@ func (h *fileHandler) Create(uri *url.URL, req *Request) error { // Returns nil and the maximum Ts version on success, error otherwise. func (h *fileHandler) Load(uri *url.URL, fn loadFn) (uint64, error) { if !pathExist(uri.Path) { - return 0, x.Errorf("The path %q does not exist or it is inaccessible.", uri.Path) + return 0, errors.Errorf("The path %q does not exist or it is inaccessible.", uri.Path) } // Get a list of all the manifest files at the location. @@ -115,7 +116,7 @@ func (h *fileHandler) Load(uri *url.URL, fn loadFn) (uint64, error) { return !isdir && strings.HasSuffix(path, suffix) }) if len(manifests) == 0 { - return 0, x.Errorf("No manifests found at path: %s", uri.Path) + return 0, errors.Errorf("No manifests found at path: %s", uri.Path) } sort.Strings(manifests) if glog.V(3) { @@ -131,7 +132,7 @@ func (h *fileHandler) Load(uri *url.URL, fn loadFn) (uint64, error) { for _, manifest := range manifests { var m Manifest if err := h.readManifest(manifest, &m); err != nil { - return 0, x.Wrapf(err, "While reading %q", manifest) + return 0, errors.Wrapf(err, "While reading %q", manifest) } if m.ReadTs == 0 || m.Version == 0 || len(m.Groups) == 0 { if glog.V(2) { @@ -146,7 +147,7 @@ func (h *fileHandler) Load(uri *url.URL, fn loadFn) (uint64, error) { file := filepath.Join(path, fmt.Sprintf(backupNameFmt, m.ReadTs, groupId)) fp, err := os.Open(file) if err != nil { - return 0, x.Wrapf(err, "Failed to open %q", file) + return 0, errors.Wrapf(err, "Failed to open %q", file) } defer fp.Close() if err = fn(fp, int(groupId)); err != nil { @@ -161,7 +162,7 @@ func (h *fileHandler) Load(uri *url.URL, fn loadFn) (uint64, error) { // ListManifests loads the manifests in the locations and returns them. func (h *fileHandler) ListManifests(uri *url.URL) ([]string, error) { if !pathExist(uri.Path) { - return nil, x.Errorf("The path %q does not exist or it is inaccessible.", uri.Path) + return nil, errors.Errorf("The path %q does not exist or it is inaccessible.", uri.Path) } // Get a list of all the manifest files at the location. @@ -170,7 +171,7 @@ func (h *fileHandler) ListManifests(uri *url.URL) ([]string, error) { return !isdir && strings.HasSuffix(path, suffix) }) if len(manifests) == 0 { - return nil, x.Errorf("No manifests found at path: %s", uri.Path) + return nil, errors.Errorf("No manifests found at path: %s", uri.Path) } sort.Strings(manifests) if glog.V(3) { diff --git a/ee/backup/handler.go b/ee/backup/handler.go index 6cdc22ff3eb..83b9cd8e228 100644 --- a/ee/backup/handler.go +++ b/ee/backup/handler.go @@ -16,7 +16,7 @@ import ( "io" "net/url" - "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) const ( @@ -130,7 +130,7 @@ func (r *Request) newHandler() (handler, error) { // find handler for this URI scheme h = getHandler(uri.Scheme) if h == nil { - return nil, x.Errorf("Unable to handle url: %s", uri) + return nil, errors.Errorf("Unable to handle url: %s", uri) } if err = h.Create(uri, r); err != nil { @@ -153,7 +153,7 @@ func Load(l string, fn loadFn) (version uint64, err error) { h := getHandler(uri.Scheme) if h == nil { - return 0, x.Errorf("Unsupported URI: %v", uri) + return 0, errors.Errorf("Unsupported URI: %v", uri) } return h.Load(uri, fn) @@ -168,7 +168,7 @@ func ListManifests(l string) ([]*ManifestStatus, error) { h := getHandler(uri.Scheme) if h == nil { - return nil, x.Errorf("Unsupported URI: %v", uri) + return nil, errors.Errorf("Unsupported URI: %v", uri) } paths, err := h.ListManifests(uri) @@ -182,7 +182,7 @@ func ListManifests(l string) ([]*ManifestStatus, error) { var ms ManifestStatus if err := h.ReadManifest(path, &m); err != nil { - return nil, x.Wrapf(err, "While reading %q", path) + return nil, errors.Wrapf(err, "While reading %q", path) } ms.Manifest = &m ms.FileName = path diff --git a/ee/backup/run.go b/ee/backup/run.go index b31b7ef63f0..c2120e737e8 100644 --- a/ee/backup/run.go +++ b/ee/backup/run.go @@ -26,6 +26,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" "github.com/spf13/cobra" "google.golang.org/grpc" ) @@ -181,7 +182,7 @@ func runRestoreCmd() error { grpc.WithBlock(), grpc.WithInsecure()) if err != nil { - return x.Wrapf(err, "Unable to connect to %s", opt.zero) + return errors.Wrapf(err, "Unable to connect to %s", opt.zero) } zc = pb.NewZeroClient(zero) } @@ -192,7 +193,7 @@ func runRestoreCmd() error { return err } if version == 0 { - return x.Errorf("Failed to obtain a restore version") + return errors.Errorf("Failed to obtain a restore version") } if glog.V(2) { fmt.Printf("Restore version: %d\n", version) @@ -249,7 +250,7 @@ func runLsbackupCmd() error { fmt.Println("Listing backups from:", opt.location) manifests, err := ListManifests(opt.location) if err != nil { - return x.Errorf("Error while listing manifests: %v", err.Error()) + return errors.Wrapf(err, "while listing manifests") } fmt.Printf("Name\tVersion\tReadTs\tGroups\n") diff --git a/ee/backup/s3_handler.go b/ee/backup/s3_handler.go index add08ca6f35..9693b5a39d4 100644 --- a/ee/backup/s3_handler.go +++ b/ee/backup/s3_handler.go @@ -29,6 +29,7 @@ import ( minio "github.com/minio/minio-go" "github.com/minio/minio-go/pkg/credentials" "github.com/minio/minio-go/pkg/s3utils" + "github.com/pkg/errors" ) const ( @@ -62,7 +63,7 @@ func (h *s3Handler) credentialsInRequest() bool { // Returns a new S3 minio client, otherwise a nil client with an error. func (h *s3Handler) setup(uri *url.URL) (*minio.Client, error) { if len(uri.Path) < 1 { - return nil, x.Errorf("Invalid bucket: %q", uri.Path) + return nil, errors.Errorf("Invalid bucket: %q", uri.Path) } glog.V(2).Infof("Backup using host: %s, path: %s", uri.Host, uri.Path) @@ -79,7 +80,7 @@ func (h *s3Handler) setup(uri *url.URL) (*minio.Client, error) { uri.Host = defaultEndpointS3 } if !s3utils.IsAmazonEndpoint(*uri) { - return nil, x.Errorf("Invalid S3 endpoint %q", uri.Host) + return nil, errors.Errorf("Invalid S3 endpoint %q", uri.Host) } // Access Key ID: AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY. // Secret Access Key: AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY. @@ -88,7 +89,7 @@ func (h *s3Handler) setup(uri *url.URL) (*minio.Client, error) { default: // minio if uri.Host == "" { - return nil, x.Errorf("Minio handler requires a host") + return nil, errors.Errorf("Minio handler requires a host") } // Access Key ID: MINIO_ACCESS_KEY. // Secret Access Key: MINIO_SECRET_KEY. @@ -131,11 +132,11 @@ func (h *s3Handler) setup(uri *url.URL) (*minio.Client, error) { // verify the requested bucket exists. found, err := mc.BucketExists(h.bucketName) if err != nil { - return nil, x.Errorf("Error while looking for bucket: %s at host: %s. Error: %v", - h.bucketName, uri.Host, err) + return nil, errors.Wrapf(err, "while looking for bucket %s at host %s", + h.bucketName, uri.Host) } if !found { - return nil, x.Errorf("Bucket was not found: %s", h.bucketName) + return nil, errors.Errorf("Bucket was not found: %s", h.bucketName) } if len(parts) > 1 { h.objectPrefix = filepath.Join(parts[1:]...) @@ -242,7 +243,7 @@ func (h *s3Handler) Load(uri *url.URL, fn loadFn) (uint64, error) { } } if len(manifests) == 0 { - return 0, x.Errorf("No manifests found at: %s", uri.String()) + return 0, errors.Errorf("No manifests found at: %s", uri.String()) } sort.Strings(manifests) if glog.V(3) { @@ -258,7 +259,7 @@ func (h *s3Handler) Load(uri *url.URL, fn loadFn) (uint64, error) { for _, manifest := range manifests { var m Manifest if err := h.readManifest(mc, manifest, &m); err != nil { - return 0, x.Wrapf(err, "While reading %q", manifest) + return 0, errors.Wrapf(err, "While reading %q", manifest) } if m.ReadTs == 0 || m.Version == 0 || len(m.Groups) == 0 { if glog.V(2) { @@ -273,15 +274,15 @@ func (h *s3Handler) Load(uri *url.URL, fn loadFn) (uint64, error) { object := filepath.Join(path, fmt.Sprintf(backupNameFmt, m.ReadTs, groupId)) reader, err := mc.GetObject(h.bucketName, object, minio.GetObjectOptions{}) if err != nil { - return 0, x.Wrapf(err, "Failed to get %q", object) + return 0, errors.Wrapf(err, "Failed to get %q", object) } defer reader.Close() st, err := reader.Stat() if err != nil { - return 0, x.Wrapf(err, "Stat failed %q", object) + return 0, errors.Wrapf(err, "Stat failed %q", object) } if st.Size <= 0 { - return 0, x.Errorf("Remote object is empty or inaccessible: %s", object) + return 0, errors.Errorf("Remote object is empty or inaccessible: %s", object) } fmt.Printf("Downloading %q, %d bytes\n", object, st.Size) if err = fn(reader, int(groupId)); err != nil { @@ -312,7 +313,7 @@ func (h *s3Handler) ListManifests(uri *url.URL) ([]string, error) { } } if len(manifests) == 0 { - return nil, x.Errorf("No manifests found at: %s", uri.String()) + return nil, errors.Errorf("No manifests found at: %s", uri.String()) } sort.Strings(manifests) if glog.V(3) { diff --git a/gql/math.go b/gql/math.go index 37d2e1e5789..c5327c93cf2 100644 --- a/gql/math.go +++ b/gql/math.go @@ -24,6 +24,7 @@ import ( "github.com/dgraph-io/dgraph/lex" "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) type mathTreeStack struct{ a []*MathTree } @@ -41,7 +42,7 @@ func (s *mathTreeStack) popAssert() *MathTree { func (s *mathTreeStack) pop() (*MathTree, error) { if s.empty() { - return nil, x.Errorf("Empty stack") + return nil, errors.Errorf("Empty stack") } last := s.a[len(s.a)-1] s.a = s.a[:len(s.a)-1] @@ -96,25 +97,25 @@ func isZero(f string, rval types.Val) bool { func evalMathStack(opStack, valueStack *mathTreeStack) error { topOp, err := opStack.pop() if err != nil { - return x.Errorf("Invalid Math expression") + return errors.Errorf("Invalid Math expression") } if isUnary(topOp.Fn) { // Since "not" is a unary operator, just pop one value. topVal, err := valueStack.pop() if err != nil { - return x.Errorf("Invalid math statement. Expected 1 operands") + return errors.Errorf("Invalid math statement. Expected 1 operands") } if opStack.size() > 1 { peek := opStack.peek().Fn if (peek == "/" || peek == "%") && isZero(topOp.Fn, topVal.Const) { - return x.Errorf("Division by zero") + return errors.Errorf("Division by zero") } } topOp.Child = []*MathTree{topVal} } else if isTernary(topOp.Fn) { if valueStack.size() < 3 { - return x.Errorf("Invalid Math expression. Expected 3 operands") + return errors.Errorf("Invalid Math expression. Expected 3 operands") } topVal1 := valueStack.popAssert() topVal2 := valueStack.popAssert() @@ -123,10 +124,10 @@ func evalMathStack(opStack, valueStack *mathTreeStack) error { } else { if valueStack.size() < 2 { - return x.Errorf("Invalid Math expression. Expected 2 operands") + return errors.Errorf("Invalid Math expression. Expected 2 operands") } if isZero(topOp.Fn, valueStack.peek().Const) { - return x.Errorf("Division by zero.") + return errors.Errorf("Division by zero.") } topVal1 := valueStack.popAssert() topVal2 := valueStack.popAssert() @@ -154,7 +155,7 @@ func parseMathFunc(it *lex.ItemIterator, again bool) (*MathTree, bool, error) { it.Next() item := it.Item() if item.Typ != itemLeftRound { - return nil, false, x.Errorf("Expected ( after math") + return nil, false, errors.Errorf("Expected ( after math") } } @@ -216,7 +217,7 @@ func parseMathFunc(it *lex.ItemIterator, again bool) (*MathTree, bool, error) { if peekIt[0].Typ == itemLeftRound { again := false if !isMathFunc(item.Val) { - return nil, false, x.Errorf("Unknown math function: %v", item.Val) + return nil, false, errors.Errorf("Unknown math function: %v", item.Val) } var child *MathTree for { @@ -259,13 +260,13 @@ func parseMathFunc(it *lex.ItemIterator, again bool) (*MathTree, bool, error) { } _, err := opStack.pop() // Pop away the (. if err != nil { - return nil, false, x.Errorf("Invalid Math expression") + return nil, false, errors.Errorf("Invalid Math expression") } if !opStack.empty() { - return nil, false, x.Errorf("Invalid math expression.") + return nil, false, errors.Errorf("Invalid math expression.") } if valueStack.size() != 1 { - return nil, false, x.Errorf("Expected one item in value stack, but got %d", + return nil, false, errors.Errorf("Expected one item in value stack, but got %d", valueStack.size()) } res, err := valueStack.pop() @@ -286,14 +287,14 @@ func parseMathFunc(it *lex.ItemIterator, again bool) (*MathTree, bool, error) { } _, err := opStack.pop() // Pop away the (. if err != nil { - return nil, false, x.Errorf("Invalid Math expression") + return nil, false, errors.Errorf("Invalid Math expression") } if opStack.empty() { // The parentheses are balanced out. Let's break. break } } else { - return nil, false, x.Errorf("Unexpected item while parsing math expression: %v", item) + return nil, false, errors.Errorf("Unexpected item while parsing math expression: %v", item) } } @@ -307,11 +308,11 @@ func parseMathFunc(it *lex.ItemIterator, again bool) (*MathTree, bool, error) { if valueStack.empty() { // This happens when we have math(). We can either return an error or // ignore. Currently, let's just ignore and pretend there is no expression. - return nil, false, x.Errorf("Empty () not allowed in math block.") + return nil, false, errors.Errorf("Empty () not allowed in math block.") } if valueStack.size() != 1 { - return nil, false, x.Errorf("Expected one item in value stack, but got %d", + return nil, false, errors.Errorf("Expected one item in value stack, but got %d", valueStack.size()) } res, err := valueStack.pop() diff --git a/gql/mutation.go b/gql/mutation.go index 47ddfb76152..511aa6c2c25 100644 --- a/gql/mutation.go +++ b/gql/mutation.go @@ -17,7 +17,6 @@ package gql import ( - "errors" "fmt" "strconv" @@ -25,6 +24,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) var ( @@ -104,7 +104,7 @@ func toUid(subject string, newToUid map[string]uint64) (uid uint64, err error) { if id, present := newToUid[subject]; present { return id, err } - return 0, x.Errorf("UID not found/generated for xid %s\n", subject) + return 0, errors.Errorf("UID not found/generated for xid %s\n", subject) } var emptyEdge pb.DirectedEdge @@ -168,7 +168,7 @@ func (nq NQuad) CreateValueEdge(subjectUid uint64) (*pb.DirectedEdge, error) { func (nq NQuad) ToDeletePredEdge() (*pb.DirectedEdge, error) { if nq.Subject != x.Star && nq.ObjectValue.String() != x.Star { - return &emptyEdge, x.Errorf("Subject and object both should be *. Got: %+v", nq) + return &emptyEdge, errors.Errorf("Subject and object both should be *. Got: %+v", nq) } out := &pb.DirectedEdge{ @@ -214,7 +214,7 @@ func (nq NQuad) ToEdgeUsing(newToUid map[string]uint64) (*pb.DirectedEdge, error case x.ValuePlain, x.ValueMulti: edge, err = nq.CreateValueEdge(sUid) default: - return &emptyEdge, x.Errorf("Unknown value type for nquad: %+v", nq) + return &emptyEdge, errors.Errorf("Unknown value type for nquad: %+v", nq) } if err != nil { return nil, err diff --git a/gql/parser.go b/gql/parser.go index e5d28c18cbc..1ca31147741 100644 --- a/gql/parser.go +++ b/gql/parser.go @@ -27,6 +27,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" ) const ( @@ -219,7 +220,7 @@ func (fn *fragmentNode) expand(fmap fragmentMap) error { return nil } if fn.Entered { - return x.Errorf("Cycle detected: %s", fn.Name) + return errors.Errorf("Cycle detected: %s", fn.Name) } fn.Entered = true if err := fn.Gq.expandFragments(fmap); err != nil { @@ -239,7 +240,7 @@ func (gq *GraphQuery) expandFragments(fmap fragmentMap) error { fname := child.fragment // Name of fragment being referenced. fchild := fmap[fname] if fchild == nil { - return x.Errorf("Missing fragment: %s", fname) + return errors.Errorf("Missing fragment: %s", fname) } if err := fchild.expand(fmap); err != nil { return err @@ -286,13 +287,13 @@ func checkValueType(vm varMap) error { typ := v.Type if len(typ) == 0 { - return x.Errorf("Type of variable %v not specified", k) + return errors.Errorf("Type of variable %v not specified", k) } // Ensure value is not nil if the variable is required. if typ[len(typ)-1] == '!' { if v.Value == "" { - return x.Errorf("Variable %v should be initialised", k) + return errors.Errorf("Variable %v should be initialised", k) } typ = typ[:len(typ)-1] } @@ -303,24 +304,24 @@ func checkValueType(vm varMap) error { case "int": { if _, err := strconv.ParseInt(v.Value, 0, 64); err != nil { - return x.Wrapf(err, "Expected an int but got %v", v.Value) + return errors.Wrapf(err, "Expected an int but got %v", v.Value) } } case "float": { if _, err := strconv.ParseFloat(v.Value, 64); err != nil { - return x.Wrapf(err, "Expected a float but got %v", v.Value) + return errors.Wrapf(err, "Expected a float but got %v", v.Value) } } case "bool": { if _, err := strconv.ParseBool(v.Value); err != nil { - return x.Wrapf(err, "Expected a bool but got %v", v.Value) + return errors.Wrapf(err, "Expected a bool but got %v", v.Value) } } case "string": // Value is a valid string. No checks required. default: - return x.Errorf("Type %v not supported", typ) + return errors.Errorf("Type %q not supported", typ) } } } @@ -332,7 +333,7 @@ func substituteVar(f string, res *string, vmap varMap) error { if len(f) > 0 && f[0] == '$' { va, ok := vmap[f] if !ok || va.Type == "" { - return x.Errorf("Variable not defined %v", f) + return errors.Errorf("Variable not defined %v", f) } *res = va.Value } @@ -352,7 +353,7 @@ func substituteVariables(gq *GraphQuery, vmap varMap) error { idVal, ok := gq.Args["id"] if ok && len(gq.UID) == 0 { if idVal == "" { - return x.Errorf("Id can't be empty") + return errors.Errorf("Id can't be empty") } uids, err := parseID(idVal) if err != nil { @@ -415,10 +416,10 @@ func substituteVariablesFilter(f *FilterTree, vmap varMap) error { // This is to support GraphQL variables in uid functions. idVal, ok := vmap[v.Value] if !ok { - return x.Errorf("Couldn't find value for GraphQL variable: [%s]", v.Value) + return errors.Errorf("Couldn't find value for GraphQL variable: [%s]", v.Value) } if idVal.Value == "" { - return x.Errorf("Id can't be empty") + return errors.Errorf("Id can't be empty") } uids, err := parseID(idVal.Value) if err != nil { @@ -558,7 +559,7 @@ func validateResult(res *Result) error { continue } if _, found := seenQueryAliases[q.Alias]; found { - return x.Errorf("Duplicate aliases not allowed: %v", q.Alias) + return errors.Errorf("Duplicate aliases not allowed: %v", q.Alias) } seenQueryAliases[q.Alias] = true } @@ -582,22 +583,22 @@ func checkDependency(vl []*Vars) error { defines = x.RemoveDuplicates(defines) if len(defines) != lenBefore { - return x.Errorf("Some variables are declared multiple times.") + return errors.Errorf("Some variables are declared multiple times.") } if len(defines) > len(needs) { - return x.Errorf("Some variables are defined but not used\nDefined:%v\nUsed:%v\n", + return errors.Errorf("Some variables are defined but not used\nDefined:%v\nUsed:%v\n", defines, needs) } if len(defines) < len(needs) { - return x.Errorf("Some variables are used but not defined\nDefined:%v\nUsed:%v\n", + return errors.Errorf("Some variables are used but not defined\nDefined:%v\nUsed:%v\n", defines, needs) } for i := 0; i < len(defines); i++ { if defines[i] != needs[i] { - return x.Errorf("Variables are not used properly. \nDefined:%v\nUsed:%v\n", + return errors.Errorf("Variables are not used properly. \nDefined:%v\nUsed:%v\n", defines, needs) } } @@ -1092,7 +1093,7 @@ func unquoteIfQuoted(str string) (string, error) { return str, nil } uq, err := strconv.Unquote(str) - return uq, x.Wrapf(err, "could not unquote %q:", str) + return uq, errors.Wrapf(err, "could not unquote %q:", str) } // parseArguments parses the arguments part of the GraphQL query root. @@ -1274,7 +1275,7 @@ func (s *filterTreeStack) popAssert() *FilterTree { func (s *filterTreeStack) pop() (*FilterTree, error) { if s.empty() { - return nil, x.Errorf("Empty stack") + return nil, errors.Errorf("Empty stack") } last := s.a[len(s.a)-1] s.a = s.a[:len(s.a)-1] @@ -1289,19 +1290,19 @@ func (s *filterTreeStack) peek() *FilterTree { func evalStack(opStack, valueStack *filterTreeStack) error { topOp, err := opStack.pop() if err != nil { - return x.Errorf("Invalid filter statement") + return errors.Errorf("Invalid filter statement") } if topOp.Op == "not" { // Since "not" is a unary operator, just pop one value. topVal, err := valueStack.pop() if err != nil { - return x.Errorf("Invalid filter statement") + return errors.Errorf("Invalid filter statement") } topOp.Child = []*FilterTree{topVal} } else { // "and" and "or" are binary operators, so pop two values. if valueStack.size() < 2 { - return x.Errorf("Invalid filter statement") + return errors.Errorf("Invalid filter statement") } topVal1 := valueStack.popAssert() topVal2 := valueStack.popAssert() @@ -1434,7 +1435,7 @@ type regexArgs struct { func parseRegexArgs(val string) (regexArgs, error) { end := strings.LastIndex(val, "/") if end < 0 { - return regexArgs{}, x.Errorf("Unexpected error while parsing regex arg: %s", val) + return regexArgs{}, errors.Errorf("Unexpected error while parsing regex arg: %s", val) } expr := strings.Replace(val[1:end], "\\/", "/", -1) flags := "" @@ -1623,7 +1624,7 @@ L: attrItemsAgo = 0 } else if expectLang { if val == "*" { - return nil, x.Errorf( + return nil, errors.Errorf( "The * symbol cannot be used as a valid language inside functions") } function.Lang = val @@ -2074,7 +2075,7 @@ func parseID(val string) ([]uint64, error) { } if val[len(val)-1] != ']' { - return nil, x.Errorf("Invalid id list at root. Got: %+v", val) + return nil, errors.Errorf("Invalid id list at root. Got: %+v", val) } var buf bytes.Buffer for _, c := range val[1:] { @@ -2091,7 +2092,7 @@ func parseID(val string) ([]uint64, error) { continue } if c == '[' || c == ')' { - return nil, x.Errorf("Invalid id list at root. Got: %+v", val) + return nil, errors.Errorf("Invalid id list at root. Got: %+v", val) } buf.WriteRune(c) } @@ -2246,7 +2247,7 @@ func parseLanguageList(it *lex.ItemIterator) ([]string, error) { for _, lang := range langs { if lang == string(star) && len(langs) > 1 { - return nil, x.Errorf( + return nil, errors.Errorf( "If * is used, no other languages are allowed in the language list. Found %v", langs) } diff --git a/gql/parser_mutation.go b/gql/parser_mutation.go index 50366e1f0d0..6ebaee6125b 100644 --- a/gql/parser_mutation.go +++ b/gql/parser_mutation.go @@ -17,11 +17,9 @@ package gql import ( - "errors" - "github.com/dgraph-io/dgo/protos/api" "github.com/dgraph-io/dgraph/lex" - "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) func ParseMutation(mutation string) (*api.Mutation, error) { @@ -35,7 +33,7 @@ func ParseMutation(mutation string) (*api.Mutation, error) { } item := it.Item() if item.Typ != itemLeftCurl { - return nil, x.Errorf("Expected { at the start of block. Got: [%s]", item.Val) + return nil, errors.Errorf("Expected { at the start of block. Got: [%s]", item.Val) } for it.Next() { @@ -46,7 +44,7 @@ func ParseMutation(mutation string) (*api.Mutation, error) { if item.Typ == itemRightCurl { // mutations must be enclosed in a single block. if it.Next() && it.Item().Typ != lex.ItemEOF { - return nil, x.Errorf("Unexpected %s after the end of the block.", it.Item().Val) + return nil, errors.Errorf("Unexpected %s after the end of the block.", it.Item().Val) } return &mu, nil } @@ -56,7 +54,7 @@ func ParseMutation(mutation string) (*api.Mutation, error) { } } } - return nil, x.Errorf("Invalid mutation.") + return nil, errors.Errorf("Invalid mutation.") } // parseMutationOp parses and stores set or delete operation string in Mutation. @@ -69,29 +67,29 @@ func parseMutationOp(it *lex.ItemIterator, op string, mu *api.Mutation) error { } if item.Typ == itemLeftCurl { if parse { - return x.Errorf("Too many left curls in set mutation.") + return errors.Errorf("Too many left curls in set mutation.") } parse = true } if item.Typ == itemMutationContent { if !parse { - return x.Errorf("Mutation syntax invalid.") + return errors.Errorf("Mutation syntax invalid.") } if op == "set" { mu.SetNquads = []byte(item.Val) } else if op == "delete" { mu.DelNquads = []byte(item.Val) } else if op == "schema" { - return x.Errorf("Altering schema not supported through http client.") + return errors.Errorf("Altering schema not supported through http client.") } else if op == "dropall" { - return x.Errorf("Dropall not supported through http client.") + return errors.Errorf("Dropall not supported through http client.") } else { - return x.Errorf("Invalid mutation operation.") + return errors.Errorf("Invalid mutation operation.") } } if item.Typ == itemRightCurl { return nil } } - return x.Errorf("Invalid mutation formatting.") + return errors.Errorf("Invalid mutation formatting.") } diff --git a/lex/lexer.go b/lex/lexer.go index 55930320101..d05b7cedfc4 100644 --- a/lex/lexer.go +++ b/lex/lexer.go @@ -17,11 +17,11 @@ package lex import ( - "errors" "fmt" "unicode/utf8" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) const EOF = -1 @@ -120,7 +120,7 @@ func (p *ItemIterator) Save() int { // Peek returns the next n items without consuming them. func (p *ItemIterator) Peek(num int) ([]Item, error) { if (p.idx + num + 1) > len(p.l.items) { - return nil, x.Errorf("Out of range for peek") + return nil, errors.Errorf("Out of range for peek") } return p.l.items[p.idx+1 : p.idx+num+1], nil } @@ -378,17 +378,17 @@ func (l *Lexer) LexQuotedString() error { l.Backup() r := l.Next() if r != quote { - return x.Errorf("String should start with quote.") + return errors.Errorf("String should start with quote.") } for { r := l.Next() if r == EOF { - return x.Errorf("Unexpected end of input.") + return errors.Errorf("Unexpected end of input.") } if r == '\\' { r := l.Next() if !l.IsEscChar(r) { - return x.Errorf("Not a valid escape char: '%c'", r) + return errors.Errorf("Not a valid escape char: '%c'", r) } continue // eat the next char } diff --git a/posting/index.go b/posting/index.go index eba436fd631..81b95859666 100644 --- a/posting/index.go +++ b/posting/index.go @@ -34,6 +34,7 @@ import ( "github.com/dgraph-io/dgraph/tok" "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) var emptyCountParams countParams @@ -53,11 +54,11 @@ func indexTokens(info *indexMutationInfo) ([]string, error) { schemaType, err := schema.State().TypeOf(attr) if err != nil || !schemaType.IsScalar() { - return nil, x.Errorf("Cannot index attribute %s of type object.", attr) + return nil, errors.Errorf("Cannot index attribute %s of type object.", attr) } if !schema.State().IsIndexed(attr) { - return nil, x.Errorf("Attribute %s is not indexed.", attr) + return nil, errors.Errorf("Attribute %s is not indexed.", attr) } sv, err := types.Convert(info.val, schemaType) if err != nil { @@ -363,7 +364,7 @@ func (txn *Txn) addMutationHelper(ctx context.Context, l *List, doUpdateIndex bo func (l *List) AddMutationWithIndex(ctx context.Context, edge *pb.DirectedEdge, txn *Txn) error { if len(edge.Attr) == 0 { - return x.Errorf("Predicate cannot be empty for edge with subject: [%v], object: [%v]"+ + return errors.Errorf("Predicate cannot be empty for edge with subject: [%v], object: [%v]"+ " and value: [%v]", edge.Entity, edge.ValueId, edge.Value) } diff --git a/posting/list.go b/posting/list.go index c2155e7bdf5..d138bc306e8 100644 --- a/posting/list.go +++ b/posting/list.go @@ -19,7 +19,6 @@ package posting import ( "bytes" "context" - "errors" "fmt" "log" "math" @@ -38,6 +37,7 @@ import ( "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/types/facets" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) var ( @@ -553,7 +553,7 @@ func (l *List) iterate(readTs uint64, afterUid uint64, f func(obj *pb.Posting) e deleteBelowTs, mposts := l.pickPostings(readTs) if readTs < l.minTs { - return x.Errorf("readTs: %d less than minTs: %d for key: %q", readTs, l.minTs, l.key) + return errors.Errorf("readTs: %d less than minTs: %d for key: %q", readTs, l.minTs, l.key) } midx, mlen := 0, len(mposts) diff --git a/posting/mvcc.go b/posting/mvcc.go index 5b9a44ed987..2bf15f2f862 100644 --- a/posting/mvcc.go +++ b/posting/mvcc.go @@ -28,11 +28,12 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" farm "github.com/dgryski/go-farm" + "github.com/pkg/errors" ) var ( // ErrTsTooOld is returned when a transaction is too old to be applied. - ErrTsTooOld = x.Errorf("Transaction is too old") + ErrTsTooOld = errors.Errorf("Transaction is too old") ) // ShouldAbort returns whether the transaction should be aborted. @@ -181,10 +182,10 @@ func ReadPostingList(key []byte, it *badger.Iterator) (*List, error) { return nil, err } case BitSchemaPosting: - return nil, x.Errorf( + return nil, errors.Errorf( "Trying to read schema in ReadPostingList for key: %s", hex.Dump(key)) default: - return nil, x.Errorf( + return nil, errors.Errorf( "Unexpected meta: %d for key: %s", item.UserMeta(), hex.Dump(key)) } if item.DiscardEarlierVersions() { diff --git a/query/aggregator.go b/query/aggregator.go index 0fbb1b389cd..71055eeaeb3 100644 --- a/query/aggregator.go +++ b/query/aggregator.go @@ -24,6 +24,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) type aggregator struct { @@ -58,7 +59,7 @@ func convertTo(from *pb.TaskValue) (types.Val, error) { } va, err := types.Convert(vh, vh.Tid) if err != nil { - return vh, x.Wrapf(err, "Fail to convert from api.Value to types.Val") + return vh, errors.Wrapf(err, "Fail to convert from api.Value to types.Val") } return va, err } @@ -107,7 +108,7 @@ func compareValues(ag string, va, vb types.Val) (bool, error) { case "!=": return !isEqual, nil } - return false, x.Errorf("Invalid compare function %v", ag) + return false, errors.Errorf("Invalid compare function %q", ag) } func (ag *aggregator) ApplyVal(v types.Val) error { @@ -135,37 +136,37 @@ func (ag *aggregator) ApplyVal(v types.Val) error { switch ag.name { case "ln": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } v.Value = math.Log(l) res = v case "exp": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } v.Value = math.Exp(l) res = v case "u-": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } v.Value = -l res = v case "sqrt": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } v.Value = math.Sqrt(l) res = v case "floor": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } v.Value = math.Floor(l) res = v case "ceil": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } v.Value = math.Ceil(l) res = v @@ -174,7 +175,7 @@ func (ag *aggregator) ApplyVal(v types.Val) error { v.Value = float64(time.Since(v.Value.(time.Time))) / 1000000000.0 v.Tid = types.FloatID } else { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } res = v } @@ -194,43 +195,43 @@ func (ag *aggregator) ApplyVal(v types.Val) error { switch ag.name { case "+": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } va.Value = va.Value.(float64) + l res = va case "-": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } va.Value = va.Value.(float64) - l res = va case "*": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } va.Value = va.Value.(float64) * l res = va case "/": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v %v %v", ag.name, va.Tid, v.Tid) + return errors.Errorf("Wrong type encountered for func %q %q %q", ag.name, va.Tid, v.Tid) } if l == 0 { - return x.Errorf("Division by zero") + return errors.Errorf("Division by zero") } va.Value = va.Value.(float64) / l res = va case "%": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } if l == 0 { - return x.Errorf("Division by zero") + return errors.Errorf("Division by zero") } va.Value = math.Mod(va.Value.(float64), l) res = va case "pow": if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } va.Value = math.Pow(va.Value.(float64), l) res = va @@ -239,7 +240,7 @@ func (ag *aggregator) ApplyVal(v types.Val) error { return nil } if !isIntOrFloat { - return x.Errorf("Wrong type encountered for func %v", ag.name) + return errors.Errorf("Wrong type encountered for func %q", ag.name) } va.Value = math.Log(va.Value.(float64)) / math.Log(l) res = va @@ -258,7 +259,7 @@ func (ag *aggregator) ApplyVal(v types.Val) error { res = va } default: - return x.Errorf("Unhandled aggregator function %v", ag.name) + return errors.Errorf("Unhandled aggregator function %q", ag.name) } ag.result = res return nil diff --git a/query/groupby.go b/query/groupby.go index 726aee63ea5..b35228dbd5e 100644 --- a/query/groupby.go +++ b/query/groupby.go @@ -24,7 +24,7 @@ import ( "github.com/dgraph-io/dgraph/algo" "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/types" - "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) type groupPair struct { @@ -43,7 +43,7 @@ func (grp *groupResult) aggregateChild(child *SubGraph) error { fieldName := child.Params.Alias if child.Params.DoCount { if child.Attr != "uid" { - return x.Errorf("Only uid predicate is allowed in count within groupby") + return errors.Errorf("Only uid predicate is allowed in count within groupby") } if fieldName == "" { fieldName = "count" @@ -348,12 +348,12 @@ func (sg *SubGraph) fillGroupedVars(doneVars map[string]varValue, path []*SubGra continue } if len(grp.keys) > 1 { - return x.Errorf("Expected one UID for var in groupby but got: %d", len(grp.keys)) + return errors.Errorf("Expected one UID for var in groupby but got: %d", len(grp.keys)) } uidVal := grp.keys[0].key.Value uid, ok := uidVal.(uint64) if !ok { - return x.Errorf("Vars can be assigned only when grouped by UID attribute") + return errors.Errorf("Vars can be assigned only when grouped by UID attribute") } // grp.aggregates could be empty if schema conversion failed during aggregation if len(grp.aggregates) > 0 { diff --git a/query/math.go b/query/math.go index 2b6319e6291..8502b4eda76 100644 --- a/query/math.go +++ b/query/math.go @@ -18,8 +18,8 @@ package query import ( "github.com/dgraph-io/dgraph/types" - "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" ) type mathTree struct { @@ -160,7 +160,7 @@ func processBinaryBoolean(mNode *mathTree) error { } res, err := compareValues(aggName, val, curVal) if err != nil { - return x.Wrapf(err, "Wrong values in comparison function.") + return errors.Wrapf(err, "Wrong values in comparison function.") } destMap[k] = types.Val{ Tid: types.BoolID, @@ -177,7 +177,7 @@ func processTernary(mNode *mathTree) error { aggName := mNode.Fn condMap := mNode.Child[0].Val if len(condMap) == 0 { - return x.Errorf("Expected a value variable in %v but missing.", aggName) + return errors.Errorf("Expected a value variable in %v but missing.", aggName) } varOne := mNode.Child[1].Val varTwo := mNode.Child[2].Val @@ -187,7 +187,7 @@ func processTernary(mNode *mathTree) error { var res types.Val v, ok := val.Value.(bool) if !ok { - return x.Errorf("First variable of conditional function not a bool value") + return errors.Errorf("First variable of conditional function not a bool value") } if v { // Pick the value of first map. @@ -233,7 +233,7 @@ func evalMathTree(mNode *mathTree) error { aggName := mNode.Fn if isUnary(aggName) { if len(mNode.Child) != 1 { - return x.Errorf("Function %v expects 1 argument. But got: %v", aggName, + return errors.Errorf("Function %v expects 1 argument. But got: %v", aggName, len(mNode.Child)) } return processUnary(mNode) @@ -241,7 +241,7 @@ func evalMathTree(mNode *mathTree) error { if isBinary(aggName) { if len(mNode.Child) != 2 { - return x.Errorf("Function %v expects 2 argument. But got: %v", aggName, + return errors.Errorf("Function %v expects 2 argument. But got: %v", aggName, len(mNode.Child)) } return processBinary(mNode) @@ -249,7 +249,7 @@ func evalMathTree(mNode *mathTree) error { if isBinaryBoolean(aggName) { if len(mNode.Child) != 2 { - return x.Errorf("Function %v expects 2 argument. But got: %v", aggName, + return errors.Errorf("Function %v expects 2 argument. But got: %v", aggName, len(mNode.Child)) } return processBinaryBoolean(mNode) @@ -257,11 +257,11 @@ func evalMathTree(mNode *mathTree) error { if isTernary(aggName) { if len(mNode.Child) != 3 { - return x.Errorf("Function %v expects 3 argument. But got: %v", aggName, + return errors.Errorf("Function %v expects 3 argument. But got: %v", aggName, len(mNode.Child)) } return processTernary(mNode) } - return x.Errorf("Unhandled Math operator: %v", aggName) + return errors.Errorf("Unhandled Math operator: %v", aggName) } diff --git a/query/mutation.go b/query/mutation.go index 414393bf32a..8f0225e5f72 100644 --- a/query/mutation.go +++ b/query/mutation.go @@ -18,7 +18,6 @@ package query import ( "context" - "errors" "strings" "time" @@ -31,6 +30,7 @@ import ( "github.com/dgraph-io/dgraph/worker" "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" ) // ApplyMutations performs the required edge expansions and forwards the results to the @@ -38,7 +38,7 @@ import ( func ApplyMutations(ctx context.Context, m *pb.Mutations) (*api.TxnContext, error) { edges, err := expandEdges(ctx, m) if err != nil { - return nil, x.Wrapf(err, "While adding pb.edges") + return nil, errors.Wrapf(err, "While adding pb.edges") } m.Edges = edges @@ -98,7 +98,7 @@ func verifyUid(ctx context.Context, uid uint64) error { return nil } if time.Now().After(deadline) { - err := x.Errorf("Uid: [%d] cannot be greater than lease: [%d]", uid, lease) + err := errors.Errorf("Uid: [%d] cannot be greater than lease: [%d]", uid, lease) glog.V(2).Infof("verifyUid returned error: %v", err) return err } @@ -122,7 +122,7 @@ func AssignUids(ctx context.Context, nquads []*api.NQuad) (map[string]uint64, er } if len(nq.Subject) == 0 { - return nil, x.Errorf("Subject must not be empty for nquad: %+v", nq) + return nil, errors.Errorf("Subject must not be empty for nquad: %+v", nq) } var uid uint64 if strings.HasPrefix(nq.Subject, "_:") { @@ -182,7 +182,7 @@ func ToDirectedEdges(gmu *gql.Mutation, var edge *pb.DirectedEdge edge, err = wnq.ToEdgeUsing(newUids) if err != nil { - return x.Wrap(err) + return errors.Wrap(err, "") } edge.Op = op edges = append(edges, edge) diff --git a/query/outputnode.go b/query/outputnode.go index 8cfa229b82d..854ddd2a536 100644 --- a/query/outputnode.go +++ b/query/outputnode.go @@ -19,12 +19,12 @@ package query import ( "bytes" "encoding/json" - "errors" "fmt" "sort" "strings" "time" + "github.com/pkg/errors" geom "github.com/twpayne/go-geom" "github.com/twpayne/go-geom/encoding/geojson" @@ -279,7 +279,8 @@ func merge(parent [][]*fastJsonNode, child [][]*fastJsonNode) ([][]*fastJsonNode for _, ca := range child { cnt += len(pa) + len(ca) if cnt > x.Config.NormalizeNodeLimit { - return nil, x.Errorf("Couldn't evaluate @normalize directive - too many results") + return nil, errors.Errorf( + "Couldn't evaluate @normalize directive - too many results") } list := make([]*fastJsonNode, 0, len(pa)+len(ca)) list = append(list, pa...) @@ -398,7 +399,7 @@ func (fj *fastJsonNode) addAggregations(sg *SubGraph) error { aggVal, ok := child.Params.uidToVal[0] if !ok { if len(child.Params.NeedsVar) == 0 { - return x.Errorf("Only aggregated variables allowed within empty block.") + return errors.Errorf("Only aggregated variables allowed within empty block.") } // the aggregation didn't happen, most likely was called with unset vars. // See: query.go:fillVars @@ -437,7 +438,7 @@ func processNodeUids(fj *fastJsonNode, sg *SubGraph) error { if sg.Params.isGroupBy { if len(sg.GroupbyRes) == 0 { - return x.Errorf("Expected GroupbyRes to have length > 0.") + return errors.Errorf("Expected GroupbyRes to have length > 0.") } fj.addGroupby(sg, sg.GroupbyRes[0], sg.Params.Alias) return nil diff --git a/query/query.go b/query/query.go index ce7c9548012..b6939a7c968 100644 --- a/query/query.go +++ b/query/query.go @@ -422,7 +422,7 @@ func (sg *SubGraph) preTraverse(uid uint64, dst outputNode) error { continue } if len(pc.facetsMatrix) > 0 && len(pc.facetsMatrix) != len(pc.uidMatrix) { - return x.Errorf("Length of facetsMatrix and uidMatrix mismatch: %d vs %d", + return errors.Errorf("Length of facetsMatrix and uidMatrix mismatch: %d vs %d", len(pc.facetsMatrix), len(pc.uidMatrix)) } @@ -432,7 +432,7 @@ func (sg *SubGraph) preTraverse(uid uint64, dst outputNode) error { } if pc.Params.isGroupBy { if len(pc.GroupbyRes) <= idx { - return x.Errorf("Unexpected length while adding Groupby. Idx: [%v], len: [%v]", + return errors.Errorf("Unexpected length while adding Groupby. Idx: [%v], len: [%v]", idx, len(pc.GroupbyRes)) } dst.addGroupby(pc, pc.GroupbyRes[idx], pc.fieldName()) @@ -547,7 +547,7 @@ func (sg *SubGraph) preTraverse(uid uint64, dst outputNode) error { if pc.Params.expandAll && len(pc.LangTags[idx].Lang) != 0 { if i >= len(pc.LangTags[idx].Lang) { - return x.Errorf( + return errors.Errorf( "pb.error: all lang tags should be either present or absent") } fieldNameWithTag := fieldName @@ -602,7 +602,7 @@ func convertWithBestEffort(tv *pb.TaskValue, attr string) (types.Val, error) { // value would be in binary format with appropriate type v, _ := getValue(tv) if !v.Tid.IsScalar() { - return v, x.Errorf("Leaf predicate:'%v' must be a scalar.", attr) + return v, errors.Errorf("Leaf predicate:'%v' must be a scalar.", attr) } // creates appropriate type from binary format @@ -635,7 +635,7 @@ func filterCopy(sg *SubGraph, ft *gql.FilterTree) error { } else { sg.Attr = ft.Func.Attr if !isValidFuncName(ft.Func.Name) { - return x.Errorf("Invalid function name: %s", ft.Func.Name) + return errors.Errorf("Invalid function name: %s", ft.Func.Name) } if isUidFnWithoutVar(ft.Func) { @@ -645,7 +645,7 @@ func filterCopy(sg *SubGraph, ft *gql.FilterTree) error { } } else { if ft.Func.Attr == "uid" { - return x.Errorf(`Argument cannot be "uid"`) + return errors.Errorf(`Argument cannot be "uid"`) } sg.createSrcFunction(ft.Func) sg.Params.NeedsVar = append(sg.Params.NeedsVar, ft.Func.NeedsVar...) @@ -707,7 +707,7 @@ func treeCopy(gq *gql.GraphQuery, sg *SubGraph) error { for _, gchild := range gq.Children { if sg.Params.Alias == "shortest" && gchild.Expand != "" { - return x.Errorf("expand() not allowed inside shortest") + return errors.Errorf("expand() not allowed inside shortest") } key := "" @@ -717,7 +717,7 @@ func treeCopy(gq *gql.GraphQuery, sg *SubGraph) error { key = uniqueKey(gchild) } if _, ok := attrsSeen[key]; ok { - return x.Errorf("%s not allowed multiple times in same sub-query.", + return errors.Errorf("%s not allowed multiple times in same sub-query.", key) } attrsSeen[key] = struct{}{} @@ -753,7 +753,7 @@ func treeCopy(gq *gql.GraphQuery, sg *SubGraph) error { for argk := range gchild.Args { if !isValidArg(argk) { - return x.Errorf("Invalid argument: %s", argk) + return errors.Errorf("Invalid argument: %s", argk) } } if err := args.fill(gchild); err != nil { @@ -761,7 +761,7 @@ func treeCopy(gq *gql.GraphQuery, sg *SubGraph) error { } if len(args.Order) != 0 && len(args.FacetOrder) != 0 { - return x.Errorf("Cannot specify order at both args and facets") + return errors.Errorf("Cannot specify order at both args and facets") } dst := &SubGraph{ @@ -779,19 +779,19 @@ func treeCopy(gq *gql.GraphQuery, sg *SubGraph) error { if gchild.Func != nil && (gchild.Func.IsAggregator() || gchild.Func.IsPasswordVerifier()) { if len(gchild.Children) != 0 { - return x.Errorf("Node with %q cant have child attr", gchild.Func.Name) + return errors.Errorf("Node with %q cant have child attr", gchild.Func.Name) } // embedded filter will cause ambiguous output like following, // director.film @filter(gt(initial_release_date, "2016")) { // min(initial_release_date @filter(gt(initial_release_date, "1986")) // } if gchild.Filter != nil { - return x.Errorf( + return errors.Errorf( "Node with %q cant have filter, please place the filter on the upper level", gchild.Func.Name) } if gchild.Func.Attr == "uid" { - return x.Errorf(`Argument cannot be "uid"`) + return errors.Errorf(`Argument cannot be "uid"`) } dst.createSrcFunction(gchild.Func) } @@ -970,7 +970,7 @@ func newGraph(ctx context.Context, gq *gql.GraphQuery) (*SubGraph, error) { for argk := range gq.Args { if !isValidArg(argk) { - return nil, x.Errorf("Invalid argument: %s", argk) + return nil, errors.Errorf("Invalid argument: %s", argk) } } if err := args.fill(gq); err != nil { @@ -986,11 +986,11 @@ func newGraph(ctx context.Context, gq *gql.GraphQuery) (*SubGraph, error) { } else { // Disallow uid as attribute - issue#3110 if len(gq.Func.UID) == 0 { - return nil, x.Errorf(`Argument cannot be "uid"`) + return nil, errors.Errorf(`Argument cannot be "uid"`) } } if !isValidFuncName(gq.Func.Name) { - return nil, x.Errorf("Invalid function name: %s", gq.Func.Name) + return nil, errors.Errorf("Invalid function name: %s", gq.Func.Name) } sg.createSrcFunction(gq.Func) @@ -1025,7 +1025,7 @@ func toFacetsFilter(gft *gql.FilterTree) (*pb.FilterTree, error) { return nil, nil } if gft.Func != nil && len(gft.Func.NeedsVar) != 0 { - return nil, x.Errorf("Variables not supported in pb.FilterTree") + return nil, errors.Errorf("Variables not supported in pb.FilterTree") } ftree := &pb.FilterTree{Op: gft.Op} for _, gftc := range gft.Child { @@ -1064,7 +1064,7 @@ func createTaskQuery(sg *SubGraph) (*pb.Query, error) { for _, arg := range sg.SrcFunc.Args { srcFunc.Args = append(srcFunc.Args, arg.Value) if arg.IsValueVar { - return nil, x.Errorf("Unsupported use of value var") + return nil, errors.Errorf("Unsupported use of value var") } } } @@ -1158,7 +1158,7 @@ func evalLevelAgg( } } if relSG == nil { - return nil, x.Errorf("Invalid variable aggregation. Check the levels.") + return nil, errors.Errorf("Invalid variable aggregation. Check the levels.") } vals := doneVars[needsVar].Vals @@ -1230,7 +1230,7 @@ func (fromNode *varValue) transformTo(toPath []*SubGraph) (map[uint64]types.Val, continue } if curVal.Tid != types.IntID && curVal.Tid != types.FloatID { - return nil, x.Errorf("Encountered non int/float type for summing") + return nil, errors.Errorf("Encountered non int/float type for summing") } for j := 0; j < len(ul.Uids); j++ { dstUid := ul.Uids[j] @@ -1368,7 +1368,7 @@ func (sg *SubGraph) valueVarAggregation(doneVars map[string]varValue, path []*Su // The value var can be empty. No need to check for nil. sg.Params.uidToVal = srcMap.Vals } else { - return x.Errorf("Unhandled pb.node %v with parent %v", sg.Attr, parent.Attr) + return errors.Errorf("Unhandled pb.node %v with parent %v", sg.Attr, parent.Attr) } return nil @@ -1568,7 +1568,7 @@ func (sg *SubGraph) populateUidValVar(doneVars map[string]varValue, sgPath []*Su for idx, uid := range sg.SrcUIDs.Uids { if len(sg.valueMatrix[idx].Values) > 1 { - return x.Errorf("Value variables not supported for predicate with list type.") + return errors.Errorf("Value variables not supported for predicate with list type.") } if len(sg.valueMatrix[idx].Values) == 0 { @@ -1640,7 +1640,7 @@ func (sg *SubGraph) populateFacetVars(doneVars map[string]varValue, sgPath []*Su } if nVal.Tid != types.IntID && nVal.Tid != types.FloatID { - return x.Errorf("Repeated id with non int/float value for facet var encountered.") + return errors.Errorf("Repeated id with non int/float value for facet var encountered.") } ag := aggregator{name: "sum"} ag.Apply(pVal) @@ -1706,7 +1706,7 @@ func (sg *SubGraph) fillVars(mp map[string]varValue) error { lists = append(lists, &pb.List{Uids: uids}) case len(l.Vals) != 0 || l.Uids != nil: - return x.Errorf("Wrong variable type encountered for var(%v) %v.", v.Name, v.Typ) + return errors.Errorf("Wrong variable type encountered for var(%v) %v.", v.Name, v.Typ) default: // This var does not match any uids or vals but we are still trying to access it. @@ -1749,7 +1749,7 @@ func (sg *SubGraph) replaceVarInFunc() error { continue } if len(sg.Params.uidToVal) == 0 { - return x.Errorf("No value found for value variable %q", arg.Value) + return errors.Errorf("No value found for value variable %q", arg.Value) } // We don't care about uids, just take all the values and put as args. // There would be only one value var per subgraph as per current assumptions. @@ -1786,7 +1786,7 @@ func (sg *SubGraph) applyIneqFunc() error { src := types.Val{Tid: types.StringID, Value: []byte(val)} dst, err := types.Convert(src, typ) if err != nil { - return x.Errorf("Invalid argment %v. Comparing with different type", val) + return errors.Errorf("Invalid argment %v. Comparing with different type", val) } if sg.SrcUIDs != nil { for _, uid := range sg.SrcUIDs.Uids { @@ -1950,7 +1950,7 @@ func expandSubgraph(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) { for _, ch := range sg.Children { if ch.isSimilar(temp) { - return out, x.Errorf("Repeated subgraph: [%s] while using expand()", ch.Attr) + return out, errors.Errorf("Repeated subgraph: [%s] while using expand()", ch.Attr) } } out = append(out, temp) @@ -2014,7 +2014,7 @@ func ProcessGraph(ctx context.Context, sg, parent *SubGraph, rch chan error) { if sg.SrcUIDs == nil { glog.Errorf("SrcUIDs is unexpectedly nil. Subgraph: %+v", sg) - rch <- x.Errorf("SrcUIDs shouldn't be nil.") + rch <- errors.Errorf("SrcUIDs shouldn't be nil.") return } // If we have a filter SubGraph which only contains an operator, @@ -2348,7 +2348,7 @@ func (sg *SubGraph) sortAndPaginateUsingFacet(ctx context.Context) error { return nil } if len(sg.facetsMatrix) != len(sg.uidMatrix) { - return x.Errorf("Facet matrix and UID matrix mismatch: %d vs %d", + return errors.Errorf("Facet matrix and UID matrix mismatch: %d vs %d", len(sg.facetsMatrix), len(sg.uidMatrix)) } orderby := sg.Params.FacetOrder @@ -2410,7 +2410,7 @@ func (sg *SubGraph) sortAndPaginateUsingFacet(ctx context.Context) error { func (sg *SubGraph) sortAndPaginateUsingVar(ctx context.Context) error { if len(sg.Params.uidToVal) == 0 { - return x.Errorf("Variable: [%s] used before definition.", sg.Params.Order[0].Attr) + return errors.Errorf("Variable: [%s] used before definition.", sg.Params.Order[0].Attr) } for i := 0; i < len(sg.uidMatrix); i++ { @@ -2618,7 +2618,7 @@ func (req *Request) ProcessQuery(ctx context.Context) (err error) { if gq == nil || (len(gq.UID) == 0 && gq.Func == nil && len(gq.NeedsVar) == 0 && gq.Alias != "shortest" && !gq.IsEmpty) { - return x.Errorf("Invalid query. No function used at root and no aggregation" + + return errors.Errorf("Invalid query. No function used at root and no aggregation" + " or math variables found in the body.") } sg, err := ToSubGraph(ctx, gq) @@ -2733,7 +2733,7 @@ func (req *Request) ProcessQuery(ctx context.Context) (err error) { // Ensure all the queries are executed. for _, it := range hasExecuted { if !it { - return x.Errorf("Query couldn't be executed") + return errors.Errorf("Query couldn't be executed") } } req.Latency.Processing += time.Since(execStart) diff --git a/query/recurse.go b/query/recurse.go index a2651f2b205..a381a582378 100644 --- a/query/recurse.go +++ b/query/recurse.go @@ -18,12 +18,12 @@ package query import ( "context" - "errors" "fmt" "math" "github.com/dgraph-io/dgraph/algo" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) func (start *SubGraph) expandRecurse(ctx context.Context, maxDepth uint64) error { @@ -163,7 +163,7 @@ func (start *SubGraph) expandRecurse(ctx context.Context, maxDepth uint64) error if numEdges > x.Config.QueryEdgeLimit { // If we've seen too many edges, stop the query. - return x.Errorf("Exceeded query edge limit = %v. Found %v edges.", + return errors.Errorf("Exceeded query edge limit = %v. Found %v edges.", x.Config.QueryEdgeLimit, numEdges) } @@ -201,13 +201,13 @@ func expandChildren(ctx context.Context, sg *SubGraph, children []*SubGraph) ([] func recurse(ctx context.Context, sg *SubGraph) error { if !sg.Params.Recurse { - return x.Errorf("Invalid recurse path query") + return errors.Errorf("Invalid recurse path query") } depth := sg.Params.RecurseArgs.Depth if depth == 0 { if sg.Params.RecurseArgs.AllowLoop { - return x.Errorf("Depth must be > 0 when loop is true for recurse query") + return errors.Errorf("Depth must be > 0 when loop is true for recurse query") } // If no depth is specified, expand till we reach all leaf nodes // or we see reach too many nodes. @@ -216,7 +216,7 @@ func recurse(ctx context.Context, sg *SubGraph) error { for _, child := range sg.Children { if len(child.Children) > 0 { - return x.Errorf( + return errors.Errorf( "recurse queries require that all predicates are specified in one level") } } diff --git a/query/shortest.go b/query/shortest.go index 67956295c26..d184d72185d 100644 --- a/query/shortest.go +++ b/query/shortest.go @@ -27,6 +27,7 @@ import ( "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/types/facets" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) type pathInfo struct { @@ -54,8 +55,8 @@ var pathPool = sync.Pool{ }, } -var errStop = x.Errorf("STOP") -var errFacet = x.Errorf("Skip the edge") +var errStop = errors.Errorf("STOP") +var errFacet = errors.Errorf("Skip the edge") type priorityQueue []*queueItem @@ -114,7 +115,7 @@ func (sg *SubGraph) getCost(matrix, list int) (cost float64, return cost, fcs, rerr } if len(fcs.Facets) > 1 { - rerr = x.Errorf("Expected 1 but got %d facets", len(fcs.Facets)) + rerr = errors.Errorf("Expected 1 but got %d facets", len(fcs.Facets)) return cost, fcs, rerr } tv, err := facets.ValFor(fcs.Facets[0]) @@ -208,7 +209,7 @@ func (sg *SubGraph) expandOut(ctx context.Context, if numEdges > x.Config.QueryEdgeLimit { // If we've seen too many edges, stop the query. - rch <- x.Errorf("Exceeded query edge limit = %v. Found %v edges.", + rch <- errors.Errorf("Exceeded query edge limit = %v. Found %v edges.", x.Config.QueryEdgeLimit, numEdges) return } @@ -264,7 +265,7 @@ func (sg *SubGraph) copyFiltersRecurse(otherSubgraph *SubGraph) { func runKShortestPaths(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) { var err error if sg.Params.Alias != "shortest" { - return nil, x.Errorf("Invalid shortest path query") + return nil, errors.Errorf("Invalid shortest path query") } numPaths := sg.Params.numPaths @@ -421,7 +422,7 @@ func runKShortestPaths(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) { func shortestPath(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) { var err error if sg.Params.Alias != "shortest" { - return nil, x.Errorf("Invalid shortest path query") + return nil, errors.Errorf("Invalid shortest path query") } numPaths := sg.Params.numPaths if numPaths == 0 { diff --git a/raftwal/storage.go b/raftwal/storage.go index 657ca6531e0..5dba764e1fe 100644 --- a/raftwal/storage.go +++ b/raftwal/storage.go @@ -19,19 +19,18 @@ package raftwal import ( "bytes" "encoding/binary" - "errors" "math" "sync" "github.com/dgraph-io/badger" + "github.com/dgraph-io/dgraph/protos/pb" + "github.com/dgraph-io/dgraph/x" "github.com/gogo/protobuf/proto" "github.com/golang/glog" + "github.com/pkg/errors" "go.etcd.io/etcd/raft" "go.etcd.io/etcd/raft/raftpb" "golang.org/x/net/trace" - - "github.com/dgraph-io/dgraph/protos/pb" - "github.com/dgraph-io/dgraph/x" ) type DiskStorage struct { @@ -341,7 +340,7 @@ func (w *DiskStorage) setSnapshot(batch *badger.WriteBatch, s raftpb.Snapshot) e } data, err := s.Marshal() if err != nil { - return x.Wrapf(err, "wal.Store: While marshal snapshot") + return errors.Wrapf(err, "wal.Store: While marshal snapshot") } if err := batch.Set(w.snapshotKey(), data, 0); err != nil { return err @@ -377,7 +376,7 @@ func (w *DiskStorage) setHardState(batch *badger.WriteBatch, st raftpb.HardState } data, err := st.Marshal() if err != nil { - return x.Wrapf(err, "wal.Store: While marshal hardstate") + return errors.Wrapf(err, "wal.Store: While marshal hardstate") } return batch.Set(w.HardStateKey(), data, 0) } @@ -397,7 +396,7 @@ func (w *DiskStorage) reset(es []raftpb.Entry) error { for _, e := range es { data, err := e.Marshal() if err != nil { - return x.Wrapf(err, "wal.Store: While marshal entry") + return errors.Wrapf(err, "wal.Store: While marshal entry") } k := w.EntryKey(e.Index) if err := batch.Set(k, data, 0); err != nil { @@ -661,7 +660,7 @@ func (w *DiskStorage) addEntries(batch *badger.WriteBatch, entries []raftpb.Entr k := w.EntryKey(e.Index) data, err := e.Marshal() if err != nil { - return x.Wrapf(err, "wal.Append: While marshal entry") + return errors.Wrapf(err, "wal.Append: While marshal entry") } if err := batch.Set(k, data, 0); err != nil { return err diff --git a/schema/parse.go b/schema/parse.go index 7ef3bd12e6a..72607d9756e 100644 --- a/schema/parse.go +++ b/schema/parse.go @@ -24,6 +24,7 @@ import ( "github.com/dgraph-io/dgraph/tok" "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) // ParseBytes parses the byte array which holds the schema. We will reset @@ -246,7 +247,7 @@ func resolveTokenizers(updates []*pb.SchemaUpdate) error { if (typ == types.UidID || typ == types.DefaultID || typ == types.PasswordID) && schema.Directive == pb.SchemaUpdate_INDEX { - return x.Errorf("Indexing not allowed on predicate %s of type %s", + return errors.Errorf("Indexing not allowed on predicate %s of type %s", schema.Predicate, typ.Name()) } @@ -255,10 +256,10 @@ func resolveTokenizers(updates []*pb.SchemaUpdate) error { } if len(schema.Tokenizer) == 0 && schema.Directive == pb.SchemaUpdate_INDEX { - return x.Errorf("Require type of tokenizer for pred: %s of type: %s for indexing.", + return errors.Errorf("Require type of tokenizer for pred: %s of type: %s for indexing.", schema.Predicate, typ.Name()) } else if len(schema.Tokenizer) > 0 && schema.Directive != pb.SchemaUpdate_INDEX { - return x.Errorf("Tokenizers present without indexing on attr %s", schema.Predicate) + return errors.Errorf("Tokenizers present without indexing on attr %s", schema.Predicate) } // check for valid tokeniser types and duplicates var seen = make(map[string]bool) @@ -266,22 +267,22 @@ func resolveTokenizers(updates []*pb.SchemaUpdate) error { for _, t := range schema.Tokenizer { tokenizer, has := tok.GetTokenizer(t) if !has { - return x.Errorf("Invalid tokenizer %s", t) + return errors.Errorf("Invalid tokenizer %s", t) } tokenizerType, ok := types.TypeForName(tokenizer.Type()) x.AssertTrue(ok) // Type is validated during tokenizer loading. if tokenizerType != typ { - return x.Errorf("Tokenizer: %s isn't valid for predicate: %s of type: %s", + return errors.Errorf("Tokenizer: %s isn't valid for predicate: %s of type: %s", tokenizer.Name(), schema.Predicate, typ.Name()) } if _, ok := seen[tokenizer.Name()]; !ok { seen[tokenizer.Name()] = true } else { - return x.Errorf("Duplicate tokenizers present for attr %s", schema.Predicate) + return errors.Errorf("Duplicate tokenizers present for attr %s", schema.Predicate) } if tokenizer.IsSortable() { if seenSortableTok { - return x.Errorf("More than one sortable index encountered for: %v", + return errors.Errorf("More than one sortable index encountered for: %v", schema.Predicate) } seenSortableTok = true @@ -334,7 +335,7 @@ func parseTypeDeclaration(it *lex.ItemIterator) (*pb.TypeUpdate, error) { return nil, it.Item().Errorf("Unexpected token. Got %v", it.Item().Val) } } - return nil, x.Errorf("Shouldn't reach here.") + return nil, errors.Errorf("Shouldn't reach here.") } func parseTypeField(it *lex.ItemIterator) (*pb.SchemaUpdate, error) { @@ -437,7 +438,7 @@ func Parse(s string) (*SchemasAndTypes, error) { switch item.Typ { case lex.ItemEOF: if err := resolveTokenizers(result.Schemas); err != nil { - return nil, x.Wrapf(err, "failed to enrich schema") + return nil, errors.Wrapf(err, "failed to enrich schema") } return &result, nil @@ -463,5 +464,5 @@ func Parse(s string) (*SchemasAndTypes, error) { return nil, it.Item().Errorf("Unexpected token: %v while parsing schema", item) } } - return nil, x.Errorf("Shouldn't reach here") + return nil, errors.Errorf("Shouldn't reach here") } diff --git a/schema/schema.go b/schema/schema.go index f7b995e709c..6908ca097d9 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -30,6 +30,7 @@ import ( "github.com/dgraph-io/dgraph/tok" "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) var ( @@ -167,7 +168,7 @@ func (s *state) TypeOf(pred string) (types.TypeID, error) { if schema, ok := s.predicate[pred]; ok { return types.TypeID(schema.ValueType), nil } - return types.UndefinedID, x.Errorf("Schema not defined for predicate: %v.", pred) + return types.UndefinedID, errors.Errorf("Schema not defined for predicate: %v.", pred) } // IsIndexed returns whether the predicate is indexed or not @@ -306,7 +307,7 @@ func Init(ps *badger.DB) { func Load(predicate string) error { if len(predicate) == 0 { - return x.Errorf("Empty predicate") + return errors.Errorf("Empty predicate") } key := x.SchemaKey(predicate) txn := pstore.NewTransactionAt(1, false) diff --git a/systest/cluster_test.go b/systest/cluster_test.go index 64f1c0bd181..60013d66364 100644 --- a/systest/cluster_test.go +++ b/systest/cluster_test.go @@ -33,6 +33,7 @@ import ( "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" "github.com/stretchr/testify/require" ) @@ -88,7 +89,7 @@ func matchExportCount(opts matchExport) error { } expected := `{"code": "Success", "message": "Export completed."}` if string(b) != expected { - return x.Errorf("Unexpected message while exporting: %v", string(b)) + return errors.Errorf("Unexpected message while exporting: %v", string(b)) } dataFile, err := findFile(filepath.Join(opts.dir, "export"), ".rdf.gz") @@ -102,7 +103,7 @@ func matchExportCount(opts matchExport) error { } count := strings.TrimSpace(string(out)) if count != strconv.Itoa(opts.expectedRDF) { - return x.Errorf("Export count mismatch. Got: %s", count) + return errors.Errorf("Export count mismatch. Got: %s", count) } schemaFile, err := findFile(filepath.Join(opts.dir, "export"), ".schema.gz") @@ -116,7 +117,7 @@ func matchExportCount(opts matchExport) error { } count = strings.TrimSpace(string(out)) if count != strconv.Itoa(opts.expectedSchema) { - return x.Errorf("Schema export count mismatch. Got: %s", count) + return errors.Errorf("Schema export count mismatch. Got: %s", count) } glog.Infoln("Export count matched.") return nil @@ -146,13 +147,13 @@ func waitForNodeToBeHealthy(t *testing.T, port int) { func restart(cmd *exec.Cmd) error { cmd.Process.Signal(syscall.SIGINT) if _, err := cmd.Process.Wait(); err != nil { - return x.Errorf("Error while waiting for Dgraph process to be killed: %v", err) + return errors.Wrapf(err, "while waiting for Dgraph process to be killed") } cmd.Process = nil glog.Infoln("Trying to restart Dgraph Alpha") if err := cmd.Start(); err != nil { - return x.Errorf("Couldn't start Dgraph alpha again: %v\n", err) + return errors.Wrapf(err, "couldn't start Dgraph alpha again") } return nil } diff --git a/tok/tok.go b/tok/tok.go index e62399c1a4f..fc9fb7d8481 100644 --- a/tok/tok.go +++ b/tok/tok.go @@ -28,6 +28,7 @@ import ( "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) // Tokenizer identifiers are unique and can't be reused. @@ -287,7 +288,7 @@ func (t ExactTokenizer) Tokens(v interface{}) ([]string, error) { if term, ok := v.(string); ok { return []string{term}, nil } - return nil, x.Errorf("Exact indices only supported for string types") + return nil, errors.Errorf("Exact indices only supported for string types") } func (t ExactTokenizer) Identifier() byte { return IdentExact } func (t ExactTokenizer) IsSortable() bool { return true } @@ -371,7 +372,7 @@ func (t TrigramTokenizer) Type() string { return "string" } func (t TrigramTokenizer) Tokens(v interface{}) ([]string, error) { value, ok := v.(string) if !ok { - return nil, x.Errorf("Trigram indices only supported for string types") + return nil, errors.Errorf("Trigram indices only supported for string types") } l := len(value) - 2 if l > 0 { @@ -395,13 +396,13 @@ func (t HashTokenizer) Type() string { return "string" } func (t HashTokenizer) Tokens(v interface{}) ([]string, error) { term, ok := v.(string) if !ok { - return nil, x.Errorf("Hash tokenizer only supported for string types") + return nil, errors.Errorf("Hash tokenizer only supported for string types") } // Blake2 is a hash function equivalent of SHA series, but faster. SHA is the best hash function // for doing checksum of content, because they have low collision ratios. See issue #2776. hash := blake2b.Sum256([]byte(term)) if len(hash) == 0 { - return nil, x.Errorf("Hash tokenizer failed to create hash") + return nil, errors.Errorf("Hash tokenizer failed to create hash") } return []string{string(hash[:])}, nil } diff --git a/tok/tokens.go b/tok/tokens.go index 9a16c731a7d..55676d139c5 100644 --- a/tok/tokens.go +++ b/tok/tokens.go @@ -17,7 +17,7 @@ package tok import ( - "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) func GetLangTokenizer(t Tokenizer, lang string) Tokenizer { @@ -35,11 +35,11 @@ func GetLangTokenizer(t Tokenizer, lang string) Tokenizer { func GetTokens(id byte, funcArgs ...string) ([]string, error) { if l := len(funcArgs); l != 1 { - return nil, x.Errorf("Function requires 1 arguments, but got %d", l) + return nil, errors.Errorf("Function requires 1 arguments, but got %d", l) } tokenizer, ok := GetTokenizerByID(id) if !ok { - return nil, x.Errorf("No tokenizer was found with id %v", id) + return nil, errors.Errorf("No tokenizer was found with id %v", id) } return BuildTokens(funcArgs[0], tokenizer) } @@ -50,7 +50,7 @@ func GetTermTokens(funcArgs []string) ([]string, error) { func GetFullTextTokens(funcArgs []string, lang string) ([]string, error) { if l := len(funcArgs); l != 1 { - return nil, x.Errorf("Function requires 1 arguments, but got %d", l) + return nil, errors.Errorf("Function requires 1 arguments, but got %d", l) } return BuildTokens(funcArgs[0], FullTextTokenizer{lang: lang}) } diff --git a/types/conversion.go b/types/conversion.go index 383faaf188a..42a1f3ef296 100644 --- a/types/conversion.go +++ b/types/conversion.go @@ -31,7 +31,6 @@ import ( "github.com/twpayne/go-geom/encoding/wkb" "github.com/dgraph-io/dgo/protos/api" - "github.com/dgraph-io/dgraph/x" ) // Convert converts the value to given scalar type. @@ -41,7 +40,7 @@ func Convert(from Val, toID TypeID) (Val, error) { // sanity: we expect a value data, ok := from.Value.([]byte) if !ok { - return to, x.Errorf("Invalid data to convert to %s", toID.Name()) + return to, errors.Errorf("Invalid data to convert to %s", toID.Name()) } to = ValueForType(toID) fromID := from.Tid @@ -59,12 +58,12 @@ func Convert(from Val, toID TypeID) (Val, error) { *res = string(data) case IntID: if len(data) < 8 { - return to, x.Errorf("Invalid data for int64 %v", data) + return to, errors.Errorf("Invalid data for int64 %v", data) } *res = int64(binary.LittleEndian.Uint64(data)) case FloatID: if len(data) < 8 { - return to, x.Errorf("Invalid data for float %v", data) + return to, errors.Errorf("Invalid data for float %v", data) } i := binary.LittleEndian.Uint64(data) *res = math.Float64frombits(i) @@ -76,7 +75,7 @@ func Convert(from Val, toID TypeID) (Val, error) { *res = true return to, nil } - return to, x.Errorf("Invalid value for bool %v", data[0]) + return to, errors.Errorf("Invalid value for bool %v", data[0]) case DateTimeID: var t time.Time if err := t.UnmarshalBinary(data); err != nil { @@ -151,7 +150,7 @@ func Convert(from Val, toID TypeID) (Val, error) { case IntID: { if len(data) < 8 { - return to, x.Errorf("Invalid data for int64 %v", data) + return to, errors.Errorf("Invalid data for int64 %v", data) } vc := int64(binary.LittleEndian.Uint64(data)) switch toID { @@ -176,7 +175,7 @@ func Convert(from Val, toID TypeID) (Val, error) { case FloatID: { if len(data) < 8 { - return to, x.Errorf("Invalid data for float %v", data) + return to, errors.Errorf("Invalid data for float %v", data) } i := binary.LittleEndian.Uint64(data) vc := math.Float64frombits(i) @@ -190,7 +189,7 @@ func Convert(from Val, toID TypeID) (Val, error) { *res = bs[:] case IntID: if vc > math.MaxInt64 || vc < math.MinInt64 || math.IsNaN(vc) { - return to, x.Errorf("Float out of int64 range") + return to, errors.Errorf("Float out of int64 range") } *res = int64(vc) case BoolID: @@ -210,7 +209,7 @@ func Convert(from Val, toID TypeID) (Val, error) { { var vc bool if len(data) == 0 || data[0] > 1 { - return to, x.Errorf("Invalid value for bool %v", data) + return to, errors.Errorf("Invalid value for bool %v", data) } vc = data[0] == 1 @@ -312,7 +311,7 @@ func Convert(from Val, toID TypeID) (Val, error) { func Marshal(from Val, to *Val) error { if to == nil { - return x.Errorf("Invalid conversion %s to nil", from.Tid.Name()) + return errors.Errorf("Invalid conversion %s to nil", from.Tid.Name()) } fromID := from.Tid @@ -407,7 +406,7 @@ func Marshal(from Val, to *Val) error { case GeoID: vc, ok := val.(geom.T) if !ok { - return x.Errorf("Expected a Geo type") + return errors.Errorf("Expected a Geo type") } switch toID { case BinaryID: @@ -450,37 +449,37 @@ func ObjectValue(id TypeID, value interface{}) (*api.Value, error) { case StringID: var v string if v, ok = value.(string); !ok { - return def, x.Errorf("Expected value of type string. Got : %v", value) + return def, errors.Errorf("Expected value of type string. Got : %v", value) } return &api.Value{Val: &api.Value_StrVal{StrVal: v}}, nil case DefaultID: var v string if v, ok = value.(string); !ok { - return def, x.Errorf("Expected value of type string. Got : %v", value) + return def, errors.Errorf("Expected value of type string. Got : %v", value) } return &api.Value{Val: &api.Value_DefaultVal{DefaultVal: v}}, nil case IntID: var v int64 if v, ok = value.(int64); !ok { - return def, x.Errorf("Expected value of type int64. Got : %v", value) + return def, errors.Errorf("Expected value of type int64. Got : %v", value) } return &api.Value{Val: &api.Value_IntVal{IntVal: v}}, nil case FloatID: var v float64 if v, ok = value.(float64); !ok { - return def, x.Errorf("Expected value of type float64. Got : %v", value) + return def, errors.Errorf("Expected value of type float64. Got : %v", value) } return &api.Value{Val: &api.Value_DoubleVal{DoubleVal: v}}, nil case BoolID: var v bool if v, ok = value.(bool); !ok { - return def, x.Errorf("Expected value of type bool. Got : %v", value) + return def, errors.Errorf("Expected value of type bool. Got : %v", value) } return &api.Value{Val: &api.Value_BoolVal{BoolVal: v}}, nil case BinaryID: var v []byte if v, ok = value.([]byte); !ok { - return def, x.Errorf("Expected value of type []byte. Got : %v", value) + return def, errors.Errorf("Expected value of type []byte. Got : %v", value) } return &api.Value{Val: &api.Value_BytesVal{BytesVal: v}}, nil // Geo and datetime are stored in binary format in the N-Quad, so lets @@ -500,11 +499,11 @@ func ObjectValue(id TypeID, value interface{}) (*api.Value, error) { case PasswordID: var v string if v, ok = value.(string); !ok { - return def, x.Errorf("Expected value of type password. Got : %v", value) + return def, errors.Errorf("Expected value of type password. Got : %v", value) } return &api.Value{Val: &api.Value_PasswordVal{PasswordVal: v}}, nil default: - return def, x.Errorf("ObjectValue not available for: %v", id) + return def, errors.Errorf("ObjectValue not available for: %v", id) } } @@ -517,7 +516,7 @@ func toBinary(id TypeID, b interface{}) ([]byte, error) { } func cantConvert(from TypeID, to TypeID) error { - return x.Errorf("Cannot convert %s to type %s", from.Name(), to.Name()) + return errors.Errorf("Cannot convert %s to type %s", from.Name(), to.Name()) } func (v Val) MarshalJSON() ([]byte, error) { @@ -537,5 +536,5 @@ func (v Val) MarshalJSON() ([]byte, error) { case PasswordID: return json.Marshal(v.Value.(string)) } - return nil, x.Errorf("Invalid type for MarshalJSON: %v", v.Tid) + return nil, errors.Errorf("Invalid type for MarshalJSON: %v", v.Tid) } diff --git a/types/facets/utils.go b/types/facets/utils.go index e64d6d698b6..712bfcc0224 100644 --- a/types/facets/utils.go +++ b/types/facets/utils.go @@ -27,7 +27,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/tok" "github.com/dgraph-io/dgraph/types" - "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) // Sorts And validates the facets. @@ -40,7 +40,7 @@ func SortAndValidate(fs []*api.Facet) error { }) for i := 1; i < len(fs); i++ { if fs[i-1].Key == fs[i].Key { - return x.Errorf("Repeated keys are not allowed in facets. But got %s", + return errors.Errorf("Repeated keys are not allowed in facets. But got %s", fs[i].Key) } } @@ -89,7 +89,7 @@ func valAndValType(val string) (interface{}, api.Facet_ValType, error) { // strings should be in quotes. if len(val) >= 2 && val[0] == '"' && val[len(val)-1] == '"' { uq, err := strconv.Unquote(val) - return uq, api.Facet_STRING, x.Wrapf(err, "could not unquote %q:", val) + return uq, api.Facet_STRING, errors.Wrapf(err, "could not unquote %q:", val) } if intVal, err := strconv.ParseInt(val, 0, 64); err == nil { return int64(intVal), api.Facet_INT, nil @@ -123,7 +123,7 @@ func valAndValType(val string) (interface{}, api.Facet_ValType, error) { if t, err := types.ParseTime(val); err == nil { return t, api.Facet_DATETIME, nil } - return nil, api.Facet_STRING, x.Errorf("Could not parse the facet value : [%s]", val) + return nil, api.Facet_STRING, errors.Errorf("Could not parse the facet value : [%s]", val) } // FacetFor returns Facet for given key and val. diff --git a/types/geofilter.go b/types/geofilter.go index fc2bcaed5f6..252a7778f56 100644 --- a/types/geofilter.go +++ b/types/geofilter.go @@ -26,6 +26,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) // QueryType indicates the type of geo query. @@ -67,15 +68,15 @@ func GetGeoTokens(srcFunc *pb.SrcFunction) ([]string, *GeoQueryData, error) { switch funcName { case "near": if len(srcFunc.Args) != 2 { - return nil, nil, x.Errorf("near function requires 2 arguments, but got %d", + return nil, nil, errors.Errorf("near function requires 2 arguments, but got %d", len(srcFunc.Args)) } maxDist, err := strconv.ParseFloat(srcFunc.Args[1], 64) if err != nil { - return nil, nil, x.Wrapf(err, "Error while converting distance to float") + return nil, nil, errors.Wrapf(err, "Error while converting distance to float") } if maxDist < 0 { - return nil, nil, x.Errorf("Distance cannot be negative") + return nil, nil, errors.Errorf("Distance cannot be negative") } g, err := convertToGeom(srcFunc.Args[0]) if err != nil { @@ -84,7 +85,7 @@ func GetGeoTokens(srcFunc *pb.SrcFunction) ([]string, *GeoQueryData, error) { return queryTokensGeo(QueryTypeNear, g, maxDist) case "within": if len(srcFunc.Args) != 1 { - return nil, nil, x.Errorf("within function requires 1 arguments, but got %d", + return nil, nil, errors.Errorf("within function requires 1 arguments, but got %d", len(srcFunc.Args)) } g, err := convertToGeom(srcFunc.Args[0]) @@ -94,7 +95,7 @@ func GetGeoTokens(srcFunc *pb.SrcFunction) ([]string, *GeoQueryData, error) { return queryTokensGeo(QueryTypeWithin, g, 0.0) case "contains": if len(srcFunc.Args) != 1 { - return nil, nil, x.Errorf("contains function requires 1 arguments, but got %d", + return nil, nil, errors.Errorf("contains function requires 1 arguments, but got %d", len(srcFunc.Args)) } g, err := convertToGeom(srcFunc.Args[0]) @@ -104,7 +105,7 @@ func GetGeoTokens(srcFunc *pb.SrcFunction) ([]string, *GeoQueryData, error) { return queryTokensGeo(QueryTypeContains, g, 0.0) case "intersects": if len(srcFunc.Args) != 1 { - return nil, nil, x.Errorf("intersects function requires 1 arguments, but got %d", + return nil, nil, errors.Errorf("intersects function requires 1 arguments, but got %d", len(srcFunc.Args)) } g, err := convertToGeom(srcFunc.Args[0]) @@ -113,7 +114,7 @@ func GetGeoTokens(srcFunc *pb.SrcFunction) ([]string, *GeoQueryData, error) { } return queryTokensGeo(QueryTypeIntersects, g, 0.0) default: - return nil, nil, x.Errorf("Invalid geo function") + return nil, nil, errors.Errorf("Invalid geo function") } } @@ -135,7 +136,7 @@ func queryTokensGeo(qt QueryType, g geom.T, maxDistance float64) ([]string, *Geo // We use the point and make a loop with radius maxDistance. Then we can use this for // the rest of the query. if maxDistance <= 0 { - return nil, nil, x.Errorf("Invalid max distance specified for a near query") + return nil, nil, errors.Errorf("Invalid max distance specified for a near query") } a := EarthAngle(maxDistance) l := s2.RegularLoop(*pt, a, 100) @@ -159,7 +160,7 @@ func queryTokensGeo(qt QueryType, g geom.T, maxDistance float64) ([]string, *Geo } default: - return nil, nil, x.Errorf("Cannot query using a geometry of type %T", v) + return nil, nil, errors.Errorf("Cannot query using a geometry of type %T", v) } x.AssertTruef(len(loops) > 0 || pt != nil, "We should have a point or a loop.") @@ -167,7 +168,7 @@ func queryTokensGeo(qt QueryType, g geom.T, maxDistance float64) ([]string, *Geo var cover, parents s2.CellUnion if qt == QueryTypeNear { if len(loops) == 0 { - return nil, nil, x.Errorf("Internal error while processing near query.") + return nil, nil, errors.Errorf("Internal error while processing near query.") } cover = coverLoop(loops[0], MinCellLevel, MaxCellLevel, MaxCells) parents = getParentCells(cover, MinCellLevel) @@ -183,7 +184,7 @@ func queryTokensGeo(qt QueryType, g geom.T, maxDistance float64) ([]string, *Geo // For a within query we only need to look at the objects whose parents match our cover. // So we take our cover and prefix with the parentPrefix to look in the index. if len(loops) == 0 { - return nil, nil, x.Errorf("Require a polygon for within query") + return nil, nil, errors.Errorf("Require a polygon for within query") } toks := createTokens(cover, parentPrefix) return toks, &GeoQueryData{loops: loops, qtype: qt}, nil @@ -195,7 +196,7 @@ func queryTokensGeo(qt QueryType, g geom.T, maxDistance float64) ([]string, *Geo case QueryTypeNear: if pt == nil { - return []string{}, nil, x.Errorf("Require a point for a within query.") + return []string{}, nil, errors.Errorf("Require a point for a within query.") } // A near query is the same as the intersects query. We form a loop with the given point and // the radius and then see what all does it intersect with. @@ -207,13 +208,13 @@ func queryTokensGeo(qt QueryType, g geom.T, maxDistance float64) ([]string, *Geo // given region. So we look at all the objects whose parents match our cover as well as // all the objects whose cover matches our parents. if len(loops) == 0 { - return nil, nil, x.Errorf("Require a polygon for intersects query") + return nil, nil, errors.Errorf("Require a polygon for intersects query") } toks := parentCoverTokens(parents, cover) return toks, &GeoQueryData{loops: loops, qtype: qt}, nil default: - return nil, nil, x.Errorf("Unknown query type") + return nil, nil, errors.Errorf("Unknown query type") } } diff --git a/types/geofilter_test.go b/types/geofilter_test.go index 49f0b8ebe2f..4e096da0be7 100644 --- a/types/geofilter_test.go +++ b/types/geofilter_test.go @@ -21,7 +21,7 @@ import ( "strings" "testing" - "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" "github.com/stretchr/testify/require" "github.com/twpayne/go-geom" "github.com/twpayne/go-geom/encoding/wkb" @@ -34,7 +34,7 @@ func queryTokens(qt QueryType, data string, maxDistance float64) ([]string, *Geo src.Value = []byte(geoData) gc, err := Convert(src, GeoID) if err != nil { - return nil, nil, x.Wrapf(err, "Cannot decode given geoJson input") + return nil, nil, errors.Wrapf(err, "Cannot decode given geoJson input") } g := gc.Value.(geom.T) diff --git a/types/password.go b/types/password.go index f40902978fd..9233044896b 100644 --- a/types/password.go +++ b/types/password.go @@ -19,7 +19,7 @@ package types import ( "golang.org/x/crypto/bcrypt" - "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) const ( @@ -28,7 +28,7 @@ const ( func Encrypt(plain string) (string, error) { if len(plain) < pwdLenLimit { - return "", x.Errorf("Password too short, i.e. should have at least 6 chars") + return "", errors.Errorf("Password too short, i.e. should have at least 6 chars") } encrypted, err := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost) @@ -41,7 +41,7 @@ func Encrypt(plain string) (string, error) { func VerifyPassword(plain, encrypted string) error { if len(plain) < pwdLenLimit || len(encrypted) == 0 { - return x.Errorf("Invalid password/crypted string") + return errors.Errorf("Invalid password/crypted string") } return bcrypt.CompareHashAndPassword([]byte(encrypted), []byte(plain)) diff --git a/types/s2.go b/types/s2.go index 8442d7bdaa6..d678df05414 100644 --- a/types/s2.go +++ b/types/s2.go @@ -21,6 +21,7 @@ import ( "github.com/dgraph-io/dgraph/x" "github.com/golang/geo/s2" + "github.com/pkg/errors" geom "github.com/twpayne/go-geom" "github.com/twpayne/go-geom/encoding/geojson" ) @@ -138,7 +139,7 @@ func closed(coords []geom.Coord) bool { func convertToGeom(str string) (geom.T, error) { s := x.WhiteSpace.Replace(str) if len(s) < 5 { // [1,2] - return nil, x.Errorf("Invalid coordinates") + return nil, errors.Errorf("Invalid coordinates") } var g geojson.Geometry var m json.RawMessage @@ -148,22 +149,22 @@ func convertToGeom(str string) (geom.T, error) { g.Type = "MultiPolygon" err = m.UnmarshalJSON([]byte(s)) if err != nil { - return nil, x.Wrapf(err, "Invalid coordinates") + return nil, errors.Wrapf(err, "Invalid coordinates") } g.Coordinates = &m g1, err := g.Decode() if err != nil { - return nil, x.Wrapf(err, "Invalid coordinates") + return nil, errors.Wrapf(err, "Invalid coordinates") } mp := g1.(*geom.MultiPolygon) for i := 0; i < mp.NumPolygons(); i++ { coords := mp.Polygon(i).Coords() if len(coords) == 0 { - return nil, x.Errorf("Got empty polygon inside multi-polygon.") + return nil, errors.Errorf("Got empty polygon inside multi-polygon.") } // Check that first ring is closed. if !closed(mp.Polygon(i).Coords()[0]) { - return nil, x.Errorf("Last coord not same as first") + return nil, errors.Errorf("Last coord not same as first") } } return g1, nil @@ -173,20 +174,20 @@ func convertToGeom(str string) (geom.T, error) { g.Type = "Polygon" err = m.UnmarshalJSON([]byte(s)) if err != nil { - return nil, x.Wrapf(err, "Invalid coordinates") + return nil, errors.Wrapf(err, "Invalid coordinates") } g.Coordinates = &m g1, err := g.Decode() if err != nil { - return nil, x.Wrapf(err, "Invalid coordinates") + return nil, errors.Wrapf(err, "Invalid coordinates") } coords := g1.(*geom.Polygon).Coords() if len(coords) == 0 { - return nil, x.Errorf("Got empty polygon.") + return nil, errors.Errorf("Got empty polygon.") } // Check that first ring is closed. if !closed(coords[0]) { - return nil, x.Errorf("Last coord not same as first") + return nil, errors.Errorf("Last coord not same as first") } return g1, nil } @@ -195,10 +196,10 @@ func convertToGeom(str string) (geom.T, error) { g.Type = "Point" err = m.UnmarshalJSON([]byte(s)) if err != nil { - return nil, x.Wrapf(err, "Invalid coordinates") + return nil, errors.Wrapf(err, "Invalid coordinates") } g.Coordinates = &m return g.Decode() } - return nil, x.Errorf("Invalid coordinates") + return nil, errors.Errorf("Invalid coordinates") } diff --git a/types/s2index.go b/types/s2index.go index 005348d06dd..042729186f8 100644 --- a/types/s2index.go +++ b/types/s2index.go @@ -23,6 +23,7 @@ import ( geom "github.com/twpayne/go-geom" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) func parentCoverTokens(parents s2.CellUnion, cover s2.CellUnion) []string { @@ -72,7 +73,7 @@ const ( // query. func indexCells(g geom.T) (parents, cover s2.CellUnion, err error) { if g.Stride() != 2 { - return nil, nil, x.Errorf("Covering only available for 2D co-ordinates.") + return nil, nil, errors.Errorf("Covering only available for 2D co-ordinates.") } switch v := g.(type) { case *geom.Point: @@ -101,7 +102,7 @@ func indexCells(g geom.T) (parents, cover s2.CellUnion, err error) { parents := getParentCells(cover, MinCellLevel) return parents, cover, nil default: - return nil, nil, x.Errorf("Cannot index geometry of type %T", v) + return nil, nil, errors.Errorf("Cannot index geometry of type %T", v) } } @@ -134,10 +135,10 @@ func loopFromPolygon(p *geom.Polygon) (*s2.Loop, error) { r := p.LinearRing(0) n := r.NumCoords() if n < 4 { - return nil, x.Errorf("Can't convert ring with less than 4 pts") + return nil, errors.Errorf("Can't convert ring with less than 4 pts") } if !r.Coord(0).Equal(geom.XY, r.Coord(n-1)) { - return nil, x.Errorf("Last coordinate not same as first for polygon: %+v\n", p) + return nil, errors.Errorf("Last coordinate not same as first for polygon: %+v\n", p) } // S2 specifies that the orientation of the polygons should be CCW. However there is no // restriction on the orientation in WKB (or geojson). To get the correct orientation we assume diff --git a/types/sort.go b/types/sort.go index 737b54ed842..65eb5e1b899 100644 --- a/types/sort.go +++ b/types/sort.go @@ -23,6 +23,7 @@ import ( "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) type sortBase struct { @@ -107,14 +108,14 @@ func Sort(v [][]Val, ul *pb.List, desc []bool) error { // Less returns true if a is strictly less than b. func Less(a, b Val) (bool, error) { if a.Tid != b.Tid { - return false, x.Errorf("Arguments of different type can not be compared.") + return false, errors.Errorf("Arguments of different type can not be compared.") } typ := a.Tid switch typ { case DateTimeID, UidID, IntID, FloatID, StringID, DefaultID: // Don't do anything, we can sort values of this type. default: - return false, x.Errorf("Compare not supported for type: %v", a.Tid) + return false, errors.Errorf("Compare not supported for type: %v", a.Tid) } return less(a, b), nil } @@ -159,14 +160,14 @@ func mismatchedLess(a, b Val) bool { // Equal returns true if a is equal to b. func Equal(a, b Val) (bool, error) { if a.Tid != b.Tid { - return false, x.Errorf("Arguments of different type can not be compared.") + return false, errors.Errorf("Arguments of different type can not be compared.") } typ := a.Tid switch typ { case DateTimeID, IntID, FloatID, StringID, DefaultID, BoolID: // Don't do anything, we can sort values of this type. default: - return false, x.Errorf("Equal not supported for type: %v", a.Tid) + return false, errors.Errorf("Equal not supported for type: %v", a.Tid) } return equal(a, b), nil } diff --git a/worker/backup_ee.go b/worker/backup_ee.go index 8167f63ba1f..fe052dbabea 100644 --- a/worker/backup_ee.go +++ b/worker/backup_ee.go @@ -22,6 +22,7 @@ import ( "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" "golang.org/x/net/context" ) @@ -36,7 +37,7 @@ func backupProcess(ctx context.Context, req *pb.BackupRequest) error { g := groups() if g.groupId() != req.GroupId { - return x.Errorf("Backup request group mismatch. Mine: %d. Requested: %d\n", + return errors.Errorf("Backup request group mismatch. Mine: %d. Requested: %d\n", g.groupId(), req.GroupId) } @@ -71,7 +72,7 @@ func backupGroup(ctx context.Context, in *pb.BackupRequest) error { // send request to any node in the group. pl := groups().AnyServer(in.GroupId) if pl == nil { - return x.Errorf("Couldn't find a server in group %d", in.GroupId) + return errors.Errorf("Couldn't find a server in group %d", in.GroupId) } res, err := pb.NewWorkerClient(pl.Get()).Backup(ctx, in) if err != nil { @@ -88,7 +89,7 @@ func backupGroup(ctx context.Context, in *pb.BackupRequest) error { func BackupOverNetwork(ctx context.Context, r *http.Request) error { destination := r.FormValue("destination") if destination == "" { - return x.Errorf("You must specify a 'destination' value") + return errors.Errorf("You must specify a 'destination' value") } accessKey := r.FormValue("access_key") diff --git a/worker/draft.go b/worker/draft.go index 8ad5ed36133..a92c484b791 100644 --- a/worker/draft.go +++ b/worker/draft.go @@ -19,7 +19,6 @@ package worker import ( "bytes" "encoding/binary" - "errors" "fmt" "sort" "sync" @@ -45,6 +44,7 @@ import ( "github.com/dgraph-io/dgraph/schema" "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" "github.com/golang/glog" "golang.org/x/net/context" @@ -541,7 +541,7 @@ func (n *node) commitOrAbort(pkey string, delta *pb.OracleDelta) error { toDisk(status.StartTs, status.CommitTs) } if err := writer.Flush(); err != nil { - return x.Errorf("Error while flushing to disk: %v", err) + return errors.Wrapf(err, "while flushing to disk") } g := groups() @@ -637,7 +637,6 @@ func (n *node) retrieveSnapshot(snap pb.Snapshot) error { func (n *node) proposeSnapshot(discardN int) error { snap, err := n.calculateSnapshot(0, discardN) if err != nil { - glog.Warningf("Got error while calculating snapshot: %v", err) return err } if snap == nil { @@ -1323,7 +1322,7 @@ func (n *node) joinPeers() error { c := pb.NewRaftClient(gconn) glog.Infof("Calling JoinCluster via leader: %s", pl.Addr) if _, err := c.JoinCluster(n.ctx, n.RaftContext); err != nil { - return x.Errorf("Error while joining cluster: %+v\n", err) + return errors.Wrapf(err, "error while joining cluster") } glog.Infof("Done with JoinCluster call\n") return nil @@ -1341,7 +1340,7 @@ func (n *node) isMember() (bool, error) { glog.Infof("Calling IsPeer") pr, err := c.IsPeer(n.ctx, n.RaftContext) if err != nil { - return false, x.Errorf("Error while joining cluster: %+v\n", err) + return false, errors.Wrapf(err, "error while joining cluster") } glog.Infof("Done with IsPeer call\n") return pr.Status, nil diff --git a/worker/export.go b/worker/export.go index 8f047914d29..fc1d20e7cd8 100644 --- a/worker/export.go +++ b/worker/export.go @@ -30,6 +30,7 @@ import ( "unicode" "github.com/golang/glog" + "github.com/pkg/errors" "golang.org/x/net/context" "github.com/dgraph-io/badger" @@ -377,7 +378,7 @@ func (writer *fileWriter) Close() error { // export creates a export of data by exporting it as an RDF gzip. func export(ctx context.Context, in *pb.ExportRequest) error { if in.GroupId != groups().groupId() { - return x.Errorf("Export request group mismatch. Mine: %d. Requested: %d\n", + return errors.Errorf("Export request group mismatch. Mine: %d. Requested: %d\n", groups().groupId(), in.GroupId) } glog.Infof("Export requested at %d.", in.ReadTs) @@ -553,7 +554,7 @@ func handleExportOverNetwork(ctx context.Context, in *pb.ExportRequest) error { pl := groups().Leader(in.GroupId) if pl == nil { - return x.Errorf("Unable to find leader of group: %d\n", in.GroupId) + return errors.Errorf("Unable to find leader of group: %d\n", in.GroupId) } glog.Infof("Sending export request to group: %d, addr: %s\n", in.GroupId, pl.Addr) diff --git a/worker/groups.go b/worker/groups.go index 34ec35ad3c1..7ddffaca0d1 100644 --- a/worker/groups.go +++ b/worker/groups.go @@ -36,6 +36,7 @@ import ( "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" "github.com/golang/protobuf/proto" + "github.com/pkg/errors" ) type groupi struct { @@ -237,7 +238,7 @@ func UpdateMembershipState(ctx context.Context) error { g := groups() p := g.Leader(0) if p == nil { - return x.Errorf("Don't have the address of any dgraphzero server") + return errors.Errorf("Don't have the address of any dgraphzero server") } c := pb.NewZeroClient(p.Get()) @@ -632,7 +633,7 @@ func (g *groupi) doSendMembership(tablets map[string]*pb.Tablet) error { if string(reply.GetData()) == "OK" { return nil } - return x.Errorf(string(reply.GetData())) + return errors.Errorf(string(reply.GetData())) } // sendMembershipUpdates sends the membership update to Zero leader. If this Alpha is the leader, it diff --git a/worker/mutation.go b/worker/mutation.go index e382c010762..fcbc0524317 100644 --- a/worker/mutation.go +++ b/worker/mutation.go @@ -18,7 +18,6 @@ package worker import ( "bytes" - "errors" "math" "time" @@ -34,14 +33,15 @@ import ( "github.com/dgraph-io/dgraph/x" "github.com/golang/glog" + "github.com/pkg/errors" otrace "go.opencensus.io/trace" "golang.org/x/net/context" ) var ( ErrNonExistentTabletMessage = "Requested predicate is not being served by any tablet" - errNonExistentTablet = x.Errorf(ErrNonExistentTabletMessage) - errUnservedTablet = x.Errorf("Tablet isn't being served by this instance") + errNonExistentTablet = errors.Errorf(ErrNonExistentTabletMessage) + errUnservedTablet = errors.Errorf("Tablet isn't being served by this instance") ) func isStarAll(v []byte) bool { @@ -60,7 +60,7 @@ func runMutation(ctx context.Context, edge *pb.DirectedEdge, txn *posting.Txn) e su, ok := schema.State().Get(edge.Attr) if edge.Op == pb.DirectedEdge_SET { if !ok { - return x.Errorf("runMutation: Unable to find schema for %s", edge.Attr) + return errors.Errorf("runMutation: Unable to find schema for %s", edge.Attr) } } @@ -116,7 +116,7 @@ func runSchemaMutationHelper(ctx context.Context, update *pb.SchemaUpdate, start if tablet, err := groups().Tablet(update.Predicate); err != nil { return err } else if tablet.GetGroupId() != groups().groupId() { - return x.Errorf("Tablet isn't being served by this group. Tablet: %+v", tablet) + return errors.Errorf("Tablet isn't being served by this group. Tablet: %+v", tablet) } if err := checkSchema(update); err != nil { @@ -230,30 +230,30 @@ func hasEdges(attr string, startTs uint64) bool { } func checkSchema(s *pb.SchemaUpdate) error { if len(s.Predicate) == 0 { - return x.Errorf("No predicate specified in schema mutation") + return errors.Errorf("No predicate specified in schema mutation") } if s.Directive == pb.SchemaUpdate_INDEX && len(s.Tokenizer) == 0 { - return x.Errorf("Tokenizer must be specified while indexing a predicate: %+v", s) + return errors.Errorf("Tokenizer must be specified while indexing a predicate: %+v", s) } if len(s.Tokenizer) > 0 && s.Directive != pb.SchemaUpdate_INDEX { - return x.Errorf("Directive must be SchemaUpdate_INDEX when a tokenizer is specified") + return errors.Errorf("Directive must be SchemaUpdate_INDEX when a tokenizer is specified") } typ := types.TypeID(s.ValueType) if typ == types.UidID && s.Directive == pb.SchemaUpdate_INDEX { // index on uid type - return x.Errorf("Index not allowed on predicate of type uid on predicate %s", + return errors.Errorf("Index not allowed on predicate of type uid on predicate %s", s.Predicate) } else if typ != types.UidID && s.Directive == pb.SchemaUpdate_REVERSE { // reverse on non-uid type - return x.Errorf("Cannot reverse for non-uid type on predicate %s", s.Predicate) + return errors.Errorf("Cannot reverse for non-uid type on predicate %s", s.Predicate) } // If schema update has upsert directive, it should have index directive. if s.Upsert && len(s.Tokenizer) == 0 { - return x.Errorf("Index tokenizer is mandatory for: [%s] when specifying @upsert directive", + return errors.Errorf("Index tokenizer is mandatory for: [%s] when specifying @upsert directive", s.Predicate) } @@ -268,22 +268,22 @@ func checkSchema(s *pb.SchemaUpdate) error { case t.IsScalar() && (t.Enum() == pb.Posting_PASSWORD || s.ValueType == pb.Posting_PASSWORD): // can't change password -> x, x -> password if t.Enum() != s.ValueType { - return x.Errorf("Schema change not allowed from %s to %s", - t.Enum().String(), typ.Enum().String()) + return errors.Errorf("Schema change not allowed from %s to %s", + t.Enum(), typ.Enum()) } case t.IsScalar() == typ.IsScalar(): // If old type was list and new type is non-list, we don't allow it until user // has data. if schema.State().IsList(s.Predicate) && !s.List && hasEdges(s.Predicate, math.MaxUint64) { - return x.Errorf("Schema change not allowed from [%s] => %s without"+ + return errors.Errorf("Schema change not allowed from [%s] => %s without"+ " deleting pred: %s", t.Name(), typ.Name(), s.Predicate) } default: // uid => scalar or scalar => uid. Check that there shouldn't be any data. if hasEdges(s.Predicate, math.MaxUint64) { - return x.Errorf("Schema change not allowed from scalar to uid or vice versa"+ + return errors.Errorf("Schema change not allowed from scalar to uid or vice versa"+ " while there is data for pred: %s", s.Predicate) } } @@ -292,24 +292,24 @@ func checkSchema(s *pb.SchemaUpdate) error { func checkType(t *pb.TypeUpdate) error { if len(t.TypeName) == 0 { - return x.Errorf("Type name must be specified in type update") + return errors.Errorf("Type name must be specified in type update") } for _, field := range t.Fields { if len(field.Predicate) == 0 { - return x.Errorf("Field in type definition must have a name") + return errors.Errorf("Field in type definition must have a name") } if field.ValueType == pb.Posting_OBJECT && len(field.ObjectTypeName) == 0 { - return x.Errorf("Field with value type OBJECT must specify the name of the object type") + return errors.Errorf("Field with value type OBJECT must specify the name of the object type") } if field.Directive != pb.SchemaUpdate_NONE { - return x.Errorf("Field in type definition cannot have a directive") + return errors.Errorf("Field in type definition cannot have a directive") } if len(field.Tokenizer) > 0 { - return x.Errorf("Field in type definition cannot have tokenizers") + return errors.Errorf("Field in type definition cannot have tokenizers") } } @@ -332,17 +332,17 @@ func ValidateAndConvert(edge *pb.DirectedEdge, su *pb.SchemaUpdate) error { // type checks switch { case edge.Lang != "" && !su.GetLang(): - return x.Errorf("Attr: [%v] should have @lang directive in schema to mutate edge: [%v]", + return errors.Errorf("Attr: [%v] should have @lang directive in schema to mutate edge: [%v]", edge.Attr, edge) case !schemaType.IsScalar() && !storageType.IsScalar(): return nil case !schemaType.IsScalar() && storageType.IsScalar(): - return x.Errorf("Input for predicate %s of type uid is scalar", edge.Attr) + return errors.Errorf("Input for predicate %s of type uid is scalar", edge.Attr) case schemaType.IsScalar() && !storageType.IsScalar(): - return x.Errorf("Input for predicate %s of type scalar is uid. Edge: %v", edge.Attr, edge) + return errors.Errorf("Input for predicate %s of type scalar is uid. Edge: %v", edge.Attr, edge) // The suggested storage type matches the schema, OK! case storageType == schemaType && schemaType != types.DefaultID: @@ -601,7 +601,7 @@ func (w *grpcWorker) Mutate(ctx context.Context, m *pb.Mutations) (*api.TxnConte return txnCtx, ctx.Err() } if !groups().ServesGroup(m.GroupId) { - return txnCtx, x.Errorf("This server doesn't serve group id: %v", m.GroupId) + return txnCtx, errors.Errorf("This server doesn't serve group id: %v", m.GroupId) } return txnCtx, w.proposeAndWait(ctx, txnCtx, m) diff --git a/worker/predicate_move.go b/worker/predicate_move.go index 5db56e4f19e..27dda69d52b 100644 --- a/worker/predicate_move.go +++ b/worker/predicate_move.go @@ -22,6 +22,7 @@ import ( "strconv" "github.com/golang/glog" + "github.com/pkg/errors" otrace "go.opencensus.io/trace" "golang.org/x/net/context" @@ -35,8 +36,8 @@ import ( ) var ( - errEmptyPredicate = x.Errorf("Predicate not specified") - errNotLeader = x.Errorf("Server is not leader of this group") + errEmptyPredicate = errors.Errorf("Predicate not specified") + errNotLeader = errors.Errorf("Server is not leader of this group") emptyPayload = api.Payload{} ) @@ -70,7 +71,7 @@ func batchAndProposeKeyValues(ctx context.Context, kvs chan *pb.KVS) error { // This only happens once. pk = x.Parse(kv.Key) if !pk.IsSchema() { - return x.Errorf("Expecting first key to be schema key: %+v", kv) + return errors.Errorf("Expecting first key to be schema key: %+v", kv) } // Delete on all nodes. p := &pb.Proposal{CleanPredicate: pk.Attr} @@ -105,7 +106,7 @@ func batchAndProposeKeyValues(ctx context.Context, kvs chan *pb.KVS) error { // for a predicate or not. func (w *grpcWorker) ReceivePredicate(stream pb.Worker_ReceivePredicateServer) error { if !groups().Node.AmLeader() { - return x.Errorf("ReceivePredicate failed: Not the leader of group") + return errors.Errorf("ReceivePredicate failed: Not the leader of group") } // No new deletion/background cleanup would start after we start streaming tablet, // so all the proposals for a particular tablet would atmost wait for deletion of @@ -171,7 +172,7 @@ func (w *grpcWorker) MovePredicate(ctx context.Context, } if groups().groupId() != in.SourceGid { return &emptyPayload, - x.Errorf("Group id doesn't match, received request for %d, my gid: %d", + errors.Errorf("Group id doesn't match, received request for %d, my gid: %d", in.SourceGid, groups().groupId()) } if len(in.Predicate) == 0 { @@ -183,7 +184,7 @@ func (w *grpcWorker) MovePredicate(ctx context.Context, return &emptyPayload, groups().Node.proposeAndWait(ctx, p) } if err := posting.Oracle().WaitForTs(ctx, in.TxnTs); err != nil { - return &emptyPayload, x.Errorf("While waiting for txn ts: %d. Error: %v", in.TxnTs, err) + return &emptyPayload, errors.Errorf("While waiting for txn ts: %d. Error: %v", in.TxnTs, err) } if gid, err := groups().BelongsTo(in.Predicate); err != nil { return &emptyPayload, err @@ -209,7 +210,7 @@ func movePredicateHelper(ctx context.Context, in *pb.MovePredicatePayload) error pl := groups().Leader(in.DestGid) if pl == nil { - return x.Errorf("Unable to find a connection for group: %d\n", in.DestGid) + return errors.Errorf("Unable to find a connection for group: %d\n", in.DestGid) } c := pb.NewWorkerClient(pl.Get()) s, err := c.ReceivePredicate(ctx) diff --git a/worker/predicate_test.go b/worker/predicate_test.go index e812493728d..7c8c014b7a3 100644 --- a/worker/predicate_test.go +++ b/worker/predicate_test.go @@ -251,7 +251,7 @@ func TestPopulateShard(t *testing.T) { // return err // } // if len(val) == 0 { - // return x.Errorf("value for uid 1 predicate name not found\n") + // return errors.Errorf("value for uid 1 predicate name not found\n") // } // return nil // }) @@ -285,7 +285,7 @@ func TestPopulateShard(t *testing.T) { // } // require.NoError(t, item.Value(func(val []byte) error { // if len(val) != 0 { - // return x.Errorf("value for uid 1 predicate name shouldn't be present\n") + // return errors.Errorf("value for uid 1 predicate name shouldn't be present\n") // } // return nil // })) @@ -295,7 +295,7 @@ func TestPopulateShard(t *testing.T) { // } // require.NoError(t, item.Value(func(val []byte) error { // if len(val) != 0 { - // return x.Errorf("value for uid 1 predicate name shouldn't be present\n") + // return errors.Errorf("value for uid 1 predicate name shouldn't be present\n") // } // return nil // })) diff --git a/worker/proposal.go b/worker/proposal.go index f0bba54bf52..8640c190114 100644 --- a/worker/proposal.go +++ b/worker/proposal.go @@ -17,7 +17,6 @@ package worker import ( - "errors" "fmt" "sync/atomic" "time" @@ -31,6 +30,7 @@ import ( tag "go.opencensus.io/tag" otrace "go.opencensus.io/trace" + "github.com/pkg/errors" "golang.org/x/net/context" ) @@ -124,7 +124,7 @@ func (n *node) proposeAndWait(ctx context.Context, proposal *pb.Proposal) (perr }() if n.Raft() == nil { - return x.Errorf("Raft isn't initialized yet") + return errors.Errorf("Raft isn't initialized yet") } if ctx.Err() != nil { return ctx.Err() @@ -203,7 +203,7 @@ func (n *node) proposeAndWait(ctx context.Context, proposal *pb.Proposal) (perr return err } if err = n.Raft().Propose(cctx, data); err != nil { - return x.Wrapf(err, "While proposing") + return errors.Wrapf(err, "While proposing") } timer := time.NewTimer(timeout) diff --git a/worker/schema.go b/worker/schema.go index 017741a0dd1..152b9503076 100644 --- a/worker/schema.go +++ b/worker/schema.go @@ -17,6 +17,7 @@ package worker import ( + "github.com/pkg/errors" otrace "go.opencensus.io/trace" "golang.org/x/net/context" @@ -224,7 +225,7 @@ func (w *grpcWorker) Schema(ctx context.Context, s *pb.SchemaRequest) (*pb.Schem } if !groups().ServesGroup(s.GroupId) { - return &emptySchemaResult, x.Errorf("This server doesn't serve group id: %v", s.GroupId) + return &emptySchemaResult, errors.Errorf("This server doesn't serve group id: %v", s.GroupId) } return getSchema(ctx, s) } diff --git a/worker/sort.go b/worker/sort.go index 7623171c448..9ea5d581dea 100644 --- a/worker/sort.go +++ b/worker/sort.go @@ -23,6 +23,7 @@ import ( "time" "github.com/dgraph-io/badger" + "github.com/pkg/errors" otrace "go.opencensus.io/trace" "golang.org/x/net/context" @@ -68,9 +69,10 @@ func SortOverNetwork(ctx context.Context, q *pb.SortMessage) (*pb.SortResult, er return processSort(ctx, q) } - result, err := processWithBackupRequest(ctx, gid, func(ctx context.Context, c pb.WorkerClient) (interface{}, error) { - return c.Sort(ctx, q) - }) + result, err := processWithBackupRequest( + ctx, gid, func(ctx context.Context, c pb.WorkerClient) (interface{}, error) { + return c.Sort(ctx, q) + }) if err != nil { return &emptySortResult, err } @@ -92,7 +94,8 @@ func (w *grpcWorker) Sort(ctx context.Context, s *pb.SortMessage) (*pb.SortResul span.Annotatef(nil, "Sorting: Attribute: %q groupId: %v Sort", s.Order[0].Attr, gid) if gid != groups().groupId() { - return nil, x.Errorf("attr: %q groupId: %v Request sent to wrong server.", s.Order[0].Attr, gid) + return nil, errors.Errorf("attr: %q groupId: %v Request sent to wrong server.", + s.Order[0].Attr, gid) } var reply *pb.SortResult @@ -112,8 +115,8 @@ func (w *grpcWorker) Sort(ctx context.Context, s *pb.SortMessage) (*pb.SortResul } var ( - errContinue = x.Errorf("Continue processing buckets") - errDone = x.Errorf("Done processing buckets") + errContinue = errors.Errorf("Continue processing buckets") + errDone = errors.Errorf("Done processing buckets") ) func resultWithError(err error) *sortresult { @@ -132,7 +135,7 @@ func sortWithoutIndex(ctx context.Context, ts *pb.SortMessage) *sortresult { // might have millions of keys just for retrieving some values. sType, err := schema.State().TypeOf(ts.Order[0].Attr) if err != nil || !sType.IsScalar() { - return resultWithError(x.Errorf("Cannot sort attribute %s of type object.", + return resultWithError(errors.Errorf("Cannot sort attribute %s of type object.", ts.Order[0].Attr)) } @@ -189,12 +192,12 @@ func sortWithIndex(ctx context.Context, ts *pb.SortMessage) *sortresult { order := ts.Order[0] typ, err := schema.State().TypeOf(order.Attr) if err != nil { - return resultWithError(fmt.Errorf("Attribute %s not defined in schema", order.Attr)) + return resultWithError(errors.Errorf("Attribute %s not defined in schema", order.Attr)) } // Get the tokenizers and choose the corresponding one. if !schema.State().IsIndexed(order.Attr) { - return resultWithError(x.Errorf("Attribute %s is not indexed.", order.Attr)) + return resultWithError(errors.Errorf("Attribute %s is not indexed.", order.Attr)) } tokenizers := schema.State().Tokenizer(order.Attr) @@ -211,12 +214,12 @@ func sortWithIndex(ctx context.Context, ts *pb.SortMessage) *sortresult { // String type can have multiple tokenizers, only one of which is // sortable. if typ == types.StringID { - return resultWithError(x.Errorf("Attribute:%s does not have exact index for sorting.", - order.Attr)) + return resultWithError(errors.Errorf( + "Attribute %s does not have exact index for sorting.", order.Attr)) } // Other types just have one tokenizer, so if we didn't find a // sortable tokenizer, then attribute isn't sortable. - return resultWithError(x.Errorf("Attribute:%s is not sortable.", order.Attr)) + return resultWithError(errors.Errorf("Attribute %s is not sortable.", order.Attr)) } // Iterate over every bucket / token. @@ -420,12 +423,14 @@ func processSort(ctx context.Context, ts *pb.SortMessage) (*pb.SortResult, error span.Annotate(nil, "Done waiting") if ts.Count < 0 { - return nil, x.Errorf("We do not yet support negative or infinite count with sorting: %s %d. "+ - "Try flipping order and return first few elements instead.", ts.Order[0].Attr, ts.Count) + return nil, errors.Errorf( + "We do not yet support negative or infinite count with sorting: %s %d. "+ + "Try flipping order and return first few elements instead.", ts.Order[0].Attr, ts.Count) } // TODO (pawan) - Why check only the first attribute, what if other attributes are of list type? if schema.State().IsList(ts.Order[0].Attr) { - return nil, x.Errorf("Sorting not supported on attr: %s of type: [scalar]", ts.Order[0].Attr) + return nil, errors.Errorf("Sorting not supported on attr: %s of type: [scalar]", + ts.Order[0].Attr) } // We're not using any txn local cache here. So, no need to deal with that yet. @@ -516,7 +521,7 @@ func intersectBucket(ctx context.Context, ts *pb.SortMessage, token string, order := ts.Order[0] sType, err := schema.State().TypeOf(order.Attr) if err != nil || !sType.IsScalar() { - return x.Errorf("Cannot sort attribute %s of type object.", order.Attr) + return errors.Errorf("Cannot sort attribute %s of type object.", order.Attr) } scalar := sType diff --git a/worker/task.go b/worker/task.go index 90d23ff2ba0..4f83790d3f5 100644 --- a/worker/task.go +++ b/worker/task.go @@ -18,7 +18,6 @@ package worker import ( "bytes" - "errors" "fmt" "sort" "strconv" @@ -42,6 +41,7 @@ import ( cindex "github.com/google/codesearch/index" cregexp "github.com/google/codesearch/regexp" + "github.com/pkg/errors" "golang.org/x/net/context" ) @@ -56,7 +56,7 @@ func invokeNetworkRequest(ctx context.Context, addr string, f func(context.Context, pb.WorkerClient) (interface{}, error)) (interface{}, error) { pl, err := conn.GetPools().Get(addr) if err != nil { - return &emptyResult, x.Wrapf(err, "dispatchTaskOverNetwork: while retrieving connection.") + return &emptyResult, errors.Wrapf(err, "dispatchTaskOverNetwork: while retrieving connection.") } conn := pl.Get() @@ -179,7 +179,7 @@ func convertValue(attr, data string) (types.Val, error) { return types.Val{}, err } if !t.IsScalar() { - return types.Val{}, x.Errorf("Attribute %s is not valid scalar type", attr) + return types.Val{}, errors.Errorf("Attribute %s is not valid scalar type", attr) } src := types.Val{Tid: types.StringID, Value: []byte(data)} dst, err := types.Convert(src, t) @@ -203,7 +203,7 @@ func convertToType(v types.Val, typ types.TypeID) (*pb.TaskValue, error) { data := types.ValueForType(types.BinaryID) err = types.Marshal(val, &data) if err != nil { - return result, x.Errorf("Failed convertToType during Marshal") + return result, errors.Errorf("Failed convertToType during Marshal") } result.Val = data.Value.([]byte) return result, nil @@ -316,7 +316,7 @@ func (srcFn *functionContext) needsValuePostings(typ types.TypeID) (bool, error) case notAFunction: return typ.IsScalar(), nil } - return false, x.Errorf("Unhandled case in fetchValuePostings for fn: %s", srcFn.fname) + return false, errors.Errorf("Unhandled case in fetchValuePostings for fn: %s", srcFn.fname) } // Handles fetching of value posting lists and filtering of uids based on that. @@ -334,7 +334,7 @@ func (qs *queryState) handleValuePostings(ctx context.Context, args funcArgs) er switch srcFn.fnType { case notAFunction, aggregatorFn, passwordFn, compareAttrFn: default: - return x.Errorf("Unhandled function in handleValuePostings: %s", srcFn.fname) + return errors.Errorf("Unhandled function in handleValuePostings: %s", srcFn.fname) } if srcFn.atype == types.PasswordID && srcFn.fnType != passwordFn { @@ -342,8 +342,8 @@ func (qs *queryState) handleValuePostings(ctx context.Context, args funcArgs) er return nil } if srcFn.fnType == passwordFn && srcFn.atype != types.PasswordID { - return x.Errorf("checkpwd fn can only be used on attr: [%s] with schema type password."+ - " Got type: %s", q.Attr, types.TypeID(srcFn.atype).Name()) + return errors.Errorf("checkpwd fn can only be used on attr: [%s] with schema type "+ + "password. Got type: %s", q.Attr, types.TypeID(srcFn.atype).Name()) } if srcFn.n == 0 { return nil @@ -441,7 +441,7 @@ func (qs *queryState) handleValuePostings(ctx context.Context, args funcArgs) er if q.FacetsFilter != nil { // else part means isValueEdge // This is Value edge and we are asked to do facet filtering. Not supported. - return x.Errorf("Facet filtering is not supported on values.") + return errors.Errorf("Facet filtering is not supported on values.") } // add facets to result. @@ -575,7 +575,7 @@ func (qs *queryState) handleUidPostings( compareAttrFn: key = x.IndexKey(q.Attr, srcFn.tokens[i]) default: - return x.Errorf("Unhandled function in handleUidPostings: %s", srcFn.fname) + return errors.Errorf("Unhandled function in handleUidPostings: %s", srcFn.fname) } // Get or create the posting list for an entity, attribute combination. @@ -775,15 +775,15 @@ func (qs *queryState) helpProcessTask( } if q.Reverse && !schema.State().IsReversed(attr) { - return nil, x.Errorf("Predicate %s doesn't have reverse edge", attr) + return nil, errors.Errorf("Predicate %s doesn't have reverse edge", attr) } if needsIndex(srcFn.fnType) && !schema.State().IsIndexed(q.Attr) { - return nil, x.Errorf("Predicate %s is not indexed", q.Attr) + return nil, errors.Errorf("Predicate %s is not indexed", q.Attr) } if len(q.Langs) > 0 && !schema.State().HasLang(attr) { - return nil, x.Errorf("Language tags can only be used with predicates of string type"+ + return nil, errors.Errorf("Language tags can only be used with predicates of string type"+ " having @lang directive in schema. Got: [%v]", attr) } @@ -904,7 +904,7 @@ func needsStringFiltering(srcFn *functionContext, langs []string, attr string) b func (qs *queryState) handleCompareScalarFunction(arg funcArgs) error { attr := arg.q.Attr if ok := schema.State().HasCount(attr); !ok { - return x.Errorf("Need @count directive in schema for attr: %s for fn: %s at root", + return errors.Errorf("Need @count directive in schema for attr: %s for fn: %s at root", attr, arg.srcFn.fname) } count := arg.srcFn.threshold @@ -931,10 +931,10 @@ func (qs *queryState) handleRegexFunction(ctx context.Context, arg funcArgs) err typ, err := schema.State().TypeOf(attr) span.Annotatef(nil, "Attr: %s. Type: %s", attr, typ.Name()) if err != nil || !typ.IsScalar() { - return x.Errorf("Attribute not scalar: %s %v", attr, typ) + return errors.Errorf("Attribute not scalar: %s %v", attr, typ) } if typ != types.StringID { - return x.Errorf("Got non-string type. Regex match is allowed only on string type.") + return errors.Errorf("Got non-string type. Regex match is allowed only on string type.") } useIndex := schema.State().HasTokenizer(tok.IdentTrigram, attr) span.Annotatef(nil, "Trigram index found: %t, func at root: %t", @@ -959,7 +959,7 @@ func (qs *queryState) handleRegexFunction(ctx context.Context, arg funcArgs) err // No index and at root, return error instructing user to use `has` or index. default: - return x.Errorf( + return errors.Errorf( "Attribute %v does not have trigram index for regex matching. "+ "Please add a trigram index or use has/uid function with regexp() as filter.", attr) @@ -1039,7 +1039,7 @@ func (qs *queryState) handleCompareFunction(ctx context.Context, arg funcArgs) e // Need to evaluate inequality for entries in the first bucket. typ, err := schema.State().TypeOf(attr) if err != nil || !typ.IsScalar() { - return x.Errorf("Attribute not scalar: %s %v", attr, typ) + return errors.Errorf("Attribute not scalar: %s %v", attr, typ) } x.AssertTrue(len(arg.out.UidMatrix) > 0) @@ -1158,10 +1158,10 @@ func (qs *queryState) handleMatchFunction(ctx context.Context, arg funcArgs) err uids := &pb.List{} switch { case !typ.IsScalar(): - return x.Errorf("Attribute not scalar: %s %v", attr, typ) + return errors.Errorf("Attribute not scalar: %s %v", attr, typ) case typ != types.StringID: - return x.Errorf("Got non-string type. Fuzzy match is allowed only on string type.") + return errors.Errorf("Got non-string type. Fuzzy match is allowed only on string type.") case arg.q.UidList != nil && len(arg.q.UidList.Uids) != 0: uids = arg.q.UidList @@ -1174,7 +1174,7 @@ func (qs *queryState) handleMatchFunction(ctx context.Context, arg funcArgs) err } default: - return x.Errorf( + return errors.Errorf( "Attribute %v does not have trigram index for fuzzy matching. "+ "Please add a trigram index or use has/uid function with match() as filter.", attr) @@ -1397,7 +1397,7 @@ const ( func ensureArgsCount(srcFunc *pb.SrcFunction, expected int) error { if len(srcFunc.Args) != expected { - return x.Errorf("Function '%s' requires %d arguments, but got %d (%v)", + return errors.Errorf("Function '%s' requires %d arguments, but got %d (%v)", srcFunc.Name, expected, len(srcFunc.Args), srcFunc.Args) } return nil @@ -1440,10 +1440,10 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { // confirm agrregator could apply on the attributes typ, err := schema.State().TypeOf(attr) if err != nil { - return nil, x.Errorf("Attribute %q is not scalar-type", attr) + return nil, errors.Errorf("Attribute %q is not scalar-type", attr) } if !couldApplyAggregatorOn(f, typ) { - return nil, x.Errorf("Aggregator %q could not apply on %v", + return nil, errors.Errorf("Aggregator %q could not apply on %v", f, attr) } fc.n = len(q.UidList.Uids) @@ -1452,11 +1452,11 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { // Only eq can have multiple args. It should have atleast one. if fc.fname == eq { if len(args) < 1 { - return nil, x.Errorf("eq expects atleast 1 argument.") + return nil, errors.Errorf("eq expects atleast 1 argument.") } } else { // Others can have only 1 arg. if len(args) != 1 { - return nil, x.Errorf("%+v expects only 1 argument. Got: %+v", + return nil, errors.Errorf("%+v expects only 1 argument. Got: %+v", fc.fname, args) } } @@ -1465,7 +1465,7 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { // eq can have multiple args. for _, arg := range args { if fc.ineqValue, err = convertValue(attr, arg); err != nil { - return nil, x.Errorf("Got error: %v while running: %v", err, + return nil, errors.Errorf("Got error: %v while running: %v", err, q.SrcFunc) } // Get tokens ge / le ineqValueToken. @@ -1496,7 +1496,7 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { return nil, err } if fc.threshold, err = strconv.ParseInt(q.SrcFunc.Args[0], 0, 64); err != nil { - return nil, x.Wrapf(err, "Compare %v(%v) require digits, but got invalid num", + return nil, errors.Wrapf(err, "Compare %v(%v) require digits, but got invalid num", q.SrcFunc.Name, q.SrcFunc.Args[0]) } checkRoot(q, fc) @@ -1521,7 +1521,7 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { } required, found := verifyStringIndex(attr, fnType) if !found { - return nil, x.Errorf("Attribute %s is not indexed with type %s", attr, required) + return nil, errors.Errorf("Attribute %s is not indexed with type %s", attr, required) } if fc.tokens, err = getStringTokens(q.SrcFunc.Args, langForFunc(q.Langs), fnType); err != nil { return nil, err @@ -1534,7 +1534,7 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { } required, found := verifyStringIndex(attr, fnType) if !found { - return nil, x.Errorf("Attribute %s is not indexed with type %s", attr, required) + return nil, errors.Errorf("Attribute %s is not indexed with type %s", attr, required) } fc.intersectDest = needsIntersect(f) // Max Levenshtein distance @@ -1542,10 +1542,10 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { s, q.SrcFunc.Args = q.SrcFunc.Args[1], q.SrcFunc.Args[:1] max, err := strconv.ParseInt(s, 10, 32) if err != nil { - return nil, x.Errorf("Levenshtein distance value must be an int, got %v", s) + return nil, errors.Errorf("Levenshtein distance value must be an int, got %v", s) } if max < 0 { - return nil, x.Errorf("Levenshtein distance value must be greater than 0, got %v", s) + return nil, errors.Errorf("Levenshtein distance value must be greater than 0, got %v", s) } fc.threshold = int64(max) fc.tokens = q.SrcFunc.Args @@ -1556,7 +1556,7 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { } tokerName := q.SrcFunc.Args[0] if !verifyCustomIndex(q.Attr, tokerName) { - return nil, x.Errorf("Attribute %s is not indexed with custom tokenizer %s", + return nil, errors.Errorf("Attribute %s is not indexed with custom tokenizer %s", q.Attr, tokerName) } valToTok, err := convertValue(q.Attr, q.SrcFunc.Args[1]) @@ -1565,7 +1565,7 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { } tokenizer, ok := tok.GetTokenizer(tokerName) if !ok { - return nil, x.Errorf("Could not find tokenizer with name %q", tokerName) + return nil, errors.Errorf("Could not find tokenizer with name %q", tokerName) } fc.tokens, _ = tok.BuildTokens(valToTok.Value, tok.GetLangTokenizer(tokenizer, langForFunc(q.Langs))) @@ -1581,7 +1581,7 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { if modifiers == "i" { ignoreCase = true } else { - return nil, x.Errorf("Invalid regexp modifier: %s", modifiers) + return nil, errors.Errorf("Invalid regexp modifier: %s", modifiers) } } matchType := "(?m)" // this is cregexp library specific @@ -1604,17 +1604,17 @@ func parseSrcFn(q *pb.Query) (*functionContext, error) { fc.uidPresent, err = strconv.ParseUint(q.SrcFunc.Args[0], 0, 64) if err != nil { if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrSyntax { - return nil, x.Errorf("Value %q in %s is not a number", + return nil, errors.Errorf("Value %q in %s is not a number", q.SrcFunc.Args[0], q.SrcFunc.Name) } return nil, err } checkRoot(q, fc) if fc.isFuncAtRoot { - return nil, x.Errorf("uid_in function not allowed at root") + return nil, errors.Errorf("uid_in function not allowed at root") } default: - return nil, x.Errorf("FnType %d not handled in numFnAttrs.", fnType) + return nil, errors.Errorf("FnType %d not handled in numFnAttrs.", fnType) } return fc, nil } @@ -1716,7 +1716,7 @@ func applyFacetsTree(postingFacets []*api.Facet, ftree *facetsTree) (bool, error } return filterOnStandardFn(fname, fc.Tokens, ftree.function.tokens) } - return false, x.Errorf("Fn %s not supported in facets filtering.", fname) + return false, errors.Errorf("Fn %s not supported in facets filtering.", fname) } var res []bool @@ -1737,7 +1737,7 @@ func applyFacetsTree(postingFacets []*api.Facet, ftree *facetsTree) (bool, error case "or": return res[0] || res[1], nil } - return false, x.Errorf("Unexpected behavior in applyFacetsTree.") + return false, errors.Errorf("Unexpected behavior in applyFacetsTree.") } // filterOnStandardFn : tells whether facet corresponding to fcTokens can be taken or not. @@ -1775,7 +1775,7 @@ func filterOnStandardFn(fname string, fcTokens []string, argTokens []string) (bo } return false, nil } - return false, x.Errorf("Fn %s not supported in facets filtering.", fname) + return false, errors.Errorf("Fn %s not supported in facets filtering.", fname) } type facetsFunc struct { @@ -1805,7 +1805,7 @@ func preprocessFilter(tree *pb.FilterTree) (*facetsTree, error) { fnType, fname := parseFuncTypeHelper(ftree.function.name) if len(tree.Func.Args) != 1 { - return nil, x.Errorf("One argument expected in %s, but got %d.", + return nil, errors.Errorf("One argument expected in %s, but got %d.", fname, len(tree.Func.Args)) } @@ -1820,7 +1820,7 @@ func preprocessFilter(tree *pb.FilterTree) (*facetsTree, error) { sort.Strings(argTokens) ftree.function.tokens = argTokens default: - return nil, x.Errorf("Fn %s not supported in preprocessFilter.", fname) + return nil, errors.Errorf("Fn %s not supported in preprocessFilter.", fname) } return ftree, nil } @@ -1837,18 +1837,18 @@ func preprocessFilter(tree *pb.FilterTree) (*facetsTree, error) { switch strings.ToLower(tree.Op) { case "not": if numChild != 1 { - return nil, x.Errorf("Expected 1 child for not but got %d.", numChild) + return nil, errors.Errorf("Expected 1 child for not but got %d.", numChild) } case "and": if numChild != 2 { - return nil, x.Errorf("Expected 2 child for not but got %d.", numChild) + return nil, errors.Errorf("Expected 2 child for not but got %d.", numChild) } case "or": if numChild != 2 { - return nil, x.Errorf("Expected 2 child for not but got %d.", numChild) + return nil, errors.Errorf("Expected 2 child for not but got %d.", numChild) } default: - return nil, x.Errorf("Unsupported operation in facet filtering: %s.", tree.Op) + return nil, errors.Errorf("Unsupported operation in facet filtering: %s.", tree.Op) } return ftree, nil } @@ -1880,7 +1880,7 @@ func (qs *queryState) evaluate(cp countParams, out *pb.Result) error { x.AssertTruef(false, "unhandled count comparison fn: %v", cp.fn) } if illegal { - return x.Errorf("count(predicate) cannot be used to search for " + + return errors.Errorf("count(predicate) cannot be used to search for " + "negative counts (nonsensical) or zero counts (not tracked).") } diff --git a/worker/tokens.go b/worker/tokens.go index 9acd9757ab1..616db91b40e 100644 --- a/worker/tokens.go +++ b/worker/tokens.go @@ -25,6 +25,7 @@ import ( "github.com/dgraph-io/dgraph/tok" "github.com/dgraph-io/dgraph/types" "github.com/dgraph-io/dgraph/x" + "github.com/pkg/errors" ) func verifyStringIndex(attr string, funcType FuncType) (string, bool) { @@ -78,7 +79,7 @@ func getStringTokens(funcArgs []string, lang string, funcType FuncType) ([]strin func pickTokenizer(attr string, f string) (tok.Tokenizer, error) { // Get the tokenizers and choose the corresponding one. if !schema.State().IsIndexed(attr) { - return nil, x.Errorf("Attribute %s is not indexed.", attr) + return nil, errors.Errorf("Attribute %s is not indexed.", attr) } tokenizers := schema.State().Tokenizer(attr) @@ -100,7 +101,7 @@ func pickTokenizer(attr string, f string) (tok.Tokenizer, error) { // Should we return an error if we don't find a non-lossy tokenizer for eq function. if f != "eq" { - return nil, x.Errorf("Attribute:%s does not have proper index for comparison", attr) + return nil, errors.Errorf("Attribute:%s does not have proper index for comparison", attr) } // We didn't find a sortable or !isLossy() tokenizer, lets return the first one. @@ -133,7 +134,7 @@ func getInequalityTokens(readTs uint64, attr, f string, break case len(ineqTokens) > 1: - return nil, "", x.Errorf("Attribute %s does not have a valid tokenizer.", attr) + return nil, "", errors.Errorf("Attribute %s does not have a valid tokenizer.", attr) } ineqToken := ineqTokens[0] diff --git a/x/config.go b/x/config.go index c25f1ba60de..72405fc8d01 100644 --- a/x/config.go +++ b/x/config.go @@ -23,7 +23,6 @@ import ( // Options stores the options for this package. type Options struct { - DebugMode bool // PortOffset will be used to determine the ports to use (port = default port + offset). PortOffset int // QueryEdgeLimit is the maximum number of edges that will be traversed during diff --git a/x/error.go b/x/error.go index a3b7de6cbac..70f7bdd4538 100644 --- a/x/error.go +++ b/x/error.go @@ -25,8 +25,8 @@ package x // more common in Go. If you want to check for boolean being true, use // x.Assert, x.Assertf. // (2) You receive an error from external lib, and would like to pass on with some -// stack trace information. In this case, use x.Wrap or x.Wrapf. -// (3) You want to generate a new error with stack trace info. Use x.Errorf. +// stack trace information. In this case, use x.Wrap or errors.Wrapf. +// (3) You want to generate a new error with stack trace info. Use errors.Errorf. import ( "fmt" @@ -39,14 +39,14 @@ import ( // Check logs fatal if err != nil. func Check(err error) { if err != nil { - log.Fatalf("%+v", Wrap(err)) + log.Fatalf("%+v", errors.Wrap(err, "")) } } // Checkf is Check with extra info. func Checkf(err error, format string, args ...interface{}) { if err != nil { - log.Fatalf("%+v", Wrapf(err, format, args...)) + log.Fatalf("%+v", errors.Wrapf(err, format, args...)) } } @@ -96,30 +96,6 @@ func AssertTruefNoTrace(b bool, format string, args ...interface{}) { } } -// Wrap wraps errors from external lib. -func Wrap(err error) error { - return errors.Wrap(err, "") -} - -// Wrapf is Wrap with extra info. -func Wrapf(err error, format string, args ...interface{}) error { - if err == nil { - return nil - } - if !Config.DebugMode { - return fmt.Errorf(format+" error: %+v", append(args, err)...) - } - return errors.Wrapf(err, format, args...) -} - -// Errorf creates a new error with stack trace, etc. -func Errorf(format string, args ...interface{}) error { - if !Config.DebugMode { - return fmt.Errorf(format, args...) - } - return errors.Errorf(format, args...) -} - // Fatalf logs fatal. func Fatalf(format string, args ...interface{}) { log.Fatalf("%+v", errors.Errorf(format, args...)) diff --git a/x/keys.go b/x/keys.go index 46cb6f7fdeb..5a3bd822344 100644 --- a/x/keys.go +++ b/x/keys.go @@ -18,9 +18,10 @@ package x import ( "encoding/binary" - "fmt" "math" "strings" + + "github.com/golang/glog" ) const ( @@ -421,9 +422,7 @@ func Parse(key []byte) *ParsedKey { switch p.byteType { case ByteData, ByteReverse: if len(k) < 8 { - if Config.DebugMode { - fmt.Printf("Error: Uid length < 8 for key: %q, parsed key: %+v\n", key, p) - } + glog.Errorf("Error: Uid length < 8 for key: %q, parsed key: %+v\n", key, p) return nil } p.Uid = binary.BigEndian.Uint64(k) @@ -433,10 +432,7 @@ func Parse(key []byte) *ParsedKey { } if len(k) < 16 { - // TODO(martinmr): replace DebugMode with glog.vlog(2). - if Config.DebugMode { - fmt.Printf("Error: StartUid length < 8 for key: %q, parsed key: %+v\n", key, p) - } + glog.Errorf("Error: StartUid length < 8 for key: %q, parsed key: %+v\n", key, p) return nil } @@ -449,9 +445,7 @@ func Parse(key []byte) *ParsedKey { } if len(k) < 8 { - if Config.DebugMode { - fmt.Printf("Error: StartUid length < 8 for key: %q, parsed key: %+v\n", key, p) - } + glog.Errorf("Error: StartUid length < 8 for key: %q, parsed key: %+v\n", key, p) return nil } @@ -461,9 +455,7 @@ func Parse(key []byte) *ParsedKey { p.StartUid = binary.BigEndian.Uint64(startUid) case ByteCount, ByteCountRev: if len(k) < 4 { - if Config.DebugMode { - fmt.Printf("Error: Count length < 4 for key: %q, parsed key: %+v\n", key, p) - } + glog.Errorf("Error: Count length < 4 for key: %q, parsed key: %+v\n", key, p) return nil } p.Count = binary.BigEndian.Uint32(k) @@ -473,9 +465,7 @@ func Parse(key []byte) *ParsedKey { } if len(k) < 12 { - if Config.DebugMode { - fmt.Printf("Error: StartUid length < 8 for key: %q, parsed key: %+v\n", key, p) - } + glog.Errorf("Error: StartUid length < 8 for key: %q, parsed key: %+v\n", key, p) return nil }