github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/errors/helper.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package errors
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/pingcap/errors"
    20  )
    21  
    22  // WrapError generates a new error based on given `*errors.Error`, wraps the err
    23  // as cause error.
    24  // If given `err` is nil, returns a nil error, which a the different behavior
    25  // against `Wrap` function in pingcap/errors.
    26  func WrapError(rfcError *errors.Error, err error) error {
    27  	if err == nil {
    28  		return nil
    29  	}
    30  	return rfcError.Wrap(err).GenWithStackByCause()
    31  }
    32  
    33  // ChangefeedFastFailError checks the error, returns true if it is meaningless
    34  // to retry on this error
    35  func ChangefeedFastFailError(err error) bool {
    36  	return ErrStartTsBeforeGC.Equal(errors.Cause(err)) || ErrSnapshotLostByGC.Equal(errors.Cause(err))
    37  }
    38  
    39  // ChangefeedFastFailErrorCode checks the error, returns true if it is meaningless
    40  // to retry on this error
    41  func ChangefeedFastFailErrorCode(errCode errors.RFCErrorCode) bool {
    42  	switch errCode {
    43  	case ErrStartTsBeforeGC.RFCCode(), ErrSnapshotLostByGC.RFCCode():
    44  		return true
    45  	default:
    46  		return false
    47  	}
    48  }
    49  
    50  // RFCCode returns a RFCCode from an error
    51  func RFCCode(err error) (errors.RFCErrorCode, bool) {
    52  	type rfcCoder interface {
    53  		RFCCode() errors.RFCErrorCode
    54  	}
    55  	if terr, ok := err.(rfcCoder); ok {
    56  		return terr.RFCCode(), true
    57  	}
    58  	err = errors.Cause(err)
    59  	if terr, ok := err.(rfcCoder); ok {
    60  		return terr.RFCCode(), true
    61  	}
    62  	return "", false
    63  }
    64  
    65  // IsRetryableError check the error is safe or worth to retry
    66  func IsRetryableError(err error) bool {
    67  	if err == nil {
    68  		return false
    69  	}
    70  
    71  	switch errors.Cause(err) {
    72  	case context.Canceled, context.DeadlineExceeded:
    73  		return false
    74  	}
    75  	return true
    76  }