github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/errors/reflect_test.go (about)

     1  // Copyright 2022 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  	"testing"
    18  
    19  	"github.com/pingcap/errors"
    20  	"github.com/pingcap/tiflow/dm/pkg/terror"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestToDMError(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	var (
    28  		minCode = 10000
    29  		maxCode = 50000
    30  	)
    31  
    32  	for code := minCode; code < maxCode; code++ {
    33  		// DM standard error
    34  		baseError, ok := terror.ErrorFromCode(terror.ErrCode(code))
    35  		if ok {
    36  			err := baseError.Generate()
    37  			reflectErr := ToDMError(err)
    38  			require.Error(t, reflectErr)
    39  			require.True(t, err.(*terror.Error).Equal(reflectErr))
    40  		}
    41  	}
    42  
    43  	for code := minCode; code < maxCode; code++ {
    44  		// error from format DM standard error
    45  		baseError, ok := terror.ErrorFromCode(terror.ErrCode(code))
    46  		if ok {
    47  			raw := baseError.Generate()
    48  			err := errors.New(raw.Error())
    49  			reflectErr := ToDMError(err)
    50  			require.Error(t, reflectErr)
    51  			require.True(t, baseError.Equal(reflectErr))
    52  		}
    53  	}
    54  
    55  	// Non DM error
    56  	err := errors.New("normal error")
    57  	require.Equal(t, err, ToDMError(err))
    58  }