github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/terror/adapter_test.go (about) 1 // Copyright 2019 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 terror 15 16 import ( 17 "database/sql/driver" 18 "fmt" 19 "testing" 20 21 "github.com/go-sql-driver/mysql" 22 "github.com/pingcap/errors" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestDBAdapter(t *testing.T) { 27 t.Parallel() 28 29 defaultErr := ErrDBDriverError 30 testCases := []struct { 31 err error 32 expect *Error 33 }{ 34 {nil, nil}, 35 {ErrDBBadConn, ErrDBBadConn}, 36 {driver.ErrBadConn, ErrDBBadConn}, 37 {errors.Annotate(driver.ErrBadConn, "annotate error"), ErrDBBadConn}, 38 {mysql.ErrInvalidConn, ErrDBInvalidConn}, 39 {mysql.ErrUnknownPlugin, defaultErr}, 40 } 41 42 for _, tc := range testCases { 43 err := DBErrorAdapt(tc.err, ScopeNotSet, defaultErr) 44 if tc.expect == nil { 45 require.NoError(t, err) 46 } else { 47 require.True(t, tc.expect.Equal(err)) 48 if err != tc.err { 49 exp := tc.expect 50 obj, ok := err.(*Error) 51 require.True(t, ok) 52 require.Equal(t, tc.expect.message, obj.getMsg()) 53 require.Equal(t, fmt.Sprintf(errBaseFormat+", Message: %s, RawCause: %s, Workaround: %s", exp.code, exp.class, exp.scope, exp.level, exp.message, tc.err.Error(), exp.workaround), obj.Error()) 54 } 55 } 56 } 57 }