diff --git a/fetcher/errors.go b/fetcher/errors.go index eb88a4c0..9b841d2a 100644 --- a/fetcher/errors.go +++ b/fetcher/errors.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" "log" + "strings" "github.com/fatih/color" @@ -62,11 +63,16 @@ func (f *Fetcher) RequestFailedError( log.Println(msg) } } + // if the err is context.Canceled, do not print it + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if !strings.Contains(err.Error(), context.Canceled.Error()) { + errForPrint := fmt.Errorf("%s %s: %w%s", message, err.Error(), ErrRequestFailed, f.metaData) + color.Red(errForPrint.Error()) + } - errForPrint := fmt.Errorf("%s %s: %w%s", message, err.Error(), ErrRequestFailed, f.metaData) - color.Red(errForPrint.Error()) return &Error{ - Err: errForPrint, + Err: fmt.Errorf("%s %s: %w", message, err.Error(), ErrRequestFailed), ClientErr: rosettaErr, Retry: ((rosettaErr != nil && rosettaErr.Retriable) || transientError(err) || f.forceRetry) && !errors.Is(err, context.Canceled), diff --git a/statefulsyncer/stateful_syncer.go b/statefulsyncer/stateful_syncer.go index 9a9d2f93..f434e492 100644 --- a/statefulsyncer/stateful_syncer.go +++ b/statefulsyncer/stateful_syncer.go @@ -189,8 +189,12 @@ func (s *StatefulSyncer) Prune(ctx context.Context, helper PruneHelper) error { // as the time between pruning runs. Using a timer would only guarantee // that the difference between starts of each pruning run are s.pruneSleepTime. if err := utils.ContextSleep(ctx, s.pruneSleepTime); err != nil { - err = fmt.Errorf("context is canceled during context sleep: %w%s", err, s.metaData) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf("context is canceled during context sleep: %w%s", err, s.metaData) + color.Red(err.Error()) + } return err } @@ -328,14 +332,18 @@ func (s *StatefulSyncer) NetworkStatus( ) (*types.NetworkStatusResponse, error) { networkStatus, fetchErr := s.fetcher.NetworkStatusRetry(ctx, network, nil) if fetchErr != nil { - errForPrint := fmt.Errorf( - "failed to get network status of %s with retry: %w%s", - network.Network, - fetchErr.Err, - s.metaData, - ) - color.Red(errForPrint.Error()) - return nil, errForPrint + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if fetchErr.Err != context.Canceled { + errForPrint := fmt.Errorf( + "failed to get network status of %s with retry: %w%s", + network.Network, + fetchErr.Err, + s.metaData, + ) + color.Red(errForPrint.Error()) + } + return nil, fetchErr.Err } return networkStatus, nil @@ -349,15 +357,19 @@ func (s *StatefulSyncer) Block( ) (*types.Block, error) { blockResponse, fetchErr := s.fetcher.BlockRetry(ctx, network, block) if fetchErr != nil { - errForPrint := fmt.Errorf( - "unable to fetch block %d from network %s with retry: %w%s", - *block.Index, - network.Network, - fetchErr.Err, - s.metaData, - ) - color.Red(errForPrint.Error()) - return nil, errForPrint + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if fetchErr.Err != context.Canceled { + errForPrint := fmt.Errorf( + "unable to fetch block %d from network %s with retry: %w%s", + *block.Index, + network.Network, + fetchErr.Err, + s.metaData, + ) + color.Red(errForPrint.Error()) + } + return nil, fetchErr.Err } return blockResponse, nil } diff --git a/syncer/syncer.go b/syncer/syncer.go index 6679e5b6..026b847b 100644 --- a/syncer/syncer.go +++ b/syncer/syncer.go @@ -68,13 +68,17 @@ func (s *Syncer) setStart( s.network, ) if err != nil { - err = fmt.Errorf( - "unable to get network status of %s: %w%s", - s.network.Network, - err, - s.metaData, - ) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf( + "unable to get network status of %s: %w%s", + s.network.Network, + err, + s.metaData, + ) + color.Red(err.Error()) + } return err } @@ -107,13 +111,17 @@ func (s *Syncer) nextSyncableRange( s.network, ) if err != nil { - err = fmt.Errorf( - "unable to get network status of %s: %w%s", - s.network.Network, - err, - s.metaData, - ) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf( + "unable to get network status of %s: %w%s", + s.network.Network, + err, + s.metaData, + ) + color.Red(err.Error()) + } return -1, false, err } @@ -301,8 +309,12 @@ func (s *Syncer) fetchBlockResult( case errors.Is(err, ErrOrphanHead): br.orphanHead = true case err != nil: - err = fmt.Errorf("unable to fetch block %d: %w%s", index, err, s.metaData) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf("unable to fetch block %d: %w%s", index, err, s.metaData) + color.Red(err.Error()) + } return nil, err default: br.block = block @@ -350,8 +362,12 @@ func (s *Syncer) fetchBlocks( b, ) if err != nil { - err = fmt.Errorf("unable to fetch block %d: %w%s", b, err, s.metaData) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf("unable to fetch block %d: %w%s", b, err, s.metaData) + color.Red(err.Error()) + } return s.safeExit(err) } @@ -408,13 +424,17 @@ func (s *Syncer) processBlocks( s.nextIndex, ) if err != nil { - err = fmt.Errorf( - "unable to fetch block %d during re-org: %w%s", - s.nextIndex, - err, - s.metaData, - ) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf( + "unable to fetch block %d during re-org: %w%s", + s.nextIndex, + err, + s.metaData, + ) + color.Red(err.Error()) + } return err } } else { @@ -672,8 +692,12 @@ func (s *Syncer) syncRange( } if err := g.Wait(); err != nil { - err = fmt.Errorf("unable to sync to %d: %w%s", endIndex, err, s.metaData) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf("unable to sync to %d: %w%s", endIndex, err, s.metaData) + color.Red(err.Error()) + } return err } @@ -699,9 +723,15 @@ func (s *Syncer) Sync( startIndex int64, endIndex int64, ) error { + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused if err := s.setStart(ctx, startIndex); err != nil { - err = fmt.Errorf("unable to set start index %d: %w%s", startIndex, err, s.metaData) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf("unable to set start index %d: %w%s", startIndex, err, s.metaData) + color.Red(err.Error()) + } return err } @@ -711,8 +741,12 @@ func (s *Syncer) Sync( endIndex, ) if err != nil { - err = fmt.Errorf("unable to get next syncable range: %w%s", err, s.metaData) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf("unable to get next syncable range: %w%s", err, s.metaData) + color.Red(err.Error()) + } return err } @@ -737,8 +771,12 @@ func (s *Syncer) Sync( err = s.syncRange(ctx, rangeEnd) if err != nil { - err = fmt.Errorf("unable to sync to %d: %w%s", rangeEnd, err, s.metaData) - color.Red(err.Error()) + // context.Canceled could because of validation succeed, + // print an error in succeed situation will be confused + if err != context.Canceled { + err = fmt.Errorf("unable to sync to %d: %w%s", rangeEnd, err, s.metaData) + color.Red(err.Error()) + } return err }