mirror of https://github.com/tikv/client-go.git
*: fix panic in get cause error (#1344)
Signed-off-by: crazycs520 <crazycs520@gmail.com>
This commit is contained in:
parent
c40432e3ab
commit
38e0dca30c
|
|
@ -180,6 +180,17 @@ func (r *RequestErrorStats) RecordRPCErrorStats(errLabel string) {
|
|||
}
|
||||
}
|
||||
|
||||
// getErrMsg returns error message. if the error has cause error, then return cause error message.
|
||||
func getErrMsg(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
if causeErr := errors.Cause(err); causeErr != nil {
|
||||
return causeErr.Error()
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
// String implements fmt.Stringer interface.
|
||||
func (r *RegionRequestRuntimeStats) String() string {
|
||||
if r == nil {
|
||||
|
|
@ -2115,7 +2126,7 @@ func (s *RegionRequestSender) sendReqToRegion(
|
|||
if err != nil {
|
||||
s.rpcError = err
|
||||
if s.Stats != nil {
|
||||
errStr := errors.Cause(err).Error()
|
||||
errStr := getErrMsg(err)
|
||||
s.Stats.RecordRPCErrorStats(errStr)
|
||||
s.recordRPCAccessInfo(req, rpcCtx, errStr)
|
||||
}
|
||||
|
|
@ -2211,7 +2222,11 @@ func (s *RegionRequestSender) onSendFail(bo *retry.Backoffer, ctx *RPCContext, r
|
|||
}
|
||||
}
|
||||
}
|
||||
metrics.TiKVRPCErrorCounter.WithLabelValues(errors.Cause(err).Error(), storeLabel).Inc()
|
||||
if errStr := getErrMsg(err); len(errStr) > 0 {
|
||||
metrics.TiKVRPCErrorCounter.WithLabelValues(getErrMsg(err), storeLabel).Inc()
|
||||
} else {
|
||||
metrics.TiKVRPCErrorCounter.WithLabelValues("unknown", storeLabel).Inc()
|
||||
}
|
||||
|
||||
if ctx.Store != nil && ctx.Store.storeType == tikvrpc.TiFlashCompute {
|
||||
s.regionCache.InvalidateTiFlashComputeStoresIfGRPCError(err)
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ import (
|
|||
"github.com/pingcap/kvproto/pkg/mpp"
|
||||
"github.com/pingcap/kvproto/pkg/tikvpb"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/tikv/client-go/v2/config"
|
||||
"github.com/tikv/client-go/v2/config/retry"
|
||||
|
|
@ -872,3 +873,20 @@ func (s *testRegionRequestToSingleStoreSuite) TestRegionRequestStats() {
|
|||
}
|
||||
s.Contains(expecteds, access.String())
|
||||
}
|
||||
|
||||
type noCauseError struct {
|
||||
error
|
||||
}
|
||||
|
||||
func (_ noCauseError) Cause() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestGetErrMsg(t *testing.T) {
|
||||
err := noCauseError{error: errors.New("no cause err")}
|
||||
require.Equal(t, nil, errors.Cause(err))
|
||||
require.Panicsf(t, func() {
|
||||
_ = errors.Cause(err).Error()
|
||||
}, "should panic")
|
||||
require.Equal(t, "no cause err", getErrMsg(err))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue