Repository: coinbase/rosetta-cli
Pull request #311 (2022-05-11 19:21:05)

Categorizes errors thrown during check:data
[Fixes # .]https://jira.coinbase-corp.com/browse/ROSE-166

### Motivation
To organize the types of errors that can be thrown during a check:data run

### Open questions
<!--
(optional) Any open questions or feedback on design desired?
-->



Patch:
diff --git a/cmd/check_data.go b/cmd/check_data.go
index 9e6d0e7..eb79d77 100644
--- a/cmd/check_data.go
+++ b/cmd/check_data.go
@@ -17,11 +17,11 @@ package cmd
 import (
 	"context"
 	"fmt"
+	"github.com/coinbase/rosetta-cli/pkg/errors"
 	"time"
 
 	"github.com/coinbase/rosetta-cli/pkg/results"
 	"github.com/coinbase/rosetta-cli/pkg/tester"
-
 	"github.com/coinbase/rosetta-sdk-go/fetcher"
 	"github.com/coinbase/rosetta-sdk-go/utils"
 	"github.com/spf13/cobra"
@@ -131,7 +131,7 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
 		}
 	}
 
-	dataTester := tester.InitializeData(
+	dataTester, err := tester.InitializeData(
 		ctx,
 		Config,
 		Config.Network,
@@ -142,6 +142,9 @@ func runCheckDataCmd(_ *cobra.Command, _ []string) error {
 		&SignalReceived,
 	)
 
+	if err != nil {
+		return fmt.Errorf("%s:%s", errors.ErrInitDataTester, err)
+	}
 	defer dataTester.CloseDatabase(ctx)
 
 	g, ctx := errgroup.WithContext(ctx)
diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go
index cac440e..a5f93b9 100644
--- a/pkg/errors/errors.go
+++ b/pkg/errors/errors.go
@@ -21,6 +21,12 @@ import (
 // Configuration Errors
 
 var (
+	// Data Errors
+
+	ErrCheckStorageTipFailed = errors.New("unable to check storage tip")
+	ErrDataCheckHalt         = errors.New("data check halted")
+	ErrInitDataTester        = errors.New("unexpected error occurred while trying to initialize data tester")
+
 	// Construction Configuration Errors
 
 	ErrParseFileFailed           = errors.New("unable to parse config files")
@@ -30,8 +36,6 @@ var (
 	ErrParseWorkflowFailed       = errors.New("unable to parse workflow")
 	ErrConstructionConfigMissing = errors.New("construction configuration is missing")
 
-	// TODO: Data Configuration Errors
-
 	// Construction check errors
 
 	ErrConstructionCheckHalt = errors.New("construction check halted")


Test patch:
diff --git a/pkg/tester/data.go b/pkg/tester/data.go
index 382c644..e74e9bd 100644
--- a/pkg/tester/data.go
+++ b/pkg/tester/data.go
@@ -19,6 +19,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	customErrs "github.com/coinbase/rosetta-cli/pkg/errors"
 	"log"
 	"math/big"
 	"net/http"
@@ -28,7 +29,6 @@ import (
 	"github.com/coinbase/rosetta-cli/pkg/logger"
 	"github.com/coinbase/rosetta-cli/pkg/processor"
 	"github.com/coinbase/rosetta-cli/pkg/results"
-
 	"github.com/coinbase/rosetta-sdk-go/fetcher"
 	"github.com/coinbase/rosetta-sdk-go/parser"
 	"github.com/coinbase/rosetta-sdk-go/reconciler"
@@ -145,10 +145,10 @@ func InitializeData(
 	genesisBlock *types.BlockIdentifier,
 	interestingAccount *types.AccountCurrency,
 	signalReceived *bool,
-) *DataTester {
+) (*DataTester, error) {
 	dataPath, err := utils.CreateCommandPath(config.DataDirectory, dataCmdName, network)
 	if err != nil {
-		log.Fatalf("%s: cannot create command path", err.Error())
+		return nil, fmt.Errorf("%s: cannot create command path", err.Error())
 	}
 
 	opts := []database.BadgerOption{}
@@ -164,17 +164,17 @@ func InitializeData(
 
 	localStore, err := database.NewBadgerDatabase(ctx, dataPath, opts...)
 	if err != nil {
-		log.Fatalf("%s: unable to initialize database", err.Error())
+		return nil, fmt.Errorf("%s: unable to initialize database", err.Error())
 	}
 
 	exemptAccounts, err := loadAccounts(config.Data.ExemptAccounts)
 	if err != nil {
-		log.Fatalf("%s: unable to load exempt accounts", err.Error())
+		return nil, fmt.Errorf("%s: unable to load exempt accounts", err.Error())
 	}
 
 	interestingAccounts, err := loadAccounts(config.Data.InterestingAccounts)
 	if err != nil {
-		log.Fatalf("%s: unable to load interesting accounts", err.Error())
+		return nil, fmt.Errorf("%s: unable to load interesting accounts", err.Error())
 	}
 
 	counterStorage := modules.NewCounterStorage(localStore)
@@ -191,8 +191,7 @@ func InitializeData(
 		network,
 	)
 	if err != nil {
-		log.Fatalf(fmt.Sprintf("unable to initialize logger with error: %s", err.Error()))
-		return nil
+		return nil, fmt.Errorf("unable to initialize logger with error: %s", err.Error())
 	}
 
 	var forceInactiveReconciliation bool
@@ -216,16 +215,16 @@ func InitializeData(
 	// Get all previously seen accounts
 	seenAccounts, err := balanceStorage.GetAllAccountCurrency(ctx)
 	if err != nil {
-		log.Fatalf("%s: unable to get previously seen accounts", err.Error())
+		return nil, fmt.Errorf("%s: unable to get previously seen accounts", err.Error())
 	}
 
 	networkOptions, fetchErr := fetcher.NetworkOptionsRetry(ctx, network, nil)
-	if err != nil {
+	if fetchErr != nil {
 		log.Fatalf("%s: unable to get network options", fetchErr.Err.Error())
 	}
 
 	if len(networkOptions.Allow.BalanceExemptions) > 0 && config.Data.InitialBalanceFetchDisabled {
-		log.Fatal("found balance exemptions but initial balance fetch disabled")
+		return nil, fmt.Errorf("found balance exemptions but initial balance fetch disabled")
 	}
 
 	parser := parser.New(
@@ -308,10 +307,10 @@ func InitializeData(
 					genesisBlock,
 				)
 				if err != nil {
-					log.Fatalf("%s: unable to bootstrap balances", err.Error())
+					return nil, fmt.Errorf("%s: unable to bootstrap balances", err.Error())
 				}
 			case err != nil:
-				log.Fatalf("%s: unable to get head block identifier", err.Error())
+				return nil, fmt.Errorf("%s: unable to get head block identifier", err.Error())
 			default:
 				log.Println("Skipping balance bootstrapping because already started syncing")
 			}
@@ -368,7 +367,7 @@ func InitializeData(
 		historicalBalanceEnabled:    historicalBalanceEnabled,
 		parser:                      parser,
 		forceInactiveReconciliation: &forceInactiveReconciliation,
-	}
+	}, nil
 }
 
 // StartSyncing syncs from startIndex to endIndex.
@@ -860,7 +859,7 @@ func (t *DataTester) HandleErr(err error, sigListeners *[]context.CancelFunc) er
 			t.config,
 			t.counterStorage,
 			t.balanceStorage,
-			errors.New("check halted"),
+			fmt.Errorf("%w: %v", customErrs.ErrDataCheckHalt, err.Error()),
 			"",
 			"",
 		)