Skip to content

Commit a2da69f

Browse files
authored
Merge pull request #349 from GeekArthur/errorHandling
Error handling improvement for configuration and results packages
2 parents 77bdf8b + 143959f commit a2da69f

File tree

11 files changed

+88
-119
lines changed

11 files changed

+88
-119
lines changed

configuration/configuration.go

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ package configuration
1717
import (
1818
"context"
1919
"encoding/hex"
20-
"errors"
2120
"fmt"
2221
"log"
2322
"path"
2423
"runtime"
2524
"strings"
2625

27-
customerrors "github.com/coinbase/rosetta-cli/pkg/errors"
26+
cliErrs "github.com/coinbase/rosetta-cli/pkg/errors"
2827
"github.com/coinbase/rosetta-sdk-go/asserter"
2928
"github.com/coinbase/rosetta-sdk-go/constructor/dsl"
3029
"github.com/coinbase/rosetta-sdk-go/constructor/job"
@@ -213,19 +212,19 @@ func assertConstructionConfiguration(ctx context.Context, config *ConstructionCo
213212
}
214213

215214
if len(config.Workflows) > 0 && len(config.ConstructorDSLFile) > 0 {
216-
return fmt.Errorf("%w: cannot populate both workflows and DSL file path", customerrors.ErrParseFileFailed)
215+
return cliErrs.ErrMultipleDSLFiles
217216
}
218217

219218
if len(config.Workflows) == 0 && len(config.ConstructorDSLFile) == 0 {
220-
return fmt.Errorf("%w: both workflows and DSL file path are empty", customerrors.ErrParseFileFailed)
219+
return cliErrs.ErrNoDSLFile
221220
}
222221

223222
// Compile ConstructorDSLFile and save to Workflows
224223
if len(config.ConstructorDSLFile) > 0 {
225224
compiledWorkflows, err := dsl.Parse(ctx, config.ConstructorDSLFile)
226225
if err != nil {
227226
err.Log()
228-
return fmt.Errorf("%w: compilation failed", err.Err)
227+
return fmt.Errorf("DSL file is invalid, line %d, line contents %s: %w", err.Line, err.LineContents, err.Err)
229228
}
230229

231230
config.Workflows = compiledWorkflows
@@ -236,10 +235,10 @@ func assertConstructionConfiguration(ctx context.Context, config *ConstructionCo
236235
if workflow.Name == string(job.CreateAccount) || workflow.Name == string(job.RequestFunds) {
237236
if workflow.Concurrency != job.ReservedWorkflowConcurrency {
238237
return fmt.Errorf(
239-
"%w: reserved workflow %s must have concurrency %d",
240-
customerrors.ErrParseWorkflowFailed,
238+
"DSL file is invalid, reserved workflow %s must have concurrency %d: %w",
241239
workflow.Name,
242240
job.ReservedWorkflowConcurrency,
241+
cliErrs.ErrWrongWorkflowConcurrency,
243242
)
244243
}
245244
}
@@ -250,20 +249,20 @@ func assertConstructionConfiguration(ctx context.Context, config *ConstructionCo
250249
_, err := hex.DecodeString(account.PrivateKeyHex)
251250
if err != nil {
252251
return fmt.Errorf(
253-
"%w: private key %s is not hex encoded for prefunded account",
254-
err,
252+
"private key %s is not hex encoded for prefunded account: %w",
255253
account.PrivateKeyHex,
254+
err,
256255
)
257256
}
258257

259258
// Checks if valid CurveType
260259
if err := asserter.CurveType(account.CurveType); err != nil {
261-
return fmt.Errorf("%w: invalid CurveType for prefunded account", err)
260+
return fmt.Errorf("prefunded account curve type %s is invalid: %w", types.PrintStruct(account.CurveType), err)
262261
}
263262

264263
// Checks if valid AccountIdentifier
265264
if err := asserter.AccountIdentifier(account.AccountIdentifier); err != nil {
266-
return fmt.Errorf("Account.Address is missing for prefunded account")
265+
return fmt.Errorf("prefunded account identifier %s is invalid: %w", types.PrintStruct(account.AccountIdentifier), err)
267266
}
268267

269268
// Check if valid Currency when Currency is specified
@@ -272,7 +271,7 @@ func assertConstructionConfiguration(ctx context.Context, config *ConstructionCo
272271
if account.Currency != nil {
273272
err = asserter.Currency(account.Currency)
274273
if err != nil {
275-
return fmt.Errorf("%w: invalid currency for prefunded account", err)
274+
return fmt.Errorf("prefunded account currency %s is invalid: %w", types.PrintStruct(account.Currency), err)
276275
}
277276
}
278277
}
@@ -282,11 +281,11 @@ func assertConstructionConfiguration(ctx context.Context, config *ConstructionCo
282281

283282
func assertDataConfiguration(config *DataConfiguration) error { // nolint:gocognit
284283
if config.StartIndex != nil && *config.StartIndex < 0 {
285-
return fmt.Errorf("start index %d cannot be negative", *config.StartIndex)
284+
return fmt.Errorf("start index %d is invalid: %w", *config.StartIndex, cliErrs.ErrNegativeStartIndex)
286285
}
287286

288287
if !config.ReconciliationDisabled && config.BalanceTrackingDisabled {
289-
return errors.New("balance tracking must be enabled to perform reconciliation")
288+
return cliErrs.ErrBalanceTrackingIsDisabledForReconciliation
290289
}
291290

292291
if config.EndConditions == nil {
@@ -295,45 +294,40 @@ func assertDataConfiguration(config *DataConfiguration) error { // nolint:gocogn
295294

296295
if config.EndConditions.Index != nil {
297296
if *config.EndConditions.Index < 0 {
298-
return fmt.Errorf("end index %d cannot be negative", *config.EndConditions.Index)
297+
return fmt.Errorf("end index %d is invalid: %w", *config.EndConditions.Index, cliErrs.ErrNegativeEndIndex)
299298
}
300299
}
301300

302301
if config.EndConditions.ReconciliationCoverage != nil {
303302
coverage := config.EndConditions.ReconciliationCoverage.Coverage
304303
if coverage < 0 || coverage > 1 {
305-
return fmt.Errorf("reconciliation coverage %f must be [0.0,1.0]", coverage)
304+
return fmt.Errorf("reconciliation coverage %f is invalid: %w", coverage, cliErrs.ErrReconciliationOutOfRange)
306305
}
307306

308307
index := config.EndConditions.ReconciliationCoverage.Index
309308
if index != nil && *index < 0 {
310-
return fmt.Errorf("reconciliation coverage height %d must be >= 0", *index)
309+
return fmt.Errorf("reconciliation coverage index %d is invalid: %w", *index, cliErrs.ErrNegativeReconciliationCoverageIndex)
311310
}
312311

313312
accountCount := config.EndConditions.ReconciliationCoverage.AccountCount
314313
if accountCount != nil && *accountCount < 0 {
315314
return fmt.Errorf(
316-
"reconciliation coverage account count %d must be >= 0",
315+
"reconciliation coverage account count %d is invalid: %w",
317316
*accountCount,
317+
cliErrs.ErrNegativeReconciliationCoverageAccountCount,
318318
)
319319
}
320320

321321
if config.BalanceTrackingDisabled {
322-
return errors.New(
323-
"balance tracking must be enabled for reconciliation coverage end condition",
324-
)
322+
return cliErrs.ErrBalanceTrackingIsDisabledForReconciliationCoverageEndCondition
325323
}
326324

327325
if config.IgnoreReconciliationError {
328-
return errors.New(
329-
"reconciliation errors cannot be ignored for reconciliation coverage end condition",
330-
)
326+
return cliErrs.ErrReconciliationErrorIsIgnoredForReconciliationCoverageEndCondition
331327
}
332328

333329
if config.ReconciliationDisabled {
334-
return errors.New(
335-
"reconciliation cannot be disabled for reconciliation coverage end condition",
336-
)
330+
return cliErrs.ErrReconciliationIsDisabledForReconciliationCoverageEndCondition
337331
}
338332
}
339333

@@ -342,31 +336,31 @@ func assertDataConfiguration(config *DataConfiguration) error { // nolint:gocogn
342336

343337
func assertConfiguration(ctx context.Context, config *Configuration) error {
344338
if err := asserter.NetworkIdentifier(config.Network); err != nil {
345-
return fmt.Errorf("%w: invalid network identifier", err)
339+
return fmt.Errorf("invalid network identifier %s: %w", types.PrintStruct(config.Network), err)
346340
}
347341

348342
if config.SeenBlockWorkers <= 0 {
349-
return errors.New("seen_block_workers must be > 0")
343+
return fmt.Errorf("the number of seen block workers %d is invalid: %w", config.SeenBlockWorkers, cliErrs.ErrNegativeSeenBlockWorkers)
350344
}
351345

352346
if config.SerialBlockWorkers <= 0 {
353-
return errors.New("serial_block_workers must be > 0")
347+
return fmt.Errorf("the number of serial block workers %d is invalid: %w", config.SerialBlockWorkers, cliErrs.ErrNegativeSerialBlockWorkers)
354348
}
355349

356350
if config.TableSize != nil && (*config.TableSize < 2 || *config.TableSize > 100) {
357-
return fmt.Errorf("table_size %d is not in the range [2, 100], please check your input", *config.TableSize)
351+
return fmt.Errorf("table size %d is invalid: %w", *config.TableSize, cliErrs.ErrTableSizeIsOutOfRange)
358352
}
359353

360354
if config.ValueLogFileSize != nil && (*config.ValueLogFileSize < 128 || *config.ValueLogFileSize > 2048) {
361-
return fmt.Errorf("value_log_file_size %d is not in the range [128, 2048], please check your input", *config.ValueLogFileSize)
355+
return fmt.Errorf("value log file size %d is invalid: %w", *config.ValueLogFileSize, cliErrs.ErrValueLogFileSizeIsOutOfRange)
362356
}
363357

364358
if err := assertDataConfiguration(config.Data); err != nil {
365-
return fmt.Errorf("%w: invalid data configuration", err)
359+
return fmt.Errorf("data configuration is invalid: %w", err)
366360
}
367361

368362
if err := assertConstructionConfiguration(ctx, config.Construction); err != nil {
369-
return fmt.Errorf("%w: invalid construction configuration", err)
363+
return fmt.Errorf("construction configuration is invalid: %w", err)
370364
}
371365

372366
return nil
@@ -410,7 +404,7 @@ func modifyFilePaths(config *Configuration, fileDir string) {
410404
func LoadConfiguration(ctx context.Context, filePath string) (*Configuration, error) {
411405
var configRaw Configuration
412406
if err := utils.LoadAndParse(filePath, &configRaw); err != nil {
413-
return nil, fmt.Errorf("%w: unable to open configuration file", err)
407+
return nil, fmt.Errorf("unable to load and parse configuration file: %w", err)
414408
}
415409

416410
config := populateMissingFields(&configRaw)
@@ -421,7 +415,7 @@ func LoadConfiguration(ctx context.Context, filePath string) (*Configuration, er
421415
modifyFilePaths(config, fileDir)
422416

423417
if err := assertConfiguration(ctx, config); err != nil {
424-
return nil, fmt.Errorf("%w: invalid configuration", err)
418+
return nil, fmt.Errorf("configuration is invalid: %w", err)
425419
}
426420

427421
color.Cyan(

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ module github.com/coinbase/rosetta-cli
33
go 1.16
44

55
require (
6-
github.com/coinbase/rosetta-sdk-go v0.7.11
6+
github.com/coinbase/rosetta-sdk-go v0.8.0
77
github.com/fatih/color v1.13.0
8-
github.com/golang/protobuf v1.5.2 // indirect
98
github.com/google/go-cmp v0.5.6 // indirect
109
github.com/mattn/go-colorable v0.1.12 // indirect
1110
github.com/olekukonko/tablewriter v0.0.5
@@ -14,6 +13,5 @@ require (
1413
github.com/stretchr/testify v1.7.2
1514
go.uber.org/zap v1.21.0
1615
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
17-
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
1816
google.golang.org/protobuf v1.27.1 // indirect
1917
)

0 commit comments

Comments
 (0)