diff --git a/api/http/handlerfuncs_test.go b/api/http/handlerfuncs_test.go index 74c85770fe..027e5d2a07 100644 --- a/api/http/handlerfuncs_test.go +++ b/api/http/handlerfuncs_test.go @@ -27,6 +27,7 @@ import ( "github.com/sourcenetwork/defradb/client" badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/db" + "github.com/sourcenetwork/defradb/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -188,7 +189,7 @@ func TestExecGQLWithMockBody(t *testing.T) { env = "dev" mockReadCloser := mockReadCloser{} // if Read is called, it will return error - mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, fmt.Errorf("error reading")) + mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, errors.New("error reading")) errResponse := ErrorResponse{} testRequest(testOptions{ @@ -478,7 +479,7 @@ func TestLoadSchemaHandlerWithReadBodyError(t *testing.T) { env = "dev" mockReadCloser := mockReadCloser{} // if Read is called, it will return error - mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, fmt.Errorf("error reading")) + mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, errors.New("error reading")) errResponse := ErrorResponse{} testRequest(testOptions{ diff --git a/cli/addreplicator.go b/cli/addreplicator.go index 6130a2c1d0..07dd489837 100644 --- a/cli/addreplicator.go +++ b/cli/addreplicator.go @@ -12,13 +12,13 @@ package cli import ( "context" - "fmt" ma "github.com/multiformats/go-multiaddr" "github.com/spf13/cobra" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" netclient "github.com/sourcenetwork/defradb/net/api/client" ) @@ -33,12 +33,12 @@ for the p2p data sync system.`, if err := cmd.Usage(); err != nil { return err } - return fmt.Errorf("must specify two arguments: collection and peer") + return errors.New("must specify two arguments: collection and peer") } collection := args[0] peerAddr, err := ma.NewMultiaddr(args[1]) if err != nil { - return fmt.Errorf("could not parse peer address: %w", err) + return errors.Wrap("could not parse peer address", err) } log.FeedbackInfo( @@ -52,12 +52,12 @@ for the p2p data sync system.`, cred := insecure.NewCredentials() client, err := netclient.NewClient(cfg.Net.RPCAddress, grpc.WithTransportCredentials(cred)) if err != nil { - return fmt.Errorf("failed to create RPC client: %w", err) + return errors.Wrap("failed to create RPC client", err) } rpcTimeoutDuration, err := cfg.Net.RPCTimeoutDuration() if err != nil { - return fmt.Errorf("failed to parse RPC timeout duration: %w", err) + return errors.Wrap("failed to parse RPC timeout duration", err) } ctx, cancel := context.WithTimeout(cmd.Context(), rpcTimeoutDuration) @@ -65,7 +65,7 @@ for the p2p data sync system.`, pid, err := client.AddReplicator(ctx, collection, peerAddr) if err != nil { - return fmt.Errorf("failed to add replicator, request failed: %w", err) + return errors.Wrap("failed to add replicator, request failed", err) } log.FeedbackInfo(ctx, "Successfully added replicator", logging.NewKV("PID", pid)) return nil diff --git a/cli/blocks_get.go b/cli/blocks_get.go index ad5f2b3a91..890bbc963c 100644 --- a/cli/blocks_get.go +++ b/cli/blocks_get.go @@ -17,6 +17,7 @@ import ( "os" httpapi "github.com/sourcenetwork/defradb/api/http" + "github.com/sourcenetwork/defradb/errors" "github.com/spf13/cobra" ) @@ -28,45 +29,45 @@ var getCmd = &cobra.Command{ if err = cmd.Usage(); err != nil { return err } - return fmt.Errorf("get requires a CID argument") + return errors.New("get requires a CID argument") } cid := args[0] endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.BlocksPath, cid) if err != nil { - return fmt.Errorf("failed to join endpoint: %w", err) + return errors.Wrap("failed to join endpoint", err) } res, err := http.Get(endpoint.String()) if err != nil { - return fmt.Errorf("failed to send get request: %w", err) + return errors.Wrap("failed to send get request", err) } defer func() { if e := res.Body.Close(); e != nil { - err = fmt.Errorf("failed to read response body: %v: %w", e.Error(), err) + err = errors.Wrap(fmt.Sprintf("failed to read response body: %v", e.Error()), err) } }() response, err := io.ReadAll(res.Body) if err != nil { - return fmt.Errorf("failed to read response body: %w", err) + return errors.Wrap("failed to read response body", err) } stdout, err := os.Stdout.Stat() if err != nil { - return fmt.Errorf("failed to stat stdout: %w", err) + return errors.Wrap("failed to stat stdout", err) } if isFileInfoPipe(stdout) { cmd.Println(string(response)) } else { graphlErr, err := hasGraphQLErrors(response) if err != nil { - return fmt.Errorf("failed to handle GraphQL errors: %w", err) + return errors.Wrap("failed to handle GraphQL errors", err) } indentedResult, err := indentJSON(response) if err != nil { - return fmt.Errorf("failed to pretty print response: %w", err) + return errors.Wrap("failed to pretty print response", err) } if graphlErr { log.FeedbackError(cmd.Context(), indentedResult) diff --git a/cli/cli.go b/cli/cli.go index f288010551..5619a75434 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -24,6 +24,7 @@ import ( "strings" "github.com/sourcenetwork/defradb/config" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/spf13/cobra" ) @@ -57,7 +58,7 @@ func readStdin() (string, error) { s.Write(scanner.Bytes()) } if err := scanner.Err(); err != nil { - return "", fmt.Errorf("reading standard input: %w", err) + return "", errors.Wrap("reading standard input", err) } return s.String(), nil } @@ -76,7 +77,7 @@ func hasGraphQLErrors(buf []byte) (bool, error) { errs := graphqlErrors{} err := json.Unmarshal(buf, &errs) if err != nil { - return false, fmt.Errorf("couldn't parse GraphQL response %w", err) + return false, errors.Wrap("couldn't parse GraphQL response %w", err) } if errs.Errors != nil { return true, nil @@ -100,7 +101,7 @@ func parseAndConfigLog(ctx context.Context, cfg *config.LoggingConfig, cmd *cobr // handle --logger ,=,... --logger ,=,.. loggerKVs, err := cmd.Flags().GetStringArray("logger") if err != nil { - return fmt.Errorf("can't get logger flag: %w", err) + return errors.Wrap("can't get logger flag", err) } for _, kvs := range loggerKVs { @@ -111,7 +112,7 @@ func parseAndConfigLog(ctx context.Context, cfg *config.LoggingConfig, cmd *cobr loggingConfig, err := cfg.ToLoggerConfig() if err != nil { - return fmt.Errorf("could not get logging config: %w", err) + return errors.Wrap("could not get logging config", err) } logging.SetConfig(loggingConfig) @@ -125,7 +126,7 @@ func parseAndConfigLogAllParams(ctx context.Context, cfg *config.LoggingConfig, parsed := strings.Split(kvs, ",") if len(parsed) <= 1 { - return fmt.Errorf("logger was not provided as comma-separated pairs of =: %s", kvs) + return errors.New(fmt.Sprintf("logger was not provided as comma-separated pairs of =: %s", kvs)) } name := parsed[0] @@ -134,12 +135,12 @@ func parseAndConfigLogAllParams(ctx context.Context, cfg *config.LoggingConfig, for _, kv := range parsed[1:] { parsedKV := strings.Split(kv, "=") if len(parsedKV) != 2 { - return fmt.Errorf("level was not provided as = pair: %s", kv) + return errors.New(fmt.Sprintf("level was not provided as = pair: %s", kv)) } logcfg, err := cfg.GetOrCreateNamedLogger(name) if err != nil { - return fmt.Errorf("could not get named logger config: %w", err) + return errors.Wrap("could not get named logger config", err) } // handle field @@ -153,23 +154,23 @@ func parseAndConfigLogAllParams(ctx context.Context, cfg *config.LoggingConfig, case "stacktrace": // bool boolValue, err := strconv.ParseBool(parsedKV[1]) if err != nil { - return fmt.Errorf("couldn't parse kv bool: %w", err) + return errors.Wrap("couldn't parse kv bool", err) } logcfg.Stacktrace = boolValue case "nocolor": // bool boolValue, err := strconv.ParseBool(parsedKV[1]) if err != nil { - return fmt.Errorf("couldn't parse kv bool: %w", err) + return errors.Wrap("couldn't parse kv bool", err) } logcfg.NoColor = boolValue case "caller": // bool boolValue, err := strconv.ParseBool(parsedKV[1]) if err != nil { - return fmt.Errorf("couldn't parse kv bool: %w", err) + return errors.Wrap("couldn't parse kv bool", err) } logcfg.Caller = boolValue default: - return fmt.Errorf("unknown parameter for logger: %s", param) + return errors.New(fmt.Sprintf("unknown parameter for logger: %s", param)) } } return nil @@ -197,12 +198,12 @@ func parseAndConfigLogStringParam( for _, kv := range parsed[1:] { parsedKV := strings.Split(kv, "=") if len(parsedKV) != 2 { - return fmt.Errorf("level was not provided as = pair: %s", kv) + return errors.New(fmt.Sprintf("level was not provided as = pair: %s", kv)) } logcfg, err := cfg.GetOrCreateNamedLogger(parsedKV[0]) if err != nil { - return fmt.Errorf("could not get named logger config: %w", err) + return errors.Wrap("could not get named logger config", err) } paramSetterFn(&logcfg.LoggingConfig, parsedKV[1]) diff --git a/cli/dump.go b/cli/dump.go index 8e5f96f8e6..3f46deb60c 100644 --- a/cli/dump.go +++ b/cli/dump.go @@ -18,6 +18,7 @@ import ( "os" httpapi "github.com/sourcenetwork/defradb/api/http" + "github.com/sourcenetwork/defradb/errors" "github.com/spf13/cobra" ) @@ -27,7 +28,7 @@ var dumpCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, _ []string) (err error) { stdout, err := os.Stdout.Stat() if err != nil { - return fmt.Errorf("failed to stat stdout: %w", err) + return errors.Wrap("failed to stat stdout", err) } if !isFileInfoPipe(stdout) { log.FeedbackInfo(cmd.Context(), "Requesting the database to dump its state, server-side...") @@ -35,23 +36,23 @@ var dumpCmd = &cobra.Command{ endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.DumpPath) if err != nil { - return fmt.Errorf("failed to join endpoint: %w", err) + return errors.Wrap("failed to join endpoint", err) } res, err := http.Get(endpoint.String()) if err != nil { - return fmt.Errorf("failed dump request: %w", err) + return errors.Wrap("failed dump request", err) } defer func() { if e := res.Body.Close(); e != nil { - err = fmt.Errorf("failed to read response body: %v: %w", e.Error(), err) + err = errors.Wrap(fmt.Sprintf("failed to read response body: %v", e.Error()), err) } }() response, err := io.ReadAll(res.Body) if err != nil { - return fmt.Errorf("failed to read response body: %w", err) + return errors.Wrap("failed to read response body", err) } if isFileInfoPipe(stdout) { @@ -66,7 +67,7 @@ var dumpCmd = &cobra.Command{ r := dumpResponse{} err = json.Unmarshal(response, &r) if err != nil { - return fmt.Errorf("failed parsing of response: %w", err) + return errors.Wrap("failed parsing of response", err) } log.FeedbackInfo(cmd.Context(), r.Data.Response) } diff --git a/cli/init.go b/cli/init.go index ce19565f2f..fa0b43c4fb 100644 --- a/cli/init.go +++ b/cli/init.go @@ -15,6 +15,7 @@ import ( "os" "github.com/sourcenetwork/defradb/config" + "github.com/sourcenetwork/defradb/errors" "github.com/spf13/cobra" ) @@ -36,7 +37,7 @@ var initCmd = &cobra.Command{ PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { err := cfg.LoadWithoutRootDir() if err != nil { - return fmt.Errorf("failed to load configuration: %w", err) + return errors.Wrap("failed to load configuration", err) } // parse loglevel overrides. @@ -51,11 +52,11 @@ var initCmd = &cobra.Command{ if err := cmd.Usage(); err != nil { return err } - return fmt.Errorf("init command requires one rootdir argument, or no argument") + return errors.New("init command requires one rootdir argument, or no argument") } rootDir, rootDirExists, err := config.GetRootDir(rootDirPath) if err != nil { - return fmt.Errorf("failed to get root dir: %w", err) + return errors.Wrap("failed to get root dir", err) } if rootDirExists { // we assume the config file is using its default path in the rootdir @@ -66,11 +67,11 @@ var initCmd = &cobra.Command{ if reinitialize { err = os.Remove(configFilePath) if err != nil { - return fmt.Errorf("failed to remove configuration file: %w", err) + return errors.Wrap("failed to remove configuration file", err) } err = cfg.WriteConfigFileToRootDir(rootDir) if err != nil { - return fmt.Errorf("failed to create configuration file: %w", err) + return errors.Wrap("failed to create configuration file", err) } log.FeedbackInfo(cmd.Context(), fmt.Sprintf("Reinitialized configuration file at %v", configFilePath)) } else { @@ -85,14 +86,14 @@ var initCmd = &cobra.Command{ } else { err = cfg.WriteConfigFileToRootDir(rootDir) if err != nil { - return fmt.Errorf("failed to create configuration file: %w", err) + return errors.Wrap("failed to create configuration file", err) } log.FeedbackInfo(cmd.Context(), fmt.Sprintf("Initialized configuration file at %v", configFilePath)) } } else { err = config.CreateRootDirWithDefaultConfig(rootDir) if err != nil { - return fmt.Errorf("failed to create root dir: %w", err) + return errors.Wrap("failed to create root dir", err) } log.FeedbackInfo(cmd.Context(), fmt.Sprintf("Created DefraDB root directory at %v", rootDir)) } diff --git a/cli/peerid.go b/cli/peerid.go index 26f02ea79c..9ed2186385 100644 --- a/cli/peerid.go +++ b/cli/peerid.go @@ -12,12 +12,13 @@ package cli import ( "encoding/json" - "errors" "fmt" "io" "net/http" "os" + "github.com/sourcenetwork/defradb/errors" + httpapi "github.com/sourcenetwork/defradb/api/http" "github.com/spf13/cobra" ) @@ -28,7 +29,7 @@ var peerIDCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, _ []string) (err error) { stdout, err := os.Stdout.Stat() if err != nil { - return fmt.Errorf("failed to stat stdout: %w", err) + return errors.Wrap("failed to stat stdout", err) } if !isFileInfoPipe(stdout) { log.FeedbackInfo(cmd.Context(), "Requesting peer ID...") @@ -36,36 +37,36 @@ var peerIDCmd = &cobra.Command{ endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.PeerIDPath) if err != nil { - return fmt.Errorf("failed to join endpoint: %w", err) + return errors.Wrap("failed to join endpoint", err) } res, err := http.Get(endpoint.String()) if err != nil { - return fmt.Errorf("failed to request peer ID: %w", err) + return errors.Wrap("failed to request peer ID", err) } defer func() { if e := res.Body.Close(); e != nil { - err = fmt.Errorf("failed to read response body: %v: %w", e.Error(), err) + err = errors.Wrap(fmt.Sprintf("failed to read response body: %v", e.Error()), err) } }() response, err := io.ReadAll(res.Body) if err != nil { - return fmt.Errorf("failed to read response body: %w", err) + return errors.Wrap("failed to read response body", err) } if res.StatusCode == http.StatusNotFound { r := httpapi.ErrorResponse{} err = json.Unmarshal(response, &r) if err != nil { - return fmt.Errorf("parsing of response failed: %w", err) + return errors.Wrap("parsing of response failed", err) } if len(r.Errors) > 0 { if isFileInfoPipe(stdout) { b, err := json.Marshal(r.Errors[0]) if err != nil { - return fmt.Errorf("mashalling error response failed: %w", err) + return errors.Wrap("mashalling error response failed", err) } cmd.Println(string(b)) } else { @@ -79,12 +80,12 @@ var peerIDCmd = &cobra.Command{ r := httpapi.DataResponse{} err = json.Unmarshal(response, &r) if err != nil { - return fmt.Errorf("parsing of response failed: %w", err) + return errors.Wrap("parsing of response failed", err) } if isFileInfoPipe(stdout) { b, err := json.Marshal(r.Data) if err != nil { - return fmt.Errorf("mashalling data response failed: %w", err) + return errors.Wrap("mashalling data response failed", err) } cmd.Println(string(b)) } else if data, ok := r.Data.(map[string]any); ok { diff --git a/cli/ping.go b/cli/ping.go index f571cd2740..45a6bc39b7 100644 --- a/cli/ping.go +++ b/cli/ping.go @@ -18,6 +18,7 @@ import ( "os" httpapi "github.com/sourcenetwork/defradb/api/http" + "github.com/sourcenetwork/defradb/errors" "github.com/spf13/cobra" ) @@ -27,7 +28,7 @@ var pingCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, _ []string) (err error) { stdout, err := os.Stdout.Stat() if err != nil { - return fmt.Errorf("failed to stat stdout: %w", err) + return errors.Wrap("failed to stat stdout", err) } if !isFileInfoPipe(stdout) { log.FeedbackInfo(cmd.Context(), "Sending ping...") @@ -35,23 +36,23 @@ var pingCmd = &cobra.Command{ endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.PingPath) if err != nil { - return fmt.Errorf("failed to join endpoint: %w", err) + return errors.Wrap("failed to join endpoint", err) } res, err := http.Get(endpoint.String()) if err != nil { - return fmt.Errorf("failed to send ping: %w", err) + return errors.Wrap("failed to send ping", err) } defer func() { if e := res.Body.Close(); e != nil { - err = fmt.Errorf("failed to read response body: %v: %w", e.Error(), err) + err = errors.Wrap(fmt.Sprintf("failed to read response body: %v", e.Error()), err) } }() response, err := io.ReadAll(res.Body) if err != nil { - return fmt.Errorf("failed to read response body: %w", err) + return errors.Wrap("failed to read response body", err) } if isFileInfoPipe(stdout) { @@ -65,7 +66,7 @@ var pingCmd = &cobra.Command{ r := pingResponse{} err = json.Unmarshal(response, &r) if err != nil { - return fmt.Errorf("parsing of response failed: %w", err) + return errors.Wrap("parsing of response failed", err) } log.FeedbackInfo(cmd.Context(), r.Data.Response) } diff --git a/cli/query.go b/cli/query.go index 37db13303d..7975ea1b36 100644 --- a/cli/query.go +++ b/cli/query.go @@ -20,6 +20,7 @@ import ( "github.com/spf13/cobra" httpapi "github.com/sourcenetwork/defradb/api/http" + "github.com/sourcenetwork/defradb/errors" ) var queryCmd = &cobra.Command{ @@ -49,7 +50,7 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so if err = cmd.Usage(); err != nil { return err } - return fmt.Errorf("too many arguments") + return errors.New("too many arguments") } if isFileInfoPipe(fi) && (len(args) == 0 || args[0] != "-") { @@ -61,16 +62,16 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so } else if len(args) == 0 { err := cmd.Help() if err != nil { - return fmt.Errorf("failed to print help: %w", err) + return errors.Wrap("failed to print help", err) } return nil } else if args[0] == "-" { stdin, err := readStdin() if err != nil { - return fmt.Errorf("failed to read stdin: %w", err) + return errors.Wrap("failed to read stdin", err) } if len(stdin) == 0 { - return fmt.Errorf("no query in stdin provided") + return errors.New("no query in stdin provided") } else { query = stdin } @@ -79,12 +80,12 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so } if query == "" { - return fmt.Errorf("query cannot be empty") + return errors.New("query cannot be empty") } endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.GraphQLPath) if err != nil { - return fmt.Errorf("joining paths failed: %w", err) + return errors.Wrap("joining paths failed", err) } p := url.Values{} @@ -93,23 +94,23 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so res, err := http.Get(endpoint.String()) if err != nil { - return fmt.Errorf("failed query: %w", err) + return errors.Wrap("failed query", err) } defer func() { if e := res.Body.Close(); e != nil { - err = fmt.Errorf("failed to read response body: %v: %w", e.Error(), err) + err = errors.Wrap(fmt.Sprintf("failed to read response body: %v", e.Error()), err) } }() response, err := io.ReadAll(res.Body) if err != nil { - return fmt.Errorf("failed to read response body: %w", err) + return errors.Wrap("failed to read response body", err) } fi, err = os.Stdout.Stat() if err != nil { - return fmt.Errorf("failed to stat stdout: %w", err) + return errors.Wrap("failed to stat stdout", err) } if isFileInfoPipe(fi) { @@ -117,11 +118,11 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so } else { graphlErr, err := hasGraphQLErrors(response) if err != nil { - return fmt.Errorf("failed to handle GraphQL errors: %w", err) + return errors.Wrap("failed to handle GraphQL errors", err) } indentedResult, err := indentJSON(response) if err != nil { - return fmt.Errorf("failed to pretty print result: %w", err) + return errors.Wrap("failed to pretty print result", err) } if graphlErr { log.FeedbackError(cmd.Context(), indentedResult) diff --git a/cli/root.go b/cli/root.go index b4f966d0a7..af6393b1f5 100644 --- a/cli/root.go +++ b/cli/root.go @@ -15,6 +15,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/config" + "github.com/sourcenetwork/defradb/errors" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -37,18 +38,18 @@ See https://docs.source.network/BSL.txt for more information. PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { rootDir, exists, err := config.GetRootDir(rootDirParam) if err != nil { - return fmt.Errorf("failed to get root dir: %w", err) + return errors.Wrap("failed to get root dir", err) } defaultConfig := false if exists { err := cfg.Load(rootDir) if err != nil { - return fmt.Errorf("failed to load config: %w", err) + return errors.Wrap("failed to load config", err) } } else { err := cfg.LoadWithoutRootDir() if err != nil { - return fmt.Errorf("failed to load config: %w", err) + return errors.Wrap("failed to load config", err) } defaultConfig = true } diff --git a/cli/schema_add.go b/cli/schema_add.go index 50a467b492..585637f486 100644 --- a/cli/schema_add.go +++ b/cli/schema_add.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" httpapi "github.com/sourcenetwork/defradb/api/http" + "github.com/sourcenetwork/defradb/errors" ) var schemaFile string @@ -51,13 +52,13 @@ To learn more about the DefraDB GraphQL Schema Language, refer to https://docs.s if err = cmd.Usage(); err != nil { return err } - return fmt.Errorf("too many arguments") + return errors.New("too many arguments") } if schemaFile != "" { buf, err := os.ReadFile(schemaFile) if err != nil { - return fmt.Errorf("failed to read schema file: %w", err) + return errors.Wrap("failed to read schema file", err) } schema = string(buf) } else if isFileInfoPipe(fi) && (len(args) == 0 || args[0] != "-") { @@ -70,16 +71,16 @@ To learn more about the DefraDB GraphQL Schema Language, refer to https://docs.s } else if len(args) == 0 { err := cmd.Help() if err != nil { - return fmt.Errorf("failed to print help: %w", err) + return errors.Wrap("failed to print help", err) } return nil } else if args[0] == "-" { stdin, err := readStdin() if err != nil { - return fmt.Errorf("failed to read stdin: %w", err) + return errors.Wrap("failed to read stdin", err) } if len(stdin) == 0 { - return fmt.Errorf("no schema in stdin provided") + return errors.New("no schema in stdin provided") } else { schema = stdin } @@ -88,45 +89,45 @@ To learn more about the DefraDB GraphQL Schema Language, refer to https://docs.s } if schema == "" { - return fmt.Errorf("empty schema provided") + return errors.New("empty schema provided") } endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.SchemaLoadPath) if err != nil { - return fmt.Errorf("join paths failed: %w", err) + return errors.Wrap("join paths failed", err) } res, err := http.Post(endpoint.String(), "text", strings.NewReader(schema)) if err != nil { - return fmt.Errorf("failed to post schema: %w", err) + return errors.Wrap("failed to post schema", err) } defer func() { if e := res.Body.Close(); e != nil { - err = fmt.Errorf("failed to read response body: %v: %w", e.Error(), err) + err = errors.Wrap(fmt.Sprintf("failed to read response body: %v", e.Error()), err) } }() response, err := io.ReadAll(res.Body) if err != nil { - return fmt.Errorf("failed to read response body: %w", err) + return errors.Wrap("failed to read response body", err) } stdout, err := os.Stdout.Stat() if err != nil { - return fmt.Errorf("failed to stat stdout: %w", err) + return errors.Wrap("failed to stat stdout", err) } if isFileInfoPipe(stdout) { cmd.Println(string(response)) } else { graphlErr, err := hasGraphQLErrors(response) if err != nil { - return fmt.Errorf("failed to handle GraphQL errors: %w", err) + return errors.Wrap("failed to handle GraphQL errors", err) } if graphlErr { indentedResult, err := indentJSON(response) if err != nil { - return fmt.Errorf("failed to pretty print result: %w", err) + return errors.Wrap("failed to pretty print result", err) } log.FeedbackError(cmd.Context(), indentedResult) } else { @@ -138,7 +139,7 @@ To learn more about the DefraDB GraphQL Schema Language, refer to https://docs.s r := schemaResponse{} err = json.Unmarshal(response, &r) if err != nil { - return fmt.Errorf("failed to unmarshal response: %w", err) + return errors.Wrap("failed to unmarshal response", err) } log.FeedbackInfo(cmd.Context(), r.Data.Result) } diff --git a/cli/serverdump.go b/cli/serverdump.go index ce4b8ccef9..44f319b052 100644 --- a/cli/serverdump.go +++ b/cli/serverdump.go @@ -18,6 +18,7 @@ import ( ds "github.com/ipfs/go-datastore" badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/db" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/spf13/cobra" ) @@ -40,23 +41,23 @@ var serverDumpCmd = &cobra.Command{ info, err := os.Stat(cfg.Datastore.Badger.Path) exists := (err == nil && info.IsDir()) if !exists { - return fmt.Errorf( + return errors.New(fmt.Sprintf( "badger store does not exist at %s. Try with an existing directory", cfg.Datastore.Badger.Path, - ) + )) } log.FeedbackInfo(cmd.Context(), "Opening badger store", logging.NewKV("Path", cfg.Datastore.Badger.Path)) rootstore, err = badgerds.NewDatastore(cfg.Datastore.Badger.Path, cfg.Datastore.Badger.Options) if err != nil { - return fmt.Errorf("could not open badger datastore: %w", err) + return errors.Wrap("could not open badger datastore", err) } } else { - return fmt.Errorf("server-side dump is only supported for the Badger datastore") + return errors.New("server-side dump is only supported for the Badger datastore") } db, err := db.NewDB(cmd.Context(), rootstore) if err != nil { - return fmt.Errorf("failed to initialize database: %w", err) + return errors.Wrap("failed to initialize database", err) } log.FeedbackInfo(cmd.Context(), "Dumping DB state...") diff --git a/cli/start.go b/cli/start.go index 8b3017d63a..1185983536 100644 --- a/cli/start.go +++ b/cli/start.go @@ -12,7 +12,6 @@ package cli import ( "context" - "errors" "fmt" gonet "net" "net/http" @@ -26,6 +25,7 @@ import ( "github.com/sourcenetwork/defradb/config" badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/db" + "github.com/sourcenetwork/defradb/errors" netapi "github.com/sourcenetwork/defradb/net/api" netpb "github.com/sourcenetwork/defradb/net/api/pb" netutils "github.com/sourcenetwork/defradb/net/utils" @@ -51,17 +51,17 @@ var startCmd = &cobra.Command{ PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { rootDir, exists, err := config.GetRootDir(rootDirParam) if err != nil { - return fmt.Errorf("failed to get root dir: %w", err) + return errors.Wrap("failed to get root dir", err) } if !exists { err = config.CreateRootDirWithDefaultConfig(rootDir) if err != nil { - return fmt.Errorf("failed to create root dir: %w", err) + return errors.Wrap("failed to create root dir", err) } } err = cfg.Load(rootDir) if err != nil { - return fmt.Errorf("failed to load config: %w", err) + return errors.Wrap("failed to load config", err) } // parse loglevel overrides @@ -153,7 +153,7 @@ func (di *defraInstance) close(ctx context.Context) { log.FeedbackInfo( ctx, "The node could not be closed successfully", - logging.NewKV("Error", err), + logging.NewKV("Error", err.Error()), ) } } @@ -162,7 +162,7 @@ func (di *defraInstance) close(ctx context.Context) { log.FeedbackInfo( ctx, "The server could not be closed successfully", - logging.NewKV("Error", err), + logging.NewKV("Error", err.Error()), ) } } @@ -190,7 +190,7 @@ func start(ctx context.Context) (*defraInstance, error) { } if err != nil { - return nil, fmt.Errorf("failed to open datastore: %w", err) + return nil, errors.Wrap("failed to open datastore", err) } var options []db.Option @@ -204,7 +204,7 @@ func start(ctx context.Context) (*defraInstance, error) { db, err := db.NewDB(ctx, rootstore, options...) if err != nil { - return nil, fmt.Errorf("failed to create database: %w", err) + return nil, errors.Wrap("failed to create database", err) } // init the p2p node @@ -219,7 +219,7 @@ func start(ctx context.Context) (*defraInstance, error) { ) if err != nil { db.Close(ctx) - return nil, fmt.Errorf("failed to start P2P node: %w", err) + return nil, errors.Wrap("failed to start P2P node", err) } // parse peers and bootstrap @@ -227,7 +227,7 @@ func start(ctx context.Context) (*defraInstance, error) { log.Debug(ctx, "Parsing bootstrap peers", logging.NewKV("Peers", cfg.Net.Peers)) addrs, err := netutils.ParsePeers(strings.Split(cfg.Net.Peers, ",")) if err != nil { - return nil, fmt.Errorf("failed to parse bootstrap peers %v: %w", cfg.Net.Peers, err) + return nil, errors.Wrap(fmt.Sprintf("failed to parse bootstrap peers %v", cfg.Net.Peers), err) } log.Debug(ctx, "Bootstrapping with peers", logging.NewKV("Addresses", addrs)) n.Boostrap(addrs) @@ -235,24 +235,24 @@ func start(ctx context.Context) (*defraInstance, error) { if err := n.Start(); err != nil { if e := n.Close(); e != nil { - err = fmt.Errorf("failed to close node: %v: %w", e.Error(), err) + err = errors.Wrap(fmt.Sprintf("failed to close node: %v", e.Error()), err) } db.Close(ctx) - return nil, fmt.Errorf("failed to start P2P listeners: %w", err) + return nil, errors.Wrap("failed to start P2P listeners", err) } MtcpAddr, err := ma.NewMultiaddr(cfg.Net.TCPAddress) if err != nil { - return nil, fmt.Errorf("failed to parse multiaddress: %w", err) + return nil, errors.Wrap("failed to parse multiaddress", err) } addr, err := netutils.TCPAddrFromMultiAddr(MtcpAddr) if err != nil { - return nil, fmt.Errorf("failed to parse TCP address: %w", err) + return nil, errors.Wrap("failed to parse TCP address", err) } rpcTimeoutDuration, err := cfg.Net.RPCTimeoutDuration() if err != nil { - return nil, fmt.Errorf("failed to parse RPC timeout duration: %w", err) + return nil, errors.Wrap("failed to parse RPC timeout duration", err) } server := grpc.NewServer(grpc.KeepaliveParams(keepalive.ServerParameters{ @@ -260,7 +260,7 @@ func start(ctx context.Context) (*defraInstance, error) { })) tcplistener, err := gonet.Listen("tcp", addr) if err != nil { - return nil, fmt.Errorf("failed to listen on TCP address %v: %w", addr, err) + return nil, errors.Wrap(fmt.Sprintf("failed to listen on TCP address %v", addr), err) } netService := netapi.NewService(n.Peer) @@ -282,7 +282,7 @@ func start(ctx context.Context) (*defraInstance, error) { } s := httpapi.NewServer(db, sOpt...) if err := s.Listen(ctx); err != nil { - return nil, fmt.Errorf("failed to listen on TCP address %v: %w", s.Addr, err) + return nil, errors.Wrap(fmt.Sprintf("failed to listen on TCP address %v", s.Addr), err) } // run the server in a separate goroutine diff --git a/client/dockey.go b/client/dockey.go index 87db4d2c7f..51652f5d48 100644 --- a/client/dockey.go +++ b/client/dockey.go @@ -13,9 +13,10 @@ package client import ( "bytes" "encoding/binary" - "errors" "strings" + "github.com/sourcenetwork/defradb/errors" + "github.com/ipfs/go-cid" mbase "github.com/multiformats/go-multibase" uuid "github.com/satori/go.uuid" diff --git a/client/document.go b/client/document.go index 82aea3c6a7..dc1edac52f 100644 --- a/client/document.go +++ b/client/document.go @@ -12,11 +12,12 @@ package client import ( "encoding/json" - "errors" "fmt" "strings" "sync" + "github.com/sourcenetwork/defradb/errors" + "github.com/fxamacker/cbor/v2" "github.com/ipfs/go-cid" mh "github.com/multiformats/go-multihash" @@ -340,7 +341,7 @@ func (doc *Document) setAndParseType(field string, value any) error { } default: - return fmt.Errorf("Unhandled type in raw JSON: %v => %T", field, val) + return errors.New(fmt.Sprintf("Unhandled type in raw JSON: %v => %T", field, val)) } return nil } @@ -527,7 +528,7 @@ func (doc *Document) toMapWithKey() (map[string]any, error) { // break // default: -// return fmt.Errorf("Unhandled type in raw JSON: %v => %T", k, v) +// return errors.Wrap("Unhandled type in raw JSON: %v => %T", k, v) // } // } diff --git a/client/errors.go b/client/errors.go index bda02147c3..b12c62a18f 100644 --- a/client/errors.go +++ b/client/errors.go @@ -10,7 +10,7 @@ package client -import "errors" +import "github.com/sourcenetwork/defradb/errors" // Errors returnable from this package. // diff --git a/config/config.go b/config/config.go index f4de2acfc2..39604e6cf9 100644 --- a/config/config.go +++ b/config/config.go @@ -59,6 +59,7 @@ import ( "github.com/mitchellh/mapstructure" ma "github.com/multiformats/go-multiaddr" badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/sourcenetwork/defradb/node" "github.com/spf13/viper" @@ -158,16 +159,16 @@ func DefaultConfig() *Config { func (cfg *Config) validate() error { if err := cfg.Datastore.validate(); err != nil { - return fmt.Errorf("failed to validate Datastore config: %w", err) + return errors.Wrap("failed to validate Datastore config", err) } if err := cfg.API.validate(); err != nil { - return fmt.Errorf("failed to validate API config: %w", err) + return errors.Wrap("failed to validate API config", err) } if err := cfg.Net.validate(); err != nil { - return fmt.Errorf("failed to validate Net config: %w", err) + return errors.Wrap("failed to validate Net config", err) } if err := cfg.Log.validate(); err != nil { - return fmt.Errorf("failed to validate Log config: %w", err) + return errors.Wrap("failed to validate Log config", err) } return nil } @@ -292,7 +293,7 @@ func (dbcfg DatastoreConfig) validate() error { switch dbcfg.Store { case "badger", "memory": default: - return fmt.Errorf("invalid store type: %s", dbcfg.Store) + return errors.New(fmt.Sprintf("invalid store type: %s", dbcfg.Store)) } return nil } @@ -310,11 +311,11 @@ func defaultAPIConfig() *APIConfig { func (apicfg *APIConfig) validate() error { if apicfg.Address == "" { - return fmt.Errorf("no database URL provided") + return errors.New("no database URL provided") } _, err := net.ResolveTCPAddr("tcp", apicfg.Address) if err != nil { - return fmt.Errorf("invalid database URL: %w", err) + return errors.Wrap("invalid database URL", err) } return nil } @@ -354,19 +355,19 @@ func defaultNetConfig() *NetConfig { func (netcfg *NetConfig) validate() error { _, err := time.ParseDuration(netcfg.RPCTimeout) if err != nil { - return fmt.Errorf("invalid RPC timeout: %s", netcfg.RPCTimeout) + return errors.New(fmt.Sprintf("invalid RPC timeout: %s", netcfg.RPCTimeout)) } _, err = time.ParseDuration(netcfg.RPCMaxConnectionIdle) if err != nil { - return fmt.Errorf("invalid RPC MaxConnectionIdle: %s", netcfg.RPCMaxConnectionIdle) + return errors.New(fmt.Sprintf("invalid RPC MaxConnectionIdle: %s", netcfg.RPCMaxConnectionIdle)) } _, err = ma.NewMultiaddr(netcfg.P2PAddress) if err != nil { - return fmt.Errorf("invalid P2P address: %s", netcfg.P2PAddress) + return errors.New(fmt.Sprintf("invalid P2P address: %s", netcfg.P2PAddress)) } _, err = net.ResolveTCPAddr("tcp", netcfg.RPCAddress) if err != nil { - return fmt.Errorf("invalid RPC address: %w", err) + return errors.Wrap("invalid RPC address", err) } if len(netcfg.Peers) > 0 { peers := strings.Split(netcfg.Peers, ",") @@ -374,7 +375,7 @@ func (netcfg *NetConfig) validate() error { for i, addr := range peers { maddrs[i], err = ma.NewMultiaddr(addr) if err != nil { - return fmt.Errorf("failed to parse bootstrap peers: %s", netcfg.Peers) + return errors.New(fmt.Sprintf("failed to parse bootstrap peers: %s", netcfg.Peers)) } } } @@ -466,7 +467,7 @@ func (logcfg LoggingConfig) ToLoggerConfig() (logging.Config, error) { case logLevelFatal: loglvl = logging.Fatal default: - return logging.Config{}, fmt.Errorf("invalid log level: %s", logcfg.Level) + return logging.Config{}, errors.New(fmt.Sprintf("invalid log level: %s", logcfg.Level)) } var encfmt logging.EncoderFormat switch logcfg.Format { @@ -475,14 +476,14 @@ func (logcfg LoggingConfig) ToLoggerConfig() (logging.Config, error) { case "csv": encfmt = logging.CSV default: - return logging.Config{}, fmt.Errorf("invalid log format: %s", logcfg.Format) + return logging.Config{}, errors.New(fmt.Sprintf("invalid log format: %s", logcfg.Format)) } // handle named overrides overrides := make(map[string]logging.Config) for name, cfg := range logcfg.NamedOverrides { c, err := cfg.ToLoggerConfig() if err != nil { - return logging.Config{}, fmt.Errorf("couldn't convert override config: %w", err) + return logging.Config{}, errors.Wrap("couldn't convert override config", err) } overrides[name] = c } @@ -506,7 +507,7 @@ func (logcfg LoggingConfig) copy() LoggingConfig { func (logcfg *LoggingConfig) GetOrCreateNamedLogger(name string) (*NamedLoggingConfig, error) { if name == "" { - return nil, fmt.Errorf("provided name can't be empty for named config") + return nil, errors.New("provided name can't be empty for named config") } if namedCfg, exists := logcfg.NamedOverrides[name]; exists { return namedCfg, nil @@ -530,7 +531,7 @@ func (cfg *Config) GetLoggingConfig() (logging.Config, error) { func (c *Config) ToJSON() ([]byte, error) { jsonbytes, err := json.Marshal(c) if err != nil { - return []byte{}, fmt.Errorf("failed to marshal Config to JSON: %w", err) + return []byte{}, errors.Wrap("failed to marshal Config to JSON", err) } return jsonbytes, nil } @@ -540,10 +541,10 @@ func (c *Config) toBytes() ([]byte, error) { tmpl := template.New("configTemplate") configTemplate, err := tmpl.Parse(defaultConfigTemplate) if err != nil { - return nil, fmt.Errorf("could not parse config template: %w", err) + return nil, errors.Wrap("could not parse config template", err) } if err := configTemplate.Execute(&buffer, c); err != nil { - return nil, fmt.Errorf("could not execute config template: %w", err) + return nil, errors.Wrap("could not execute config template", err) } return buffer.Bytes(), nil } diff --git a/config/configfile.go b/config/configfile.go index 9d1a2b9cf5..52c11d9988 100644 --- a/config/configfile.go +++ b/config/configfile.go @@ -13,6 +13,8 @@ package config import ( "fmt" "os" + + "github.com/sourcenetwork/defradb/errors" ) const ( @@ -28,7 +30,7 @@ func (cfg *Config) writeConfigFile(path string) error { return err } if err := os.WriteFile(path, buffer, defaultConfigFilePerm); err != nil { - return fmt.Errorf("failed to write file: %w", err) + return errors.Wrap("failed to write file", err) } return nil } diff --git a/connor/and.go b/connor/and.go index 08590bf488..692cfde25a 100644 --- a/connor/and.go +++ b/connor/and.go @@ -1,6 +1,10 @@ package connor -import "fmt" +import ( + "fmt" + + "github.com/sourcenetwork/defradb/errors" +) // and is an operator which allows the evaluation of // of a number of conditions, matching if all of them match. @@ -17,6 +21,6 @@ func and(condition, data any) (bool, error) { return true, nil default: - return false, fmt.Errorf("unknown or condition type '%#v'", cn) + return false, errors.New(fmt.Sprintf("unknown or condition type '%#v'", cn)) } } diff --git a/connor/connor.go b/connor/connor.go index 6cdd216112..b1d9f9fa86 100644 --- a/connor/connor.go +++ b/connor/connor.go @@ -2,6 +2,8 @@ package connor import ( "fmt" + + "github.com/sourcenetwork/defradb/errors" ) // Match is the default method used in Connor to match some data to a @@ -36,6 +38,6 @@ func matchWith(op string, conditions, data any) (bool, error) { case "_or": return or(conditions, data) default: - return false, fmt.Errorf("unknown operator '%s'", op) + return false, errors.New(fmt.Sprintf("unknown operator '%s'", op)) } } diff --git a/connor/ge.go b/connor/ge.go index 4e75bcd8b0..d174e34e3a 100644 --- a/connor/ge.go +++ b/connor/ge.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/connor/numbers" + "github.com/sourcenetwork/defradb/errors" ) // ge does value comparisons to determine whether one @@ -34,6 +35,6 @@ func ge(condition, data any) (bool, error) { return false, nil default: - return false, fmt.Errorf("unknown comparison type '%#v'", condition) + return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition)) } } diff --git a/connor/gt.go b/connor/gt.go index 96b7fc358b..b538f3ff40 100644 --- a/connor/gt.go +++ b/connor/gt.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/connor/numbers" + "github.com/sourcenetwork/defradb/errors" ) // gt does value comparisons to determine whether one @@ -33,6 +34,6 @@ func gt(condition, data any) (bool, error) { return false, nil default: - return false, fmt.Errorf("unknown comparison type '%#v'", condition) + return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition)) } } diff --git a/connor/in.go b/connor/in.go index 2dd8f11e62..1a88d4e7ba 100644 --- a/connor/in.go +++ b/connor/in.go @@ -1,6 +1,6 @@ package connor -import "fmt" +import "github.com/sourcenetwork/defradb/errors" // in will determine whether a value exists within the // condition's array of available values. @@ -17,6 +17,6 @@ func in(conditions, data any) (bool, error) { return false, nil default: - return false, fmt.Errorf("unknown value type") + return false, errors.New("unknown value type") } } diff --git a/connor/le.go b/connor/le.go index 1bb9d1e5c9..9ed706db96 100644 --- a/connor/le.go +++ b/connor/le.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/connor/numbers" + "github.com/sourcenetwork/defradb/errors" ) // le does value comparisons to determine whether one @@ -34,6 +35,6 @@ func le(condition, data any) (bool, error) { return false, nil default: - return false, fmt.Errorf("unknown comparison type '%#v'", condition) + return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition)) } } diff --git a/connor/lt.go b/connor/lt.go index 64182a2a74..2744240282 100644 --- a/connor/lt.go +++ b/connor/lt.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/connor/numbers" + "github.com/sourcenetwork/defradb/errors" ) // lt does value comparisons to determine whether one @@ -34,6 +35,6 @@ func lt(condition, data any) (bool, error) { return false, nil default: - return false, fmt.Errorf("unknown comparison type '%#v'", condition) + return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition)) } } diff --git a/connor/or.go b/connor/or.go index 8ca35615a0..3ce8c47d21 100644 --- a/connor/or.go +++ b/connor/or.go @@ -1,6 +1,10 @@ package connor -import "fmt" +import ( + "fmt" + + "github.com/sourcenetwork/defradb/errors" +) // or is an operator which allows the evaluation of // of a number of conditions, matching if any of them match. @@ -17,6 +21,6 @@ func or(condition, data any) (bool, error) { return false, nil default: - return false, fmt.Errorf("unknown or condition type '%#v'", cn) + return false, errors.New(fmt.Sprintf("unknown or condition type '%#v'", cn)) } } diff --git a/core/crdt/base.go b/core/crdt/base.go index 5e1579b30f..59e64782e6 100644 --- a/core/crdt/base.go +++ b/core/crdt/base.go @@ -13,11 +13,11 @@ package crdt import ( "context" "encoding/binary" - "errors" ds "github.com/ipfs/go-datastore" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" ) // baseCRDT is embedded as a base layer into all diff --git a/core/crdt/composite.go b/core/crdt/composite.go index 4a7b832982..d6513e97b9 100644 --- a/core/crdt/composite.go +++ b/core/crdt/composite.go @@ -13,12 +13,12 @@ package crdt import ( "bytes" "context" - "errors" "sort" "strings" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" ipld "github.com/ipfs/go-ipld-format" dag "github.com/ipfs/go-merkledag" diff --git a/core/crdt/lwwreg.go b/core/crdt/lwwreg.go index ec569f49f2..e8d0b3553d 100644 --- a/core/crdt/lwwreg.go +++ b/core/crdt/lwwreg.go @@ -15,9 +15,8 @@ import ( "bytes" "context" - "fmt" - "errors" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" @@ -142,7 +141,7 @@ func (reg LWWRegister) Merge(ctx context.Context, delta core.Delta, id string) e func (reg LWWRegister) setValue(ctx context.Context, val []byte, priority uint64) error { curPrio, err := reg.getPriority(ctx, reg.key) if err != nil { - return fmt.Errorf("Failed to get priority for Set : %w", err) + return errors.Wrap("Failed to get priority for Set ", err) } // if the current priority is higher ignore put @@ -162,7 +161,7 @@ func (reg LWWRegister) setValue(ctx context.Context, val []byte, priority uint64 buf := append([]byte{byte(client.LWW_REGISTER)}, val...) err = reg.store.Put(ctx, valueK.ToDS(), buf) if err != nil { - return fmt.Errorf("Failed to store new value : %w", err) + return errors.Wrap("Failed to store new value ", err) } return reg.setPriority(ctx, reg.key, priority) diff --git a/core/key.go b/core/key.go index c2a4262a7b..c58d0589a7 100644 --- a/core/key.go +++ b/core/key.go @@ -18,6 +18,7 @@ import ( "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" ) var ( @@ -144,7 +145,7 @@ func DataStoreKeyFromDocKey(dockey client.DocKey) DataStoreKey { func NewHeadStoreKey(key string) (HeadStoreKey, error) { elements := strings.Split(key, "/") if len(elements) != 4 { - return HeadStoreKey{}, fmt.Errorf("Given headstore key string is not in expected format: %s", key) + return HeadStoreKey{}, errors.New(fmt.Sprintf("Given headstore key string is not in expected format: %s", key)) } cid, err := cid.Decode(elements[3]) @@ -430,7 +431,7 @@ func (k DataStoreKey) PrefixEnd() DataStoreKey { func (k DataStoreKey) FieldID() (uint32, error) { fieldID, err := strconv.Atoi(k.FieldId) if err != nil { - return 0, fmt.Errorf("Failed to get FieldID of Key: %w", err) + return 0, errors.Wrap("Failed to get FieldID of Key", err) } return uint32(fieldID), nil } diff --git a/core/replicated.go b/core/replicated.go index 89b7c5475d..3edc8bf578 100644 --- a/core/replicated.go +++ b/core/replicated.go @@ -12,7 +12,8 @@ package core import ( "context" - "errors" + + "github.com/sourcenetwork/defradb/errors" cid "github.com/ipfs/go-cid" ipld "github.com/ipfs/go-ipld-format" diff --git a/datastore/badger/v3/datastore.go b/datastore/badger/v3/datastore.go index e9571ed5a5..e88f0c90b2 100644 --- a/datastore/badger/v3/datastore.go +++ b/datastore/badger/v3/datastore.go @@ -6,8 +6,6 @@ package badger import ( "context" - "errors" - "fmt" "runtime" "strings" "sync" @@ -19,6 +17,7 @@ import ( logger "github.com/ipfs/go-log/v2" goprocess "github.com/jbenet/goprocess" "github.com/sourcenetwork/defradb/datastore/iterable" + "github.com/sourcenetwork/defradb/errors" "go.uber.org/zap" ) @@ -155,7 +154,7 @@ func NewDatastore(path string, options *Options) (*Datastore, error) { kv, err := badger.Open(opt) if err != nil { if strings.HasPrefix(err.Error(), "manifest has unsupported version:") { - err = fmt.Errorf("unsupported badger version, use github.com/ipfs/badgerds-upgrade to upgrade: %w", err) + err = errors.Wrap("unsupported badger version, use github.com/ipfs/badgerds-upgrade to upgrade", err) } return nil, err } diff --git a/datastore/badger/v3/iterator.go b/datastore/badger/v3/iterator.go index 47ba970dfd..4c8ec05871 100644 --- a/datastore/badger/v3/iterator.go +++ b/datastore/badger/v3/iterator.go @@ -23,6 +23,7 @@ import ( dsq "github.com/ipfs/go-datastore/query" goprocess "github.com/jbenet/goprocess" "github.com/sourcenetwork/defradb/datastore/iterable" + "github.com/sourcenetwork/defradb/errors" ) type BadgerIterator struct { @@ -54,7 +55,7 @@ func (t *txn) GetIterator(q dsq.Query) (iterable.Iterator, error) { opt.Reverse = true reversedOrder = true default: - return nil, fmt.Errorf("Order format not supported: %v", orderType) + return nil, errors.New(fmt.Sprintf("Order format not supported: %v", orderType)) } } diff --git a/datastore/blockstore.go b/datastore/blockstore.go index ae5b66599a..6fabfe4a10 100644 --- a/datastore/blockstore.go +++ b/datastore/blockstore.go @@ -12,7 +12,8 @@ package datastore import ( "context" - "errors" + + "github.com/sourcenetwork/defradb/errors" blocks "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" diff --git a/db/base/collection_keys.go b/db/base/collection_keys.go index ec0e7e44cd..67467901cc 100644 --- a/db/base/collection_keys.go +++ b/db/base/collection_keys.go @@ -11,11 +11,11 @@ package base import ( - "errors" "fmt" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" ) // MakeIndexPrefix generates a key prefix for the given collection/index descriptions diff --git a/db/collection.go b/db/collection.go index 848c116b1c..f0a4cc26f8 100644 --- a/db/collection.go +++ b/db/collection.go @@ -21,11 +21,10 @@ import ( "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/sourcenetwork/defradb/merkle/crdt" - "errors" - "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore/query" @@ -232,7 +231,7 @@ func (db *db) GetCollectionBySchemaID( schemaID string, ) (client.Collection, error) { if schemaID == "" { - return nil, fmt.Errorf("Schema ID can't be empty") + return nil, errors.New("Schema ID can't be empty") } key := core.NewCollectionSchemaKey(schemaID) @@ -255,7 +254,7 @@ func (db *db) GetAllCollections(ctx context.Context) ([]client.Collection, error KeysOnly: true, }) if err != nil { - return nil, fmt.Errorf("Failed to create collection prefix query: %w", err) + return nil, errors.Wrap("Failed to create collection prefix query", err) } defer func() { if err := q.Close(); err != nil { @@ -272,7 +271,7 @@ func (db *db) GetAllCollections(ctx context.Context) ([]client.Collection, error colName := ds.NewKey(res.Key).BaseNamespace() col, err := db.GetCollectionByName(ctx, colName) if err != nil { - return nil, fmt.Errorf("Failed to get collection (%s): %w", colName, err) + return nil, errors.Wrap(fmt.Sprintf("Failed to get collection (%s)", colName), err) } cols = append(cols, col) } @@ -464,7 +463,7 @@ func (c *collection) create(ctx context.Context, txn datastore.Txn, doc *client. dockey := client.NewDocKeyV0(doccid) key := c.getPrimaryKeyFromDocKey(dockey) if key.DocKey != doc.Key().String() { - return fmt.Errorf("Expected %s, got %s : %w", doc.Key(), key.DocKey, ErrDocVerification) + return errors.Wrap(fmt.Sprintf("Expected %s, got %s ", doc.Key(), key.DocKey), ErrDocVerification) } // check if doc already exists diff --git a/db/collection_delete.go b/db/collection_delete.go index 02d63b78ca..c92c198bc9 100644 --- a/db/collection_delete.go +++ b/db/collection_delete.go @@ -12,7 +12,6 @@ package db import ( "context" - "errors" "fmt" block "github.com/ipfs/go-block-format" @@ -25,6 +24,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/merkle/clock" "github.com/sourcenetwork/defradb/query/graphql/parser" ) @@ -293,7 +293,7 @@ func (c *collection) applyFullDelete( // Get all the heads (cids). heads, _, err := headset.List(ctx) if err != nil { - return fmt.Errorf("Failed to get document heads: %w", err) + return errors.Wrap("Failed to get document heads", err) } dagDel := newDagDeleter(txn.DAGstore()) diff --git a/db/collection_update.go b/db/collection_update.go index de0a1750f5..c6478b7be8 100644 --- a/db/collection_update.go +++ b/db/collection_update.go @@ -12,14 +12,13 @@ package db import ( "context" - "errors" - "fmt" "strings" "time" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" "github.com/sourcenetwork/defradb/query/graphql/parser" "github.com/sourcenetwork/defradb/query/graphql/planner" @@ -298,7 +297,7 @@ func (c *collection) applyPatch( //nolint:unused pathVal := opObject.Get("path") if pathVal == nil { - return fmt.Errorf("missing document field to update") + return errors.New("missing document field to update") } path, err := pathVal.StringBytes() diff --git a/db/db.go b/db/db.go index 653fdcea45..a36a4ac815 100644 --- a/db/db.go +++ b/db/db.go @@ -16,14 +16,13 @@ package db import ( "context" - "errors" - "fmt" "sync" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corenet "github.com/sourcenetwork/defradb/core/net" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/merkle/crdt" "github.com/sourcenetwork/defradb/query/graphql/planner" "github.com/sourcenetwork/defradb/query/graphql/schema" @@ -191,11 +190,11 @@ func (db *db) GetRelationshipIdField(fieldName, targetType, thisType string) (st rm := db.schema.Relations rel := rm.GetRelationByDescription(fieldName, targetType, thisType) if rel == nil { - return "", fmt.Errorf("Relation does not exists") + return "", errors.New("Relation does not exists") } subtypefieldname, _, ok := rel.GetFieldFromSchemaType(targetType) if !ok { - return "", fmt.Errorf("Relation is missing referenced field") + return "", errors.New("Relation is missing referenced field") } return subtypefieldname, nil } diff --git a/db/fetcher/dag.go b/db/fetcher/dag.go index fa1831d312..b7211dfe21 100644 --- a/db/fetcher/dag.go +++ b/db/fetcher/dag.go @@ -12,12 +12,12 @@ package fetcher import ( "context" - "errors" "sort" "strings" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" "github.com/ipfs/go-cid" dsq "github.com/ipfs/go-datastore/query" @@ -166,12 +166,12 @@ func (hh *heads) List() ([]cid.Cid, uint64, error) { var maxHeight uint64 for r := range results.Next() { if r.Error != nil { - return nil, 0, fmt.Errorf("Failed to get next query result : %w", err) + return nil, 0, errors.Wrap("Failed to get next query result ", err) } headKey := ds.NewKey(strings.TrimPrefix(r.Key, hh.namespace.String())) headCid, err := dshelp.DsKeyToCid(headKey) if err != nil { - return nil, 0, fmt.Errorf("Failed to get CID from key : %w", err) + return nil, 0, errors.Wrap("Failed to get CID from key ", err) } height, n := binary.Uvarint(r.Value) if n <= 0 { diff --git a/db/fetcher/encoded_doc.go b/db/fetcher/encoded_doc.go index ce9ab37be0..674f1a417c 100644 --- a/db/fetcher/encoded_doc.go +++ b/db/fetcher/encoded_doc.go @@ -16,6 +16,7 @@ import ( "github.com/fxamacker/cbor/v2" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" ) type EPTuple []encProperty @@ -47,11 +48,11 @@ func (e encProperty) Decode() (client.CType, any, error) { for i, untypedValue := range array { boolArray[i], ok = untypedValue.(bool) if !ok { - return ctype, nil, fmt.Errorf( + return ctype, nil, errors.New(fmt.Sprintf( "Could not convert type: %T, value: %v to bool.", untypedValue, untypedValue, - ) + )) } } val = boolArray @@ -83,11 +84,11 @@ func (e encProperty) Decode() (client.CType, any, error) { for i, untypedValue := range array { floatArray[i], ok = untypedValue.(float64) if !ok { - return ctype, nil, fmt.Errorf( + return ctype, nil, errors.New(fmt.Sprintf( "Could not convert type: %T, value: %v to float64.", untypedValue, untypedValue, - ) + )) } } val = floatArray @@ -103,11 +104,11 @@ func (e encProperty) Decode() (client.CType, any, error) { for i, untypedValue := range array { stringArray[i], ok = untypedValue.(string) if !ok { - return ctype, nil, fmt.Errorf( + return ctype, nil, errors.New(fmt.Sprintf( "Could not convert type: %T, value: %v to string.", untypedValue, untypedValue, - ) + )) } } val = stringArray @@ -146,12 +147,12 @@ func convertNillableArray[T any](items []any) ([]client.Option[T], error) { } value, ok := untypedValue.(T) if !ok { - return nil, fmt.Errorf( + return nil, errors.New(fmt.Sprintf( "Could not convert type: %T, value: %v to %T.", untypedValue, untypedValue, *new(T), - ) + )) } resultArray[i] = client.Some(value) } @@ -186,11 +187,11 @@ func convertToInt(untypedValue any) (int64, error) { case float64: return int64(value), nil default: - return 0, fmt.Errorf( + return 0, errors.New(fmt.Sprintf( "Could not convert type: %T, value: %v to int64.", untypedValue, untypedValue, - ) + )) } } diff --git a/db/fetcher/fetcher.go b/db/fetcher/fetcher.go index 686923641b..9eb15dc352 100644 --- a/db/fetcher/fetcher.go +++ b/db/fetcher/fetcher.go @@ -13,15 +13,14 @@ package fetcher import ( "bytes" "context" - "errors" dsq "github.com/ipfs/go-datastore/query" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/datastore/iterable" - - "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" + "github.com/sourcenetwork/defradb/errors" ) // Fetcher is the interface for collecting documents diff --git a/db/fetcher/versioned.go b/db/fetcher/versioned.go index c68f9027e5..e3a6320465 100644 --- a/db/fetcher/versioned.go +++ b/db/fetcher/versioned.go @@ -19,13 +19,13 @@ import ( "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/merkle/crdt" "github.com/ipfs/go-cid" ds "github.com/ipfs/go-datastore" format "github.com/ipfs/go-ipld-format" dag "github.com/ipfs/go-merkledag" - "github.com/pkg/errors" ) var ( @@ -141,8 +141,8 @@ func (vf *VersionedFetcher) Start(ctx context.Context, txn datastore.Txn, spans c, err := cid.Decode(cidRaw.DocKey) if err != nil { return errors.Wrap( - err, fmt.Sprintf("Failed to decode CID for VersionedFetcher: %s", cidRaw.DocKey), + err, ) } @@ -164,7 +164,7 @@ func (vf *VersionedFetcher) Start(ctx context.Context, txn datastore.Txn, spans } if err := vf.seekTo(vf.version); err != nil { - return fmt.Errorf("Failed seeking state to %v: %w", c, err) + return errors.Wrap(fmt.Sprintf("failed seeking state to %v", c), err) } return vf.DocumentFetcher.Start(ctx, vf.store, core.Spans{}) @@ -244,7 +244,7 @@ func (vf *VersionedFetcher) seekTo(c cid.Cid) error { } err := vf.merge(cc) if err != nil { - return fmt.Errorf("Failed merging state: %w", err) + return errors.Wrap("Failed merging state", err) } } @@ -280,7 +280,7 @@ func (vf *VersionedFetcher) seekNext(c cid.Cid, topParent bool) error { hasLocalBlock, err := vf.store.DAGstore().Has(vf.ctx, c) if err != nil { - return fmt.Errorf("(version fetcher) failed to find block in blockstore: %w", err) + return errors.Wrap("(version fetcher) failed to find block in blockstore", err) } // skip if we already have it locally if hasLocalBlock { @@ -289,12 +289,12 @@ func (vf *VersionedFetcher) seekNext(c cid.Cid, topParent bool) error { blk, err := vf.txn.DAGstore().Get(vf.ctx, c) if err != nil { - return fmt.Errorf("(version fetcher) failed to get block in blockstore: %w", err) + return errors.Wrap("(version fetcher) failed to get block in blockstore", err) } // store the block in the local (transient store) if err := vf.store.DAGstore().Put(vf.ctx, blk); err != nil { - return fmt.Errorf("(version fetcher) failed to write block to blockstore : %w", err) + return errors.Wrap("(version fetcher) failed to write block to blockstore ", err) } // add the CID to the queuedCIDs list @@ -305,14 +305,14 @@ func (vf *VersionedFetcher) seekNext(c cid.Cid, topParent bool) error { // decode the block nd, err := dag.DecodeProtobuf(blk.RawData()) if err != nil { - return fmt.Errorf("(version fetcher) failed to decode protobuf: %w", err) + return errors.Wrap("(version fetcher) failed to decode protobuf", err) } // subDAGLinks := make([]cid.Cid, 0) // @todo: set slice size l, err := nd.GetNodeLink(core.HEAD) // ErrLinkNotFound is fine, it just means we have no more head links if err != nil && !errors.Is(err, dag.ErrLinkNotFound) { - return fmt.Errorf("(version fetcher) failed to get node link from DAG: %w", err) + return errors.Wrap("(version fetcher) failed to get node link from DAG", err) } // only seekNext on parent if we have a HEAD link @@ -375,7 +375,7 @@ func (vf *VersionedFetcher) merge(c cid.Cid) error { fieldID := vf.col.Schema.GetFieldKey(l.Name) if fieldID == uint32(0) { - return fmt.Errorf("Invalid sub graph field name: %s", l.Name) + return errors.New(fmt.Sprintf("Invalid sub graph field name: %s", l.Name)) } // @todo: Right now we ONLY handle LWW_REGISTER, need to swith on this and // get CType from descriptions @@ -422,7 +422,7 @@ func (vf *VersionedFetcher) getDAGNode(c cid.Cid) (*dag.ProtoNode, error) { // get Block blk, err := vf.store.DAGstore().Get(vf.ctx, c) if err != nil { - return nil, fmt.Errorf("Failed to get DAG Node: %w", err) + return nil, errors.Wrap("Failed to get DAG Node", err) } // get node diff --git a/db/sequence.go b/db/sequence.go index b056822bec..672a0b2a16 100644 --- a/db/sequence.go +++ b/db/sequence.go @@ -13,10 +13,10 @@ package db import ( "context" "encoding/binary" - "errors" ds "github.com/ipfs/go-datastore" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" ) type sequence struct { diff --git a/docs/data_format_changes/i607-inline-array-not-nil.md b/docs/data_format_changes/i607-inline-array-not-nil copy.md similarity index 100% rename from docs/data_format_changes/i607-inline-array-not-nil.md rename to docs/data_format_changes/i607-inline-array-not-nil copy.md diff --git a/errors/defraError.go b/errors/defraError.go index ae8c8e8cd1..7ebe4355de 100644 --- a/errors/defraError.go +++ b/errors/defraError.go @@ -34,6 +34,12 @@ type defraError struct { func (e *defraError) Error() string { builder := strings.Builder{} builder.WriteString(e.message) + + if e.inner != nil { + builder.WriteString(": ") + builder.WriteString(e.inner.Error()) + } + if len(e.kvs) > 0 { builder.WriteString(".") } diff --git a/errors/errors.go b/errors/errors.go index fe2e05609e..128ca59212 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -12,6 +12,7 @@ package errors import ( "bytes" + "errors" "runtime" goErrors "github.com/go-errors/errors" @@ -49,6 +50,10 @@ func Wrap(message string, inner error, keyvals ...KV) error { return err } +func Is(err, target error) bool { + return errors.Is(err, target) +} + func newError(message string, keyvals ...KV) *defraError { return withStackTrace(message, keyvals...) } diff --git a/logging/logger.go b/logging/logger.go index 9ca512f29f..0bfb4a06f3 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -12,6 +12,7 @@ package logging import ( "context" + "fmt" stdlog "log" "os" "sync" @@ -64,7 +65,8 @@ func (l *logger) Error(ctx context.Context, message string, keyvals ...KV) { func (l *logger) ErrorE(ctx context.Context, message string, err error, keyvals ...KV) { kvs := keyvals - kvs = append(kvs, NewKV("Error", err)) + kvs = append(kvs, NewKV("Error", err.Error())) + kvs = withStackTrace(err, kvs) l.syncLock.RLock() defer l.syncLock.RUnlock() @@ -81,7 +83,8 @@ func (l *logger) Fatal(ctx context.Context, message string, keyvals ...KV) { func (l *logger) FatalE(ctx context.Context, message string, err error, keyvals ...KV) { kvs := keyvals - kvs = append(kvs, NewKV("Error", err)) + kvs = append(kvs, NewKV("Error", err.Error())) + kvs = withStackTrace(err, kvs) l.syncLock.RLock() defer l.syncLock.RUnlock() @@ -113,6 +116,9 @@ func (l *logger) FeedbackErrorE(ctx context.Context, message string, err error, defer l.syncLock.RUnlock() if l.consoleLogger != nil { l.consoleLogger.Println(message) + if stack, hasStack := getStackTrace(err); hasStack { + l.consoleLogger.Println(stack) + } } } @@ -131,6 +137,9 @@ func (l *logger) FeedbackFatalE(ctx context.Context, message string, err error, defer l.syncLock.RUnlock() if l.consoleLogger != nil { l.consoleLogger.Println(message) + if stack, hasStack := getStackTrace(err); hasStack { + l.consoleLogger.Println(stack) + } } } @@ -171,6 +180,25 @@ func (l *logger) ApplyConfig(config Config) { } } +func withStackTrace(err error, keyvals []KV) []KV { + if stack, hasStack := getStackTrace(err); hasStack { + return append(keyvals, NewKV("stacktrace", stack)) + } + + return keyvals +} + +func getStackTrace(err error) (string, bool) { + configMutex.RLock() + defer configMutex.RUnlock() + + if cachedConfig.EnableStackTrace.EnableStackTrace { + return fmt.Sprintf("%+v", err), true + } + + return "", false +} + func buildZapLogger(name string, config Config) (*zap.Logger, error) { const ( encodingTypeConsole string = "console" @@ -188,10 +216,6 @@ func buildZapLogger(name string, config Config) (*zap.Logger, error) { defaultConfig.Level = zap.NewAtomicLevelAt(zapcore.Level(config.Level.LogLevel)) } - if config.EnableStackTrace.HasValue { - defaultConfig.DisableStacktrace = !config.EnableStackTrace.EnableStackTrace - } - if config.DisableColor.HasValue && config.DisableColor.DisableColor { defaultConfig.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder } diff --git a/logging/logging_test.go b/logging/logging_test.go index 3e81f91c0d..a395e9ec3d 100644 --- a/logging/logging_test.go +++ b/logging/logging_test.go @@ -15,13 +15,13 @@ import ( "bytes" "context" "encoding/json" - "errors" - "fmt" "io" "os" "os/exec" "testing" + "github.com/sourcenetwork/defradb/errors" + "github.com/stretchr/testify/assert" ) @@ -102,7 +102,8 @@ func TestLogWritesFatalMessageWithStackTraceToLogAndKillsProcessGivenStackTraceE assert.Equal(t, logMessage, logLines[0]["msg"]) assert.Equal(t, "FATAL", logLines[0]["level"]) assert.Equal(t, "TestLogName", logLines[0]["logger"]) - assert.Contains(t, logLines[0], "stacktrace") + // no stacktrace will be present since no error was sent to the logger. + assert.NotContains(t, logLines[0], "stacktrace") // caller is disabled by default assert.NotContains(t, logLines[0], "logging_test.go") } @@ -117,7 +118,7 @@ func TestLogWritesFatalEMessageToLogAndKillsProcess(t *testing.T) { c.OutputPaths = []string{logPath} }) - logger.FatalE(ctx, logMessage, fmt.Errorf("dummy error")) + logger.FatalE(ctx, logMessage, errors.New("dummy error")) return } @@ -159,7 +160,7 @@ func TestLogWritesFatalEMessageWithStackTraceToLogAndKillsProcessGivenStackTrace c.EnableStackTrace = NewEnableStackTraceOption(true) }) - logger.FatalE(ctx, logMessage, fmt.Errorf("dummy error")) + logger.FatalE(ctx, logMessage, errors.New("dummy error")) return } @@ -201,7 +202,7 @@ type LogLevelTestCase struct { func logDebug(l Logger, c context.Context, m string) { l.Debug(c, m) } func logInfo(l Logger, c context.Context, m string) { l.Info(c, m) } func logError(l Logger, c context.Context, m string) { l.Error(c, m) } -func logErrorE(l Logger, c context.Context, m string) { l.ErrorE(c, m, fmt.Errorf("test error")) } +func logErrorE(l Logger, c context.Context, m string) { l.ErrorE(c, m, errors.New("test error")) } func getLogLevelTestCase() []LogLevelTestCase { return []LogLevelTestCase{ @@ -209,27 +210,27 @@ func getLogLevelTestCase() []LogLevelTestCase { {Debug, logDebug, "DEBUG", false, false, false}, {Debug, logInfo, "INFO", false, false, false}, {Debug, logError, "ERROR", false, false, false}, - {Debug, logError, "ERROR", true, true, false}, + {Debug, logError, "ERROR", true, false, false}, {Debug, logErrorE, "ERROR", false, false, false}, {Debug, logErrorE, "ERROR", true, true, false}, {Info, logDebug, "", false, false, false}, {Info, logInfo, "INFO", false, false, true}, {Info, logInfo, "INFO", false, false, false}, {Info, logError, "ERROR", false, false, false}, - {Info, logError, "ERROR", true, true, false}, + {Info, logError, "ERROR", true, false, false}, {Info, logErrorE, "ERROR", false, false, false}, {Info, logErrorE, "ERROR", true, true, false}, {Warn, logDebug, "", false, false, false}, {Warn, logInfo, "", false, false, false}, {Warn, logError, "ERROR", false, false, false}, - {Warn, logError, "ERROR", true, true, false}, + {Warn, logError, "ERROR", true, false, false}, {Warn, logErrorE, "ERROR", false, false, false}, {Warn, logErrorE, "ERROR", true, true, false}, {Error, logDebug, "", false, false, false}, {Error, logInfo, "", false, false, false}, {Error, logError, "ERROR", false, false, true}, {Error, logError, "ERROR", false, false, false}, - {Error, logError, "ERROR", true, true, false}, + {Error, logError, "ERROR", true, false, false}, {Error, logErrorE, "ERROR", false, false, false}, {Error, logErrorE, "ERROR", true, true, false}, {Fatal, logDebug, "", false, false, true}, @@ -524,31 +525,31 @@ func TestLogWritesMessagesToLogGivenUpdatedLogPath(t *testing.T) { func logFeedbackInfo(l Logger, c context.Context, m string) { l.FeedbackInfo(c, m) } func logFeedbackError(l Logger, c context.Context, m string) { l.FeedbackError(c, m) } func logFeedbackErrorE(l Logger, c context.Context, m string) { - l.FeedbackErrorE(c, m, fmt.Errorf("test error")) + l.FeedbackErrorE(c, m, errors.New("test error")) } func getFeedbackLogLevelTestCase() []LogLevelTestCase { return []LogLevelTestCase{ {Debug, logFeedbackInfo, "INFO", false, false, false}, {Debug, logFeedbackError, "ERROR", false, false, false}, - {Debug, logFeedbackError, "ERROR", true, true, false}, + {Debug, logFeedbackError, "ERROR", true, false, false}, {Debug, logFeedbackErrorE, "ERROR", false, false, false}, {Debug, logFeedbackErrorE, "ERROR", true, true, false}, {Info, logFeedbackInfo, "INFO", false, false, true}, {Info, logFeedbackInfo, "INFO", false, false, false}, {Info, logFeedbackError, "ERROR", false, false, false}, - {Info, logFeedbackError, "ERROR", true, true, false}, + {Info, logFeedbackError, "ERROR", true, false, false}, {Info, logFeedbackErrorE, "ERROR", false, false, false}, {Info, logFeedbackErrorE, "ERROR", true, true, false}, {Warn, logFeedbackInfo, "", false, false, false}, {Warn, logFeedbackError, "ERROR", false, false, false}, - {Warn, logFeedbackError, "ERROR", true, true, false}, + {Warn, logFeedbackError, "ERROR", true, false, false}, {Warn, logFeedbackErrorE, "ERROR", false, false, false}, {Warn, logFeedbackErrorE, "ERROR", true, true, false}, {Error, logFeedbackInfo, "", false, false, false}, {Error, logFeedbackError, "ERROR", false, false, true}, {Error, logFeedbackError, "ERROR", false, false, false}, - {Error, logFeedbackError, "ERROR", true, true, false}, + {Error, logFeedbackError, "ERROR", true, false, false}, {Error, logFeedbackErrorE, "ERROR", false, false, false}, {Error, logFeedbackErrorE, "ERROR", true, true, false}, {Fatal, logFeedbackInfo, "", false, false, false}, @@ -595,7 +596,11 @@ func TestLogWritesMessagesToFeedbackLog(t *testing.T) { assert.Equal(t, tc.WithCaller, hasCaller) } - assert.Equal(t, logMessage+"\n", b.String()) + if tc.ExpectStackTrace { + assert.Contains(t, b.String(), logMessage+"\ntest error. Stack:") + } else { + assert.Equal(t, logMessage+"\n", b.String()) + } clearRegistry("TestLogName") } diff --git a/merkle/clock/clock.go b/merkle/clock/clock.go index 01ed8d4f71..2558fae116 100644 --- a/merkle/clock/clock.go +++ b/merkle/clock/clock.go @@ -16,6 +16,7 @@ import ( "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" cid "github.com/ipfs/go-cid" dshelp "github.com/ipfs/go-ipfs-ds-help" @@ -62,7 +63,7 @@ func (mc *MerkleClock) putBlock( node, err := makeNode(delta, heads) if err != nil { - return nil, fmt.Errorf("error creating block : %w", err) + return nil, errors.Wrap("error creating block ", err) } // @todo Add a DagSyncer instance to the MerkleCRDT structure @@ -73,11 +74,11 @@ func (mc *MerkleClock) putBlock( // ctx := context.Background() // err = mc.store.dagSyncer.Add(ctx, node) // if err != nil { - // return nil, fmt.Errorf("error writing new block %s : %w", node.Cid(), err) + // return nil, errors.Wrap("error writing new block %s ", node.Cid(), err) // } err = mc.dagstore.Put(ctx, node) if err != nil { - return nil, fmt.Errorf("error writing new block %s : %w", node.Cid(), err) + return nil, errors.Wrap(fmt.Sprintf("error writing new block %s ", node.Cid()), err) } return node, nil @@ -94,7 +95,7 @@ func (mc *MerkleClock) AddDAGNode( ) (cid.Cid, ipld.Node, error) { heads, height, err := mc.headset.List(ctx) if err != nil { - return cid.Undef, nil, fmt.Errorf("error getting heads : %w", err) + return cid.Undef, nil, errors.Wrap("error getting heads ", err) } height = height + 1 @@ -103,7 +104,7 @@ func (mc *MerkleClock) AddDAGNode( // write the delta and heads to a new block nd, err := mc.putBlock(ctx, heads, height, delta) if err != nil { - return cid.Undef, nil, fmt.Errorf("Error adding block : %w", err) + return cid.Undef, nil, errors.Wrap("Error adding block ", err) } // apply the new node and merge the delta with state @@ -118,7 +119,7 @@ func (mc *MerkleClock) AddDAGNode( ) if err != nil { - return cid.Undef, nil, fmt.Errorf("error processing new block : %w", err) + return cid.Undef, nil, errors.Wrap("error processing new block ", err) } return nd.Cid(), nd, nil //@todo: Include raw block data in return } @@ -137,7 +138,7 @@ func (mc *MerkleClock) ProcessNode( log.Debug(ctx, "Running ProcessNode", logging.NewKV("CID", current)) err := mc.crdt.Merge(ctx, delta, dshelp.MultihashToDsKey(current.Hash()).String()) if err != nil { - return nil, fmt.Errorf("error merging delta from %s : %w", current, err) + return nil, errors.Wrap(fmt.Sprintf("error merging delta from %s ", current), err) } links := node.Links() @@ -155,7 +156,7 @@ func (mc *MerkleClock) ProcessNode( log.Debug(ctx, "No heads found") err := mc.headset.Add(ctx, root, rootPrio) if err != nil { - return nil, fmt.Errorf("error adding head (when reached the bottom) %s : %w", root, err) + return nil, errors.Wrap(fmt.Sprintf("error adding head (when reached the bottom) %s ", root), err) } } @@ -166,7 +167,7 @@ func (mc *MerkleClock) ProcessNode( log.Debug(ctx, "Scanning for replacement heads", logging.NewKV("Child", child)) isHead, _, err := mc.headset.IsHead(ctx, child) if err != nil { - return nil, fmt.Errorf("error checking if %s is head : %w", child, err) + return nil, errors.Wrap(fmt.Sprintf("error checking if %s is head ", child), err) } if isHead { @@ -175,7 +176,7 @@ func (mc *MerkleClock) ProcessNode( // of current branch err = mc.headset.Replace(ctx, child, root, rootPrio) if err != nil { - return nil, fmt.Errorf("error replacing head: %s->%s : %w", child, root, err) + return nil, errors.Wrap(fmt.Sprintf("error replacing head: %s->%s ", child, root), err) } continue @@ -183,7 +184,7 @@ func (mc *MerkleClock) ProcessNode( known, err := mc.dagstore.Has(ctx, child) if err != nil { - return nil, fmt.Errorf("error checking for known block %s : %w", child, err) + return nil, errors.Wrap(fmt.Sprintf("error checking for known block %s ", child), err) } if known { // we reached a non-head node in the known tree. @@ -198,7 +199,7 @@ func (mc *MerkleClock) ProcessNode( logging.NewKV("Root", root), ) // OR should this also return like below comment?? - // return nil, fmt.Errorf("error adding head (when root is new head): %s : %w", root, err) + // return nil, errors.Wrap("error adding head (when root is new head): %s ", root, err) } continue } diff --git a/merkle/clock/heads.go b/merkle/clock/heads.go index b90ccf076e..df50304ccc 100644 --- a/merkle/clock/heads.go +++ b/merkle/clock/heads.go @@ -14,13 +14,11 @@ import ( "bytes" "context" "encoding/binary" - "fmt" "sort" - "errors" - "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" cid "github.com/ipfs/go-cid" @@ -162,7 +160,7 @@ func (hh *heads) List(ctx context.Context) ([]cid.Cid, uint64, error) { var maxHeight uint64 for r := range results.Next() { if r.Error != nil { - return nil, 0, fmt.Errorf("Failed to get next query result : %w", r.Error) + return nil, 0, errors.Wrap("Failed to get next query result ", r.Error) } headKey, err := core.NewHeadStoreKey(r.Key) diff --git a/merkle/clock/heads_test.go b/merkle/clock/heads_test.go index 3363149bfd..3a6e5ad9f8 100644 --- a/merkle/clock/heads_test.go +++ b/merkle/clock/heads_test.go @@ -14,7 +14,6 @@ import ( "bytes" "context" "encoding/binary" - "errors" "math/rand" "reflect" "sort" @@ -25,6 +24,7 @@ import ( mh "github.com/multiformats/go-multihash" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" ) func newRandomCID() cid.Cid { diff --git a/merkle/crdt/factory.go b/merkle/crdt/factory.go index 647bbb8e40..8086f4eaac 100644 --- a/merkle/crdt/factory.go +++ b/merkle/crdt/factory.go @@ -11,12 +11,11 @@ package crdt import ( - "errors" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corenet "github.com/sourcenetwork/defradb/core/net" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" ) var ( diff --git a/merkle/crdt/merklecrdt.go b/merkle/crdt/merklecrdt.go index 6494dbd917..134cdb3e42 100644 --- a/merkle/crdt/merklecrdt.go +++ b/merkle/crdt/merklecrdt.go @@ -12,11 +12,11 @@ package crdt import ( "context" - "fmt" "time" "github.com/sourcenetwork/defradb/core" corenet "github.com/sourcenetwork/defradb/core/net" + "github.com/sourcenetwork/defradb/errors" "github.com/ipfs/go-cid" ipld "github.com/ipfs/go-ipld-format" @@ -94,7 +94,7 @@ func (base *baseMerkleCRDT) Broadcast(ctx context.Context, nd ipld.Node, delta c c := nd.Cid() netdelta, ok := delta.(core.NetDelta) if !ok { - return fmt.Errorf("Can't broadcast a delta payload that doesn't implement core.NetDelta") + return errors.New("Can't broadcast a delta payload that doesn't implement core.NetDelta") } log.Debug( diff --git a/net/api/client/client.go b/net/api/client/client.go index ca0cd2528f..0a84eb4510 100644 --- a/net/api/client/client.go +++ b/net/api/client/client.go @@ -14,12 +14,12 @@ package client import ( "context" - "fmt" "github.com/libp2p/go-libp2p-core/peer" ma "github.com/multiformats/go-multiaddr" "google.golang.org/grpc" + "github.com/sourcenetwork/defradb/errors" pb "github.com/sourcenetwork/defradb/net/api/pb" ) @@ -52,17 +52,17 @@ func (c *Client) AddReplicator( paddr ma.Multiaddr, ) (peer.ID, error) { if len(collection) == 0 { - return "", fmt.Errorf("Collection can't be empty") + return "", errors.New("Collection can't be empty") } if paddr == nil { - return "", fmt.Errorf("target address can't be empty") + return "", errors.New("target address can't be empty") } resp, err := c.c.AddReplicator(ctx, &pb.AddReplicatorRequest{ Collection: []byte(collection), Addr: paddr.Bytes(), }) if err != nil { - return "", fmt.Errorf("AddReplicator request failed: %w", err) + return "", errors.Wrap("AddReplicator request failed", err) } return peer.IDFromBytes(resp.PeerID) } diff --git a/net/client.go b/net/client.go index 870ffedaaf..ba0994a179 100644 --- a/net/client.go +++ b/net/client.go @@ -19,6 +19,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" pb "github.com/sourcenetwork/defradb/net/pb" @@ -36,7 +37,7 @@ var ( func (s *server) pushLog(ctx context.Context, lg core.Log, pid peer.ID) error { dockey, err := client.NewDocKeyFromString(lg.DocKey) if err != nil { - return fmt.Errorf("Failed to get DocKey from broadcast message: %w", err) + return errors.Wrap("Failed to get DocKey from broadcast message", err) } log.Debug( ctx, @@ -65,14 +66,14 @@ func (s *server) pushLog(ctx context.Context, lg core.Log, pid peer.ID) error { client, err := s.dial(pid) // grpc dial over p2p stream if err != nil { - return fmt.Errorf("Failed to push log: %w", err) + return errors.Wrap("Failed to push log", err) } cctx, cancel := context.WithTimeout(ctx, PushTimeout) defer cancel() if _, err := client.PushLog(cctx, req); err != nil { - return fmt.Errorf("Failed PushLog RPC request %s for %s to %s: %w", lg.Cid, dockey, pid, err) + return errors.Wrap(fmt.Sprintf("Failed PushLog RPC request %s for %s to %s", lg.Cid, dockey, pid), err) } return nil } diff --git a/net/dialer.go b/net/dialer.go index a85f244458..1ce36c8d60 100644 --- a/net/dialer.go +++ b/net/dialer.go @@ -14,7 +14,6 @@ package net import ( "context" - "fmt" gonet "net" "time" @@ -26,6 +25,7 @@ import ( "google.golang.org/grpc/status" corenet "github.com/sourcenetwork/defradb/core/net" + "github.com/sourcenetwork/defradb/errors" pb "github.com/sourcenetwork/defradb/net/pb" ) @@ -64,12 +64,12 @@ func (s *server) getLibp2pDialer() grpc.DialOption { return grpc.WithContextDialer(func(ctx context.Context, peerIDStr string) (gonet.Conn, error) { id, err := libpeer.Decode(peerIDStr) if err != nil { - return nil, fmt.Errorf("grpc tried to dial non peerID: %w", err) + return nil, errors.Wrap("grpc tried to dial non peerID", err) } conn, err := gostream.Dial(ctx, s.peer.host, id, corenet.Protocol) if err != nil { - return nil, fmt.Errorf("gostream dial failed: %w", err) + return nil, errors.Wrap("gostream dial failed", err) } return conn, nil diff --git a/net/pb/custom.go b/net/pb/custom.go index 97c93ce45f..9f1700d322 100644 --- a/net/pb/custom.go +++ b/net/pb/custom.go @@ -14,7 +14,6 @@ package net_pb import ( "encoding/json" - "errors" "github.com/gogo/protobuf/proto" "github.com/ipfs/go-cid" @@ -23,6 +22,7 @@ import ( "github.com/multiformats/go-varint" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" ) // customGogoType aggregates the interfaces that custom Gogo types need to implement. diff --git a/net/peer.go b/net/peer.go index e81cf97e7f..6eafe8bd53 100644 --- a/net/peer.go +++ b/net/peer.go @@ -14,7 +14,6 @@ package net import ( "context" - "errors" "fmt" "sync" "time" @@ -34,6 +33,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" corenet "github.com/sourcenetwork/defradb/core/net" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/sourcenetwork/defradb/merkle/clock" pb "github.com/sourcenetwork/defradb/net/pb" @@ -86,7 +86,7 @@ func NewPeer( dialOptions []grpc.DialOption, ) (*Peer, error) { if db == nil { - return nil, fmt.Errorf("Database object can't be empty") + return nil, errors.New("Database object can't be empty") } ctx, cancel := context.WithCancel(ctx) @@ -162,12 +162,12 @@ func (p *Peer) Close() error { // close event emitters if p.server.pubSubEmitter != nil { if err := p.server.pubSubEmitter.Close(); err != nil { - log.Info(p.ctx, "Could not close pubsub event emitter", logging.NewKV("Error", err)) + log.Info(p.ctx, "Could not close pubsub event emitter", logging.NewKV("Error", err.Error())) } } if p.server.pushLogEmitter != nil { if err := p.server.pushLogEmitter.Close(); err != nil { - log.Info(p.ctx, "Could not close push log event emitter", logging.NewKV("Error", err)) + log.Info(p.ctx, "Could not close push log event emitter", logging.NewKV("Error", err.Error())) } } @@ -261,7 +261,7 @@ func (p *Peer) AddReplicator( // verify collection col, err := p.db.GetCollectionByName(ctx, collectionName) if err != nil { - return pid, fmt.Errorf("Failed to get collection for replicator: %w", err) + return pid, errors.Wrap("Failed to get collection for replicator", err) } // extra peerID @@ -277,7 +277,7 @@ func (p *Peer) AddReplicator( // make sure it's not ourselves if pid == p.host.ID() { - return pid, fmt.Errorf("Can't target ourselves as a replicator") + return pid, errors.New("Can't target ourselves as a replicator") } // make sure we're not duplicating things @@ -285,11 +285,11 @@ func (p *Peer) AddReplicator( defer p.mu.Unlock() if reps, exists := p.replicators[col.SchemaID()]; exists { if _, exists := reps[pid]; exists { - return pid, fmt.Errorf( + return pid, errors.New(fmt.Sprintf( "Replicator already exists for %s with ID %s", collectionName, pid, - ) + )) } } else { p.replicators[col.SchemaID()] = make(map[peer.ID]struct{}) @@ -299,7 +299,7 @@ func (p *Peer) AddReplicator( // Extract the peer ID from the multiaddr. info, err := peer.AddrInfoFromP2pAddr(paddr) if err != nil { - return pid, fmt.Errorf("Failed to address info from %s: %w", paddr, err) + return pid, errors.Wrap(fmt.Sprintf("Failed to address info from %s", paddr), err) } // Add the destination's peer multiaddress in the peerstore. @@ -312,7 +312,7 @@ func (p *Peer) AddReplicator( // create read only txn and assign to col txn, err := p.db.NewTxn(ctx, true) if err != nil { - return pid, fmt.Errorf("Failed to get txn: %w", err) + return pid, errors.Wrap("Failed to get txn", err) } col = col.WithTxn(txn) @@ -320,10 +320,12 @@ func (p *Peer) AddReplicator( keysCh, err := col.GetAllDocKeys(ctx) if err != nil { txn.Discard(ctx) - return pid, fmt.Errorf( - "Failed to get dockey for replicator %s on %s: %w", - pid, - collectionName, + return pid, errors.Wrap( + fmt.Sprintf( + "Failed to get dockey for replicator %s on %s", + pid, + collectionName, + ), err, ) } @@ -399,7 +401,7 @@ func (p *Peer) AddReplicator( func (p *Peer) handleDocCreateLog(lg core.Log) error { dockey, err := client.NewDocKeyFromString(lg.DocKey) if err != nil { - return fmt.Errorf("Failed to get DocKey from broadcast message: %w", err) + return errors.Wrap("Failed to get DocKey from broadcast message", err) } // push to each peer (replicator) @@ -411,7 +413,7 @@ func (p *Peer) handleDocCreateLog(lg core.Log) error { func (p *Peer) handleDocUpdateLog(lg core.Log) error { dockey, err := client.NewDocKeyFromString(lg.DocKey) if err != nil { - return fmt.Errorf("Failed to get DocKey from broadcast message: %w", err) + return errors.Wrap("Failed to get DocKey from broadcast message", err) } log.Debug( p.ctx, @@ -436,7 +438,7 @@ func (p *Peer) handleDocUpdateLog(lg core.Log) error { p.pushLogToReplicators(p.ctx, lg) if err := p.server.publishLog(p.ctx, lg.DocKey, req); err != nil { - return fmt.Errorf("Error publishing log %s for %s: %w", lg.Cid, lg.DocKey, err) + return errors.Wrap(fmt.Sprintf("Error publishing log %s for %s", lg.Cid, lg.DocKey), err) } return nil } diff --git a/net/process.go b/net/process.go index cf7070582d..9da46bfb7e 100644 --- a/net/process.go +++ b/net/process.go @@ -26,6 +26,7 @@ import ( "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/db/base" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/sourcenetwork/defradb/merkle/clock" "github.com/sourcenetwork/defradb/merkle/crdt" @@ -53,7 +54,7 @@ func (p *Peer) processLog( // check if we already have this block // exists, err := txn.DAGstore().Has(ctx, c) // if err != nil { - // return nil, fmt.Errorf("Failed to check for existing block %s: %w", c, err) + // return nil, errors.Wrap("Failed to check for existing block %s", c, err) // } // if exists { // log.Debugf("Already have block %s locally, skipping.", c) @@ -67,7 +68,7 @@ func (p *Peer) processLog( delta, err := crdt.DeltaDecode(nd) if err != nil { - return nil, fmt.Errorf("Failed to decode delta object: %w", err) + return nil, errors.Wrap("Failed to decode delta object", err) } log.Debug( @@ -116,7 +117,7 @@ func initCRDTForType( } else { fd, ok := description.GetField(field) if !ok { - return nil, fmt.Errorf("Couldn't find field %s for doc %s", field, docKey) + return nil, errors.New(fmt.Sprintf("Couldn't find field %s for doc %s", field, docKey)) } ctype = fd.Typ fieldID := fd.ID.String() @@ -129,7 +130,7 @@ func initCRDTForType( func decodeBlockBuffer(buf []byte, cid cid.Cid) (ipld.Node, error) { blk, err := blocks.NewBlockWithCid(buf, cid) if err != nil { - return nil, fmt.Errorf("Failed to create block: %w", err) + return nil, errors.Wrap("Failed to create block", err) } return format.Decode(blk) } diff --git a/net/server.go b/net/server.go index cbc23862d8..2b8a29bb23 100644 --- a/net/server.go +++ b/net/server.go @@ -28,6 +28,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" pb "github.com/sourcenetwork/defradb/net/pb" ) @@ -73,7 +74,7 @@ func newServer(p *Peer, db client.DB, opts ...grpc.DialOption) (*server, error) log.Debug(p.ctx, "Getting all existing DocKey...") keyResults, err := s.listAllDocKeys() if err != nil { - return nil, fmt.Errorf("Failed to get DocKeys for pubsub topic registration: %w", err) + return nil, errors.Wrap("Failed to get DocKeys for pubsub topic registration", err) } i := 0 @@ -100,11 +101,11 @@ func newServer(p *Peer, db client.DB, opts ...grpc.DialOption) (*server, error) var err error s.pubSubEmitter, err = s.peer.host.EventBus().Emitter(new(EvtPubSub)) if err != nil { - log.Info(s.peer.ctx, "could not create event emitter", logging.NewKV("Error", err)) + log.Info(s.peer.ctx, "could not create event emitter", logging.NewKV("Error", err.Error())) } s.pushLogEmitter, err = s.peer.host.EventBus().Emitter(new(EvtReceivedPushLog)) if err != nil { - log.Info(s.peer.ctx, "could not create event emitter", logging.NewKV("Error", err)) + log.Info(s.peer.ctx, "could not create event emitter", logging.NewKV("Error", err.Error())) } return s, nil @@ -151,7 +152,7 @@ func (s *server) PushLog(ctx context.Context, req *pb.PushLogRequest) (*pb.PushL docKey := core.DataStoreKeyFromDocKey(req.Body.DocKey.DocKey) col, err := s.db.GetCollectionBySchemaID(ctx, schemaID) if err != nil { - return nil, fmt.Errorf("Failed to get collection from schemaID %s: %w", schemaID, err) + return nil, errors.Wrap(fmt.Sprintf("Failed to get collection from schemaID %s", schemaID), err) } var getter format.NodeGetter = s.peer.ds @@ -163,7 +164,7 @@ func (s *server) PushLog(ctx context.Context, req *pb.PushLogRequest) (*pb.PushL // handleComposite nd, err := decodeBlockBuffer(req.Body.Log.Block, cid) if err != nil { - return nil, fmt.Errorf("Failed to decode block to ipld.Node: %w", err) + return nil, errors.Wrap("Failed to decode block to ipld.Node", err) } cids, err := s.peer.processLog(ctx, col, docKey, cid, "", nd, getter) if err != nil { @@ -198,7 +199,7 @@ func (s *server) PushLog(ctx context.Context, req *pb.PushLogRequest) (*pb.PushL if err != nil { // logging instead of returning an error because the event bus should // not break the PushLog execution. - log.Info(ctx, "could not emit push log event", logging.NewKV("Error", err)) + log.Info(ctx, "could not emit push log event", logging.NewKV("Error", err.Error())) } } @@ -277,16 +278,16 @@ func (s *server) publishLog(ctx context.Context, dockey string, req *pb.PushLogR t, ok := s.topics[dockey] s.mu.Unlock() if !ok { - return fmt.Errorf("No pubsub topic found for doc %s", dockey) + return errors.New(fmt.Sprintf("No pubsub topic found for doc %s", dockey)) } data, err := req.Marshal() if err != nil { - return fmt.Errorf("failed marshling pubsub message: %w", err) + return errors.Wrap("failed marshling pubsub message", err) } if _, err := t.Publish(ctx, data, rpc.WithIgnoreResponse(true)); err != nil { - return fmt.Errorf("failed publishing to thread %s: %w", dockey, err) + return errors.Wrap(fmt.Sprintf("failed publishing to thread %s", dockey), err) } log.Debug( ctx, @@ -316,7 +317,7 @@ func (s *server) pubSubMessageHandler(from libpeer.ID, topic string, msg []byte) }) if _, err := s.PushLog(ctx, req); err != nil { log.ErrorE(ctx, "Failed pushing log for doc", err, logging.NewKV("Topic", topic)) - return nil, fmt.Errorf("Failed pushing log for doc %s: %w", topic, err) + return nil, errors.Wrap(fmt.Sprintf("Failed pushing log for doc %s", topic), err) } return nil, nil } @@ -336,7 +337,7 @@ func (s *server) pubSubEventHandler(from libpeer.ID, topic string, msg []byte) { Peer: from, }) if err != nil { - log.Info(s.peer.ctx, "could not emit pubsub event", logging.NewKV("Error", err)) + log.Info(s.peer.ctx, "could not emit pubsub event", logging.NewKV("Error", err.Error())) } } } @@ -396,11 +397,11 @@ func (a addr) String() string { return a.id.Pretty() } func peerIDFromContext(ctx context.Context) (libpeer.ID, error) { ctxPeer, ok := grpcpeer.FromContext(ctx) if !ok { - return "", fmt.Errorf("unable to identify stream peer") + return "", errors.New("unable to identify stream peer") } pid, err := libpeer.Decode(ctxPeer.Addr.String()) if err != nil { - return "", fmt.Errorf("parsing stream peer id: %w", err) + return "", errors.Wrap("parsing stream peer id", err) } return pid, nil } diff --git a/net/utils/util.go b/net/utils/util.go index f0f90356a8..18df9a2b7d 100644 --- a/net/utils/util.go +++ b/net/utils/util.go @@ -17,6 +17,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" ma "github.com/multiformats/go-multiaddr" + "github.com/sourcenetwork/defradb/errors" ) func ParsePeers(addrs []string) ([]peer.AddrInfo, error) { @@ -34,7 +35,7 @@ func ParsePeers(addrs []string) ([]peer.AddrInfo, error) { func TCPAddrFromMultiAddr(maddr ma.Multiaddr) (string, error) { var addr string if maddr == nil { - return addr, fmt.Errorf("address can't be empty") + return addr, errors.New("address can't be empty") } ip4, err := maddr.ValueForProtocol(ma.P_IP4) if err != nil { diff --git a/node/node.go b/node/node.go index 58cefee1a8..38f73c0d31 100644 --- a/node/node.go +++ b/node/node.go @@ -34,6 +34,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-peerstore/pstoreds" pubsub "github.com/libp2p/go-libp2p-pubsub" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/textileio/go-libp2p-pubsub-rpc/finalizer" "github.com/textileio/go-threads/broadcast" @@ -115,15 +116,15 @@ func NewNode( rootstore, libp2pOpts..., ) + if err != nil { + return nil, fin.Cleanup(err) + } log.Info( ctx, "Created LibP2P host", logging.NewKV("PeerId", h.ID()), logging.NewKV("Address", options.ListenAddrs), ) - if err != nil { - return nil, fin.Cleanup(err) - } bstore := db.Blockstore() lite, err := ipfslite.New(ctx, rootstore, bstore, h, d, nil) @@ -248,7 +249,7 @@ func (n *Node) WaitForPeerConnectionEvent(id peer.ID) error { } return nil case <-time.After(evtWaitTimeout): - return fmt.Errorf("waiting for peer connection timed out") + return errors.New("waiting for peer connection timed out") } } } @@ -263,7 +264,7 @@ func (n *Node) WaitForPubSubEvent(id peer.ID) error { } return nil case <-time.After(evtWaitTimeout): - return fmt.Errorf("waiting for pubsub timed out") + return errors.New("waiting for pubsub timed out") } } } @@ -278,7 +279,7 @@ func (n *Node) WaitForPushLogEvent(id peer.ID) error { } return nil case <-time.After(evtWaitTimeout): - return fmt.Errorf("waiting for pushlog timed out") + return errors.New("waiting for pushlog timed out") } } } diff --git a/query/graphql/mapper/descriptions.go b/query/graphql/mapper/descriptions.go index 527f121162..4d98dfbd7c 100644 --- a/query/graphql/mapper/descriptions.go +++ b/query/graphql/mapper/descriptions.go @@ -13,11 +13,11 @@ package mapper import ( "context" "encoding/json" - "fmt" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" ) // DescriptionsRepo is a cache of previously requested collection descriptions @@ -50,7 +50,7 @@ func (r *DescriptionsRepo) getCollectionDesc(name string) (client.CollectionDesc key := core.NewCollectionKey(name) buf, err := r.txn.Systemstore().Get(r.ctx, key.ToDS()) if err != nil { - return client.CollectionDescription{}, fmt.Errorf("Failed to get collection description: %w", err) + return client.CollectionDescription{}, errors.Wrap("Failed to get collection description", err) } desc := client.CollectionDescription{} diff --git a/query/graphql/mapper/mapper.go b/query/graphql/mapper/mapper.go index 9423db2b47..b4df79c201 100644 --- a/query/graphql/mapper/mapper.go +++ b/query/graphql/mapper/mapper.go @@ -241,16 +241,16 @@ func resolveAggregates( hostSelect, isHostSelectable := host.AsSelect() if !isHostSelectable { // I believe this is dead code as the gql library should always catch this error first - return nil, fmt.Errorf( + return nil, errors.New( "Aggregate target host must be selectable, but was not", ) } if len(hostSelect.IndexesByName[target.childExternalName]) == 0 { // I believe this is dead code as the gql library should always catch this error first - return nil, fmt.Errorf( + return nil, errors.New(fmt.Sprintf( "Unable to identify aggregate child: %s", target.childExternalName, - ) + )) } childTarget = OptionalChildTarget{ @@ -460,10 +460,10 @@ func getRequestables( mapping.Add(index, f.Name) default: - return nil, nil, fmt.Errorf( + return nil, nil, errors.New(fmt.Sprintf( "Unexpected field type: %T", field, - ) + )) } } return @@ -476,7 +476,7 @@ func getAggregateRequests(index int, aggregate *parser.Select) (aggregateRequest } if len(aggregateTargets) == 0 { - return aggregateRequest{}, fmt.Errorf( + return aggregateRequest{}, errors.New( "Aggregate must be provided with a property to aggregate.", ) } diff --git a/query/graphql/parser/commit.go b/query/graphql/parser/commit.go index 18387ff7fd..0599007160 100644 --- a/query/graphql/parser/commit.go +++ b/query/graphql/parser/commit.go @@ -11,9 +11,8 @@ package parser import ( - "errors" - "github.com/graphql-go/graphql/language/ast" + "github.com/sourcenetwork/defradb/errors" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" ) diff --git a/query/graphql/parser/filter.go b/query/graphql/parser/filter.go index 7a497cb700..54ba75ddc6 100644 --- a/query/graphql/parser/filter.go +++ b/query/graphql/parser/filter.go @@ -11,15 +11,14 @@ package parser import ( - "errors" "fmt" "strconv" "strings" "github.com/graphql-go/graphql/language/ast" - gqlp "github.com/graphql-go/graphql/language/parser" gqls "github.com/graphql-go/graphql/language/source" + "github.com/sourcenetwork/defradb/errors" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" ) diff --git a/query/graphql/parser/mutation.go b/query/graphql/parser/mutation.go index e8f76d5f1b..5b937f22d2 100644 --- a/query/graphql/parser/mutation.go +++ b/query/graphql/parser/mutation.go @@ -11,11 +11,10 @@ package parser import ( - "errors" "strings" "github.com/graphql-go/graphql/language/ast" - + "github.com/sourcenetwork/defradb/errors" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" ) diff --git a/query/graphql/parser/query.go b/query/graphql/parser/query.go index f8cd6bc926..8bca97bb63 100644 --- a/query/graphql/parser/query.go +++ b/query/graphql/parser/query.go @@ -11,11 +11,10 @@ package parser import ( - "errors" "strconv" "github.com/graphql-go/graphql/language/ast" - + "github.com/sourcenetwork/defradb/errors" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" ) diff --git a/query/graphql/planner/average.go b/query/graphql/planner/average.go index b8afc0f35b..c109a5541b 100644 --- a/query/graphql/planner/average.go +++ b/query/graphql/planner/average.go @@ -14,6 +14,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" ) @@ -42,7 +43,7 @@ func (p *Planner) Average( case parserTypes.SumFieldName: sumField = dependency default: - return nil, fmt.Errorf("Unknown dependency, name: %s", dependency.Name) + return nil, errors.New(fmt.Sprintf("Unknown dependency, name: %s", dependency.Name)) } } @@ -75,7 +76,7 @@ func (n *averageNode) Next() (bool, error) { countProp := n.currentValue.Fields[n.countFieldIndex] typedCount, isInt := countProp.(int) if !isInt { - return false, fmt.Errorf("Expected count to be int but was: %T", countProp) + return false, errors.New(fmt.Sprintf("Expected count to be int but was: %T", countProp)) } count := typedCount @@ -91,7 +92,7 @@ func (n *averageNode) Next() (bool, error) { case int64: n.currentValue.Fields[n.virtualFieldIndex] = float64(sum) / float64(count) default: - return false, fmt.Errorf("Expected sum to be either float64 or int64 or int but was: %T", sumProp) + return false, errors.New(fmt.Sprintf("Expected sum to be either float64 or int64 or int but was: %T", sumProp)) } return true, nil diff --git a/query/graphql/planner/commit.go b/query/graphql/planner/commit.go index 502e2b4e21..ea42f7ff86 100644 --- a/query/graphql/planner/commit.go +++ b/query/graphql/planner/commit.go @@ -11,11 +11,11 @@ package planner import ( - "fmt" "math" cid "github.com/ipfs/go-cid" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" ) @@ -111,7 +111,7 @@ func (p *Planner) CommitSelect(parsed *mapper.CommitSelect) (planNode, error) { case mapper.AllCommits: commit, err = p.commitSelectAll(parsed) default: - return nil, fmt.Errorf("Invalid CommitSelect type") + return nil, errors.New("Invalid CommitSelect type") } if err != nil { return nil, err diff --git a/query/graphql/planner/create.go b/query/graphql/planner/create.go index aa10b626cb..b50a4b1466 100644 --- a/query/graphql/planner/create.go +++ b/query/graphql/planner/create.go @@ -12,11 +12,11 @@ package planner import ( "encoding/json" - "fmt" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" ) @@ -54,7 +54,7 @@ func (n *createNode) Init() error { return nil } func (n *createNode) Start() error { // parse the doc if n.newDocStr == "" { - return fmt.Errorf("Invalid document to create") + return errors.New("Invalid document to create") } doc, err := client.NewDocFromJSON([]byte(n.newDocStr)) diff --git a/query/graphql/planner/dagscan.go b/query/graphql/planner/dagscan.go index 19d5d8ddb4..9465098f98 100644 --- a/query/graphql/planner/dagscan.go +++ b/query/graphql/planner/dagscan.go @@ -33,11 +33,11 @@ package planner import ( "container/list" - "fmt" "strings" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/fetcher" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" "github.com/fxamacker/cbor/v2" @@ -268,7 +268,7 @@ func (n *dagScanNode) Next() (bool, error) { c := n.queuedCids.Front() cid, ok := c.Value.(cid.Cid) if !ok { - return false, fmt.Errorf("Queued value in DAGScan isn't a CID") + return false, errors.New("Queued value in DAGScan isn't a CID") } n.queuedCids.Remove(c) n.cid = &cid @@ -280,7 +280,7 @@ func (n *dagScanNode) Next() (bool, error) { val := n.headset.Value() cid, ok := n.parsed.DocumentMapping.FirstOfName(val, "cid").(cid.Cid) if !ok { - return false, fmt.Errorf("Headset scan node returned an invalid cid") + return false, errors.New("Headset scan node returned an invalid cid") } n.cid = &cid } else if n.cid == nil { @@ -390,7 +390,7 @@ func (n *dagScanNode) dagBlockToNodeDoc(block blocks.Block) (core.Doc, []*ipld.L prio, ok := delta["Priority"].(uint64) if !ok { - return core.Doc{}, nil, fmt.Errorf("Commit Delta missing priority key") + return core.Doc{}, nil, errors.New("Commit Delta missing priority key") } n.parsed.DocumentMapping.SetFirstOfName(&commit, "height", int64(prio)) diff --git a/query/graphql/planner/datasource.go b/query/graphql/planner/datasource.go index 389528b9af..d2535fb1e0 100644 --- a/query/graphql/planner/datasource.go +++ b/query/graphql/planner/datasource.go @@ -12,10 +12,10 @@ package planner import ( "encoding/json" - "fmt" "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" ) @@ -38,7 +38,7 @@ func (p *Planner) getSource(parsed *mapper.Select) (planSource, error) { // @todo: Add field selection func (p *Planner) getCollectionScanPlan(parsed *mapper.Select) (planSource, error) { if parsed.CollectionName == "" { - return planSource{}, fmt.Errorf("collection name cannot be empty") + return planSource{}, errors.New("collection name cannot be empty") } colDesc, err := p.getCollectionDesc(parsed.CollectionName) if err != nil { @@ -64,7 +64,7 @@ func (p *Planner) getCollectionDesc(name string) (client.CollectionDescription, var desc client.CollectionDescription buf, err := p.txn.Systemstore().Get(p.ctx, key.ToDS()) if err != nil { - return desc, fmt.Errorf("Failed to get collection description: %w", err) + return desc, errors.Wrap("Failed to get collection description", err) } err = json.Unmarshal(buf, &desc) diff --git a/query/graphql/planner/executor.go b/query/graphql/planner/executor.go index 56065013db..64d4b308e3 100644 --- a/query/graphql/planner/executor.go +++ b/query/graphql/planner/executor.go @@ -16,6 +16,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" "github.com/sourcenetwork/defradb/query/graphql/parser" "github.com/sourcenetwork/defradb/query/graphql/schema" @@ -40,7 +41,7 @@ type QueryExecutor struct { func NewQueryExecutor(manager *schema.SchemaManager) (*QueryExecutor, error) { if manager == nil { - return nil, fmt.Errorf("SchemaManager cannot be nil") + return nil, errors.New("SchemaManager cannot be nil") } return &QueryExecutor{ @@ -55,7 +56,7 @@ func (e *QueryExecutor) MakeSelectQuery( selectStmt *mapper.Select, ) (Query, error) { if selectStmt == nil { - return nil, fmt.Errorf("Cannot create query without a selection") + return nil, errors.New("Cannot create query without a selection") } planner := makePlanner(ctx, db, txn) return planner.makePlan(selectStmt) @@ -101,7 +102,7 @@ func (e *QueryExecutor) ParseRequestString(request string) (*parser.Query, error schema := e.SchemaManager.Schema() validationResult := gql.ValidateDocument(schema, ast, nil) if !validationResult.IsValid { - return nil, fmt.Errorf("%v", validationResult.Errors) + return nil, errors.New(fmt.Sprintf("%v", validationResult.Errors)) } return parser.ParseQuery(ast) diff --git a/query/graphql/planner/group.go b/query/graphql/planner/group.go index c623f47862..1028e9b346 100644 --- a/query/graphql/planner/group.go +++ b/query/graphql/planner/group.go @@ -14,6 +14,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" @@ -235,10 +236,10 @@ func (n *groupNode) Explain() (map[string]any, error) { // Try to find the name of this index. fieldName, found := n.documentMapping.TryToFindNameFromIndex(orderFieldIndex) if !found { - return nil, fmt.Errorf( + return nil, errors.New(fmt.Sprintf( "No order field name (for grouping) was found for index =%d", orderFieldIndex, - ) + )) } orderFieldNames = append(orderFieldNames, fieldName) diff --git a/query/graphql/planner/multi.go b/query/graphql/planner/multi.go index ab8e8663ca..623e603a53 100644 --- a/query/graphql/planner/multi.go +++ b/query/graphql/planner/multi.go @@ -11,10 +11,8 @@ package planner import ( - "errors" - "fmt" - "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" ) /* @@ -394,14 +392,14 @@ func (s *selectNode) addSubPlan(fieldIndex int, plan planNode) error { m.addChild(fieldIndex, plan) s.source = m default: - return fmt.Errorf("Sub plan needs to be either a MergeNode or an AppendNode") + return errors.New("Sub plan needs to be either a MergeNode or an AppendNode") } // source is a mergeNode, like a TypeJoin case mergeNode: origScan := s.p.walkAndFindPlanType(plan, &scanNode{}).(*scanNode) if origScan == nil { - return fmt.Errorf("Failed to find original scan node in plan graph") + return errors.New("Failed to find original scan node in plan graph") } // create our new multiscanner multiscan := &multiScanNode{scanNode: origScan} @@ -448,7 +446,7 @@ func (s *selectNode) addSubPlan(fieldIndex int, plan planNode) error { // add our newly updated plan to the multinode node.addChild(fieldIndex, plan) default: - return fmt.Errorf("Sub plan needs to be either a MergeNode or an AppendNode") + return errors.New("Sub plan needs to be either a MergeNode or an AppendNode") } } return nil diff --git a/query/graphql/planner/order.go b/query/graphql/planner/order.go index 4d11b93dbf..ab1706df9a 100644 --- a/query/graphql/planner/order.go +++ b/query/graphql/planner/order.go @@ -14,6 +14,7 @@ import ( "fmt" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" ) @@ -109,7 +110,7 @@ func (n *orderNode) Explain() (map[string]any, error) { // Try to find the name of this index. fieldName, found := n.documentMapping.TryToFindNameFromIndex(fieldIndex) if !found { - return nil, fmt.Errorf("No corresponding name was found for index=%d", fieldIndex) + return nil, errors.New(fmt.Sprintf("No corresponding name was found for index=%d", fieldIndex)) } fieldNames = append(fieldNames, fieldName) diff --git a/query/graphql/planner/planner.go b/query/graphql/planner/planner.go index 8fe6162c19..547d0908d4 100644 --- a/query/graphql/planner/planner.go +++ b/query/graphql/planner/planner.go @@ -18,6 +18,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/sourcenetwork/defradb/query/graphql/mapper" "github.com/sourcenetwork/defradb/query/graphql/parser" @@ -111,12 +112,12 @@ func (p *Planner) newPlan(stmt any) (planNode, error) { } else if len(n.Mutations) > 0 { return p.newPlan(n.Mutations[0]) // @todo: handle multiple mutation statements } else { - return nil, fmt.Errorf("Query is missing query or mutation statements") + return nil, errors.New("Query is missing query or mutation statements") } case *parser.OperationDefinition: if len(n.Selections) == 0 { - return nil, fmt.Errorf("OperationDefinition is missing selections") + return nil, errors.New("OperationDefinition is missing selections") } return p.newPlan(n.Selections[0]) @@ -151,7 +152,7 @@ func (p *Planner) newPlan(stmt any) (planNode, error) { } return p.newObjectMutationPlan(m) } - return nil, fmt.Errorf("Unknown statement type %T", stmt) + return nil, errors.New(fmt.Sprintf("Unknown statement type %T", stmt)) } func (p *Planner) newObjectMutationPlan(stmt *mapper.Mutation) (planNode, error) { @@ -166,7 +167,7 @@ func (p *Planner) newObjectMutationPlan(stmt *mapper.Mutation) (planNode, error) return p.DeleteDocs(stmt) default: - return nil, fmt.Errorf("Unknown mutation action %T", stmt.Type) + return nil, errors.New(fmt.Sprintf("Unknown mutation action %T", stmt.Type)) } } @@ -317,7 +318,7 @@ func (p *Planner) expandTypeIndexJoinPlan(plan *typeIndexJoin, parentPlan *selec case *typeJoinMany: return p.expandPlan(node.subType, parentPlan) } - return fmt.Errorf("Unknown type index join plan") + return errors.New("Unknown type index join plan") } func (p *Planner) expandGroupNodePlan(plan *selectTopNode) error { @@ -412,7 +413,7 @@ func (p *Planner) walkAndReplacePlan(plan, target, replace planNode) error { /* Do nothing - pipe nodes should not be replaced */ // @todo: add more nodes that apply here default: - return fmt.Errorf("Unknown plan node type to replace: %T", node) + return errors.New(fmt.Sprintf("Unknown plan node type to replace: %T", node)) } return nil @@ -442,7 +443,7 @@ func (p *Planner) explainRequest( plan planNode, ) ([]map[string]any, error) { if plan == nil { - return nil, fmt.Errorf("Can't explain request of a nil plan.") + return nil, errors.New("Can't explain request of a nil plan.") } explainGraph, err := buildExplainGraph(plan) @@ -465,7 +466,7 @@ func (p *Planner) executeRequest( plan planNode, ) ([]map[string]any, error) { if plan == nil { - return nil, fmt.Errorf("Can't execute request of a nil plan.") + return nil, errors.New("Can't execute request of a nil plan.") } if err := plan.Start(); err != nil { @@ -533,10 +534,10 @@ func multiErr(errorsToWrap ...error) error { continue } if errs == nil { - errs = fmt.Errorf("%w", err) + errs = errors.New(err.Error()) continue } - errs = fmt.Errorf("%s: %w", errs, err) + errs = errors.Wrap(fmt.Sprintf("%s", errs), err) } return errs } diff --git a/query/graphql/planner/select.go b/query/graphql/planner/select.go index 4919299183..ee16c93444 100644 --- a/query/graphql/planner/select.go +++ b/query/graphql/planner/select.go @@ -11,8 +11,6 @@ package planner import ( - "fmt" - "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" "github.com/sourcenetwork/defradb/db/fetcher" @@ -212,8 +210,8 @@ func (n *selectNode) initSource() ([]aggregateNode, error) { if n.parsed.Cid != "" { c, err := cid.Decode(n.parsed.Cid) if err != nil { - return nil, fmt.Errorf( - "Failed to propagate VersionFetcher span, invalid CID: %w", + return nil, errors.Wrap( + "Failed to propagate VersionFetcher span, invalid CID", err, ) } diff --git a/query/graphql/planner/sum.go b/query/graphql/planner/sum.go index 5c4763479c..bdd4f801b7 100644 --- a/query/graphql/planner/sum.go +++ b/query/graphql/planner/sum.go @@ -15,6 +15,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/core/enumerable" "github.com/sourcenetwork/defradb/query/graphql/mapper" @@ -78,10 +79,10 @@ func (p *Planner) isValueFloat( fieldDescription, fieldDescriptionFound := parentDescription.GetField(source.Name) if !fieldDescriptionFound { - return false, fmt.Errorf( + return false, errors.New(fmt.Sprintf( "Unable to find field description for field: %s", source.Name, - ) + )) } return fieldDescription.Kind == client.FieldKind_FLOAT_ARRAY || fieldDescription.Kind == client.FieldKind_FLOAT || @@ -96,7 +97,7 @@ func (p *Planner) isValueFloat( child, isChildSelect := parent.FieldAt(source.Index).AsSelect() if !isChildSelect { - return false, fmt.Errorf("Expected child select but none was found") + return false, errors.New("Expected child select but none was found") } if _, isAggregate := parserTypes.Aggregates[source.ChildTarget.Name]; isAggregate { @@ -130,7 +131,7 @@ func (p *Planner) isValueFloat( fieldDescription, fieldDescriptionFound := childCollectionDescription.GetField(source.ChildTarget.Name) if !fieldDescriptionFound { return false, - fmt.Errorf("Unable to find child field description for field: %s", source.ChildTarget.Name) + errors.New(fmt.Sprintf("Unable to find child field description for field: %s", source.ChildTarget.Name)) } return fieldDescription.Kind == client.FieldKind_FLOAT_ARRAY || diff --git a/query/graphql/planner/top.go b/query/graphql/planner/top.go index 802947c396..5a633b3c44 100644 --- a/query/graphql/planner/top.go +++ b/query/graphql/planner/top.go @@ -11,9 +11,8 @@ package planner import ( - "errors" - "github.com/sourcenetwork/defradb/core" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" ) diff --git a/query/graphql/planner/type_join.go b/query/graphql/planner/type_join.go index 7d6507f025..2f4c4c5090 100644 --- a/query/graphql/planner/type_join.go +++ b/query/graphql/planner/type_join.go @@ -17,6 +17,7 @@ import ( "github.com/sourcenetwork/defradb/connor" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" "github.com/sourcenetwork/defradb/query/graphql/schema" ) @@ -83,8 +84,8 @@ func (p *Planner) makeTypeIndexJoin( desc := parent.sourceInfo.collectionDescription typeFieldDesc, ok := desc.GetField(subType.Name) if !ok { - // return nil, fmt.Errorf("Unknown field on sub selection") - return nil, fmt.Errorf("Unknown field %s on sub selection", subType.Name) + // return nil, errors.Wrap("Unknown field on sub selection") + return nil, errors.New(fmt.Sprintf("Unknown field %s on sub selection", subType.Name)) } meta := typeFieldDesc.RelationType @@ -93,7 +94,7 @@ func (p *Planner) makeTypeIndexJoin( } else if schema.IsOneToMany(meta) { // Many side of One-to-Many joinPlan, err = p.makeTypeJoinMany(parent, source, subType) } else { // more to come, Many-to-Many, Embedded? - return nil, fmt.Errorf("Failed sub selection, unknown relation type") + return nil, errors.New("Failed sub selection, unknown relation type") } if err != nil { return nil, err @@ -184,7 +185,7 @@ func (n *typeIndexJoin) Explain() (map[string]any, error) { explainerMap[joinSubTypeLabel] = subTypeExplainGraph default: - return explainerMap, fmt.Errorf("Unknown type of an index join to explain.") + return explainerMap, errors.New("Unknown type of an index join to explain.") } return explainerMap, nil @@ -269,7 +270,7 @@ func (p *Planner) makeTypeJoinOne( // get the correct sub field schema type (collection) subTypeFieldDesc, ok := parent.sourceInfo.collectionDescription.GetField(subType.Name) if !ok { - return nil, fmt.Errorf("couldn't find subtype field description for typeJoin node") + return nil, errors.New("couldn't find subtype field description for typeJoin node") } // determine relation direction (primary or secondary?) diff --git a/query/graphql/planner/versionedscan.go b/query/graphql/planner/versionedscan.go index 1ef987817d..6ded708b4f 100644 --- a/query/graphql/planner/versionedscan.go +++ b/query/graphql/planner/versionedscan.go @@ -11,11 +11,10 @@ package planner import ( - "errors" - "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/fetcher" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" "github.com/ipfs/go-cid" diff --git a/query/graphql/schema/descriptions.go b/query/graphql/schema/descriptions.go index c704a30cbb..2970c2a275 100644 --- a/query/graphql/schema/descriptions.go +++ b/query/graphql/schema/descriptions.go @@ -11,14 +11,13 @@ package schema import ( - "errors" "fmt" "sort" "strings" - "github.com/sourcenetwork/defradb/client" - gql "github.com/graphql-go/graphql" + "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" ) @@ -167,7 +166,7 @@ func (g *Generator) CreateDescriptions( // let's make sure its an _id field, otherwise // we might have an error here if !strings.HasSuffix(fname, "_id") { - return nil, fmt.Errorf("Error: found a duplicate field '%s' for type %s", fname, t.Name()) + return nil, errors.New(fmt.Sprintf("Error: found a duplicate field '%s' for type %s", fname, t.Name())) } continue } @@ -186,12 +185,12 @@ func (g *Generator) CreateDescriptions( rel := g.manager.Relations.GetRelationByDescription( fname, schemaName, t.Name()) if rel == nil { - return nil, fmt.Errorf( + return nil, errors.New(fmt.Sprintf( "Field missing associated relation. FieldName: %s, SchemaType: %s, ObjectType: %s", fname, field.Type.Name(), t.Name(), - ) + )) } fd.RelationName = rel.name diff --git a/query/graphql/schema/generate.go b/query/graphql/schema/generate.go index 32561dad1c..01bf07f1a7 100644 --- a/query/graphql/schema/generate.go +++ b/query/graphql/schema/generate.go @@ -303,11 +303,11 @@ func (g *Generator) createExpandedFieldAggregate( filterTypeName = targeted.Type.Name() + "FilterArg" } } else { - return fmt.Errorf( + return errors.New(fmt.Sprintf( "Aggregate target not found. HostObject: {%s}, Target: {%s}", obj.Name(), target, - ) + )) } } @@ -384,7 +384,7 @@ func (g *Generator) buildTypesFromAST( case *ast.ObjectDefinition: // check if type exists if _, ok := g.manager.schema.TypeMap()[defType.Name.Value]; ok { - return nil, fmt.Errorf("Schema type already exists: %s", defType.Name.Value) + return nil, errors.New(fmt.Sprintf("Schema type already exists: %s", defType.Name.Value)) } objconf := gql.ObjectConfig{} @@ -485,10 +485,10 @@ func (g *Generator) buildTypesFromAST( gqlType, ok := g.manager.schema.TypeMap()[defType.Name.Value] if !ok { - return nil, fmt.Errorf( + return nil, errors.New(fmt.Sprintf( "object not found whilst executing fields thunk: %s", defType.Name.Value, - ) + )) } fields[parserTypes.GroupFieldName] = &gql.Field{ @@ -528,10 +528,10 @@ func getRelationshipName( if argument.Name.Value == "name" { name, isString := argument.Value.GetValue().(string) if !isString { - return "", fmt.Errorf( + return "", errors.New(fmt.Sprintf( "Relationship name must be of type string, but was: %v", argument.Value.GetKind(), - ) + )) } return name, nil } @@ -965,7 +965,7 @@ func astNodeToGqlType(typeMap map[string]gql.Type, t ast.Type) (gql.Type, error) name := t.(*ast.Named).Name.Value ttype, ok := typeMap[name] if !ok { - return nil, fmt.Errorf("No type found for given name: %s", name) + return nil, errors.New(fmt.Sprintf("No type found for given name: %s", name)) } return ttype, nil diff --git a/query/graphql/schema/relations.go b/query/graphql/schema/relations.go index 32ffbf5806..8727f6218f 100644 --- a/query/graphql/schema/relations.go +++ b/query/graphql/schema/relations.go @@ -11,11 +11,11 @@ package schema import ( - "errors" "fmt" "strings" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" ) // type uint8 uint8 diff --git a/tests/bench/bench_util.go b/tests/bench/bench_util.go index f97413e3bb..5b3cf076ca 100644 --- a/tests/bench/bench_util.go +++ b/tests/bench/bench_util.go @@ -24,6 +24,7 @@ import ( ds "github.com/ipfs/go-datastore" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" "github.com/sourcenetwork/defradb/tests/bench/fixtures" testutils "github.com/sourcenetwork/defradb/tests/integration" @@ -78,14 +79,14 @@ func SetupCollections( // b.Logf("Loading schema: \n%s", schema) if err := db.AddSchema(ctx, schema); err != nil { - return nil, fmt.Errorf("Couldn't load schema: %w", err) + return nil, errors.Wrap("Couldn't load schema", err) } // loop to get collections for i := 0; i < numTypes; i++ { col, err := db.GetCollectionByName(ctx, fixture.TypeName(i)) if err != nil { - return nil, fmt.Errorf("Couldn't get the collection %v: %w", fixture.TypeName(i), err) + return nil, errors.Wrap(fmt.Sprintf("Couldn't get the collection %v", fixture.TypeName(i)), err) } // b.Logf("Collection Name: %s", col.Name()) collections[i] = col @@ -102,7 +103,7 @@ func ConstructSchema(fixture fixtures.Generator) (string, error) { for i := 0; i < numTypes; i++ { gql, err := fixtures.ExtractGQLFromType(fixture.Types()[i]) if err != nil { - return "", fmt.Errorf("failed generating GQL: %w", err) + return "", errors.Wrap("failed generating GQL", err) } schema += gql @@ -169,7 +170,7 @@ func BackfillBenchmarkDB( go func(index int) { docs, err := fixture.GenerateDocs() if err != nil { - errCh <- fmt.Errorf("Failed to generate document payload from fixtures: %w", err) + errCh <- errors.Wrap("Failed to generate document payload from fixtures", err) return } @@ -178,7 +179,7 @@ func BackfillBenchmarkDB( for j := 0; j < numTypes; j++ { doc, err := client.NewDocFromJSON([]byte(docs[j])) if err != nil { - errCh <- fmt.Errorf("Failed to create document from fixture: %w", err) + errCh <- errors.Wrap("Failed to create document from fixture", err) return } @@ -196,7 +197,7 @@ func BackfillBenchmarkDB( ) continue } else if err != nil { - errCh <- fmt.Errorf("Failed to create document: %w", err) + errCh <- errors.Wrap("Failed to create document", err) } keys[j] = doc.Key() break @@ -253,11 +254,11 @@ func newBenchStoreInfo(ctx context.Context, t testing.TB) (dbInfo, error) { case "memorymap": dbi, err = testutils.NewMapDB(ctx) default: - return nil, fmt.Errorf("invalid storage engine backend: %s", storage) + return nil, errors.New(fmt.Sprintf("invalid storage engine backend: %s", storage)) } if err != nil { - return nil, fmt.Errorf("Failed to create storage backend: %w", err) + return nil, errors.Wrap("Failed to create storage backend", err) } return dbi, err } diff --git a/tests/bench/collection/utils.go b/tests/bench/collection/utils.go index 92691a4aaa..9008de823f 100644 --- a/tests/bench/collection/utils.go +++ b/tests/bench/collection/utils.go @@ -18,6 +18,7 @@ import ( "testing" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" benchutils "github.com/sourcenetwork/defradb/tests/bench" "github.com/sourcenetwork/defradb/tests/bench/fixtures" ) @@ -159,7 +160,7 @@ func runCollectionBenchCreateMany( // CreateMany make sure numTypes == 1 since we only support that for now // @todo: Add support for numTypes > 1 later if numTypes != 1 { - return fmt.Errorf("Invalid number of types for create many, have %v but max is 1", numTypes) + return errors.New(fmt.Sprintf("Invalid number of types for create many, have %v but max is 1", numTypes)) } // run benchmark diff --git a/tests/bench/fixtures/fixtures.go b/tests/bench/fixtures/fixtures.go index a0af6a687a..e70b6e0cd9 100644 --- a/tests/bench/fixtures/fixtures.go +++ b/tests/bench/fixtures/fixtures.go @@ -14,10 +14,11 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "reflect" + "github.com/sourcenetwork/defradb/errors" + "github.com/bxcodec/faker" ) diff --git a/tests/bench/query/planner/utils.go b/tests/bench/query/planner/utils.go index 8b78d5ad72..fd5442024a 100644 --- a/tests/bench/query/planner/utils.go +++ b/tests/bench/query/planner/utils.go @@ -12,9 +12,9 @@ package planner import ( "context" - "fmt" "testing" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/planner" "github.com/sourcenetwork/defradb/query/graphql/schema" benchutils "github.com/sourcenetwork/defradb/tests/bench" @@ -36,7 +36,7 @@ func runQueryParserBench( for i := 0; i < b.N; i++ { _, err := exec.ParseRequestString(query) if err != nil { - return fmt.Errorf("Failed to parse query string: %w", err) + return errors.Wrap("Failed to parse query string", err) } } b.StopTimer() @@ -63,18 +63,18 @@ func runMakePlanBench( q, err := exec.ParseRequestString(query) if err != nil { - return fmt.Errorf("Failed to parse query string: %w", err) + return errors.Wrap("Failed to parse query string", err) } txn, err := db.NewTxn(ctx, false) if err != nil { - return fmt.Errorf("Failed to create txn: %w", err) + return errors.Wrap("Failed to create txn", err) } b.ResetTimer() for i := 0; i < b.N; i++ { _, err := exec.MakePlanFromParser(ctx, db, txn, q) if err != nil { - return fmt.Errorf("Failed to make plan: %w", err) + return errors.Wrap("Failed to make plan", err) } } b.StopTimer() diff --git a/tests/bench/query/simple/utils.go b/tests/bench/query/simple/utils.go index 0923f823fd..55f54f16a3 100644 --- a/tests/bench/query/simple/utils.go +++ b/tests/bench/query/simple/utils.go @@ -18,6 +18,7 @@ import ( "testing" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" benchutils "github.com/sourcenetwork/defradb/tests/bench" "github.com/sourcenetwork/defradb/tests/bench/fixtures" ) @@ -71,13 +72,13 @@ func runQueryBenchGetSync( for i := 0; i < b.N; i++ { res := db.ExecQuery(ctx, query) if len(res.Errors) > 0 { - return fmt.Errorf("Query error: %v", res.Errors) + return errors.New(fmt.Sprintf("Query error: %v", res.Errors)) } // leave comments for debug!! // l := len(res.Data.([]map[string]any)) // if l != opCount { - // return fmt.Errorf( + // return errors.Wrap( // "Invalid response, returned data doesn't match length, expected %v actual %v", // docCount, l) // } diff --git a/tests/integration/collection/utils.go b/tests/integration/collection/utils.go index 464739034c..cbee567689 100644 --- a/tests/integration/collection/utils.go +++ b/tests/integration/collection/utils.go @@ -12,11 +12,11 @@ package collection import ( "context" - "fmt" "strings" "testing" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" testUtils "github.com/sourcenetwork/defradb/tests/integration" "github.com/stretchr/testify/assert" ) @@ -115,7 +115,7 @@ func assertError(t *testing.T, description string, err error, expectedError stri return false } else { if !strings.Contains(err.Error(), expectedError) { - assert.ErrorIs(t, err, fmt.Errorf(expectedError)) + assert.ErrorIs(t, err, errors.New(expectedError)) return false } return true diff --git a/tests/integration/net/utils.go b/tests/integration/net/utils.go index 2e3b370aa9..6d479ada83 100644 --- a/tests/integration/net/utils.go +++ b/tests/integration/net/utils.go @@ -21,6 +21,7 @@ import ( "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/config" coreDB "github.com/sourcenetwork/defradb/db" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" netutils "github.com/sourcenetwork/defradb/net/utils" "github.com/sourcenetwork/defradb/node" @@ -104,7 +105,7 @@ func setupDefraNode(t *testing.T, cfg *config.Config, seeds []string) (*node.Nod cfg.NodeConfig(), ) if err != nil { - return nil, nil, fmt.Errorf("failed to start P2P node: %w", err) + return nil, nil, errors.Wrap("failed to start P2P node", err) } // parse peers and bootstrap @@ -112,7 +113,7 @@ func setupDefraNode(t *testing.T, cfg *config.Config, seeds []string) (*node.Nod log.Info(ctx, "Parsing bootstrap peers", logging.NewKV("Peers", cfg.Net.Peers)) addrs, err := netutils.ParsePeers(strings.Split(cfg.Net.Peers, ",")) if err != nil { - return nil, nil, fmt.Errorf("failed to parse bootstrap peers %v: %w", cfg.Net.Peers, err) + return nil, nil, errors.Wrap(fmt.Sprintf("failed to parse bootstrap peers %v", cfg.Net.Peers), err) } log.Info(ctx, "Bootstrapping with peers", logging.NewKV("Addresses", addrs)) n.Boostrap(addrs) @@ -121,9 +122,9 @@ func setupDefraNode(t *testing.T, cfg *config.Config, seeds []string) (*node.Nod if err := n.Start(); err != nil { closeErr := n.Close() if closeErr != nil { - return nil, nil, fmt.Errorf("unable to start P2P listeners: %v: problem closing node: %w", err, closeErr) + return nil, nil, errors.Wrap(fmt.Sprintf("unable to start P2P listeners: %v: problem closing node", err), closeErr) } - return nil, nil, fmt.Errorf("unable to start P2P listeners: %w", err) + return nil, nil, errors.Wrap("unable to start P2P listeners", err) } return n, dockeys, nil @@ -293,7 +294,7 @@ func executeTestCase(t *testing.T, test P2PTestCase) { for _, n := range nodes { n.DB.Close(ctx) if err := n.Close(); err != nil { - log.Info(ctx, "node not closing as expected", logging.NewKV("Error", err)) + log.Info(ctx, "node not closing as expected", logging.NewKV("Error", err.Error())) } } } diff --git a/tests/integration/schema/utils.go b/tests/integration/schema/utils.go index c99c484251..81b30f9e54 100644 --- a/tests/integration/schema/utils.go +++ b/tests/integration/schema/utils.go @@ -12,11 +12,11 @@ package schema import ( "context" - "fmt" "strings" "testing" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/errors" testutils "github.com/sourcenetwork/defradb/tests/integration" "github.com/stretchr/testify/assert" ) @@ -158,7 +158,7 @@ func assertError(t *testing.T, err error, expectedError string) bool { return false } else { if !strings.Contains(err.Error(), expectedError) { - assert.ErrorIs(t, err, fmt.Errorf(expectedError)) + assert.ErrorIs(t, err, errors.New(expectedError)) return false } return true @@ -167,18 +167,18 @@ func assertError(t *testing.T, err error, expectedError string) bool { func assertErrors( t *testing.T, - errors []any, + errs []any, expectedError string, ) bool { if expectedError == "" { - assert.Empty(t, errors) + assert.Empty(t, errs) } else { - for _, e := range errors { + for _, e := range errs { // This is always a string at the moment, add support for other types as and when needed errorString := e.(string) if !strings.Contains(errorString, expectedError) { // We use ErrorIs for clearer failures (is a error comparision even if it is just a string) - assert.ErrorIs(t, fmt.Errorf(errorString), fmt.Errorf(expectedError)) + assert.ErrorIs(t, errors.New(errorString), errors.New(expectedError)) continue } return true diff --git a/tests/integration/utils.go b/tests/integration/utils.go index b0b4352ff5..590d57eea0 100644 --- a/tests/integration/utils.go +++ b/tests/integration/utils.go @@ -30,6 +30,7 @@ import ( "github.com/sourcenetwork/defradb/datastore" badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3" "github.com/sourcenetwork/defradb/db" + "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/logging" ) @@ -651,7 +652,7 @@ func assertError(t *testing.T, description string, err error, expectedError stri return false } else { if !strings.Contains(err.Error(), expectedError) { - assert.ErrorIs(t, err, fmt.Errorf(expectedError)) + assert.ErrorIs(t, err, errors.New(expectedError)) return false } return true @@ -663,18 +664,18 @@ func assertError(t *testing.T, description string, err error, expectedError stri func assertErrors( t *testing.T, description string, - errors []any, + errs []any, expectedError string, ) bool { if expectedError == "" { - assert.Empty(t, errors, description) + assert.Empty(t, errs, description) } else { - for _, e := range errors { + for _, e := range errs { // This is always a string at the moment, add support for other types as and when needed errorString := e.(string) if !strings.Contains(errorString, expectedError) { // We use ErrorIs for clearer failures (is a error comparision even if it is just a string) - assert.ErrorIs(t, fmt.Errorf(errorString), fmt.Errorf(expectedError)) + assert.ErrorIs(t, errors.New(errorString), errors.New(expectedError)) continue } return true