Skip to content

Commit

Permalink
fix uncessary context canceled err (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingweicb authored Feb 1, 2023
1 parent a82130d commit 294474a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 55 deletions.
12 changes: 9 additions & 3 deletions fetcher/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"log"
"strings"

"github.com/fatih/color"

Expand Down Expand Up @@ -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),
Expand Down
50 changes: 31 additions & 19 deletions statefulsyncer/stateful_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
104 changes: 71 additions & 33 deletions syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 294474a

Please sign in to comment.