forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterror_test.go
163 lines (136 loc) · 4.61 KB
/
terror_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package terror
import (
"encoding/json"
"strings"
"testing"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/util/testleak"
)
func TestT(t *testing.T) {
CustomVerboseFlag = true
TestingT(t)
}
var _ = Suite(&testTErrorSuite{})
type testTErrorSuite struct {
}
func (s *testTErrorSuite) TestErrCode(c *C) {
c.Assert(CodeMissConnectionID, Equals, ErrCode(1))
c.Assert(CodeResultUndetermined, Equals, ErrCode(2))
}
func (s *testTErrorSuite) TestTError(c *C) {
defer testleak.AfterTest(c)()
c.Assert(ClassParser.String(), Not(Equals), "")
c.Assert(ClassOptimizer.String(), Not(Equals), "")
c.Assert(ClassKV.String(), Not(Equals), "")
c.Assert(ClassServer.String(), Not(Equals), "")
parserErr := ClassParser.New(ErrCode(1), "error 1")
c.Assert(parserErr.Error(), Not(Equals), "")
c.Assert(ClassParser.EqualClass(parserErr), IsTrue)
c.Assert(ClassParser.NotEqualClass(parserErr), IsFalse)
c.Assert(ClassOptimizer.EqualClass(parserErr), IsFalse)
optimizerErr := ClassOptimizer.New(ErrCode(2), "abc")
c.Assert(ClassOptimizer.EqualClass(errors.New("abc")), IsFalse)
c.Assert(ClassOptimizer.EqualClass(nil), IsFalse)
c.Assert(optimizerErr.Equal(optimizerErr.GenWithStack("def")), IsTrue)
c.Assert(optimizerErr.Equal(nil), IsFalse)
c.Assert(optimizerErr.Equal(errors.New("abc")), IsFalse)
// Test case for FastGen.
c.Assert(optimizerErr.Equal(optimizerErr.FastGen("def")), IsTrue)
c.Assert(optimizerErr.Equal(optimizerErr.FastGen("def: %s", "def")), IsTrue)
kvErr := ClassKV.New(1062, "key already exist")
e := kvErr.FastGen("Duplicate entry '%d' for key 'PRIMARY'", 1)
c.Assert(e.Error(), Equals, "[kv:1062]Duplicate entry '1' for key 'PRIMARY'")
kvMySQLErrCodes := map[ErrCode]uint16{
1062: uint16(1062),
}
ErrClassToMySQLCodes[ClassKV] = kvMySQLErrCodes
sqlErr := errors.Cause(e).(*Error).ToSQLError()
c.Assert(sqlErr.Message, Equals, "Duplicate entry '1' for key 'PRIMARY'")
c.Assert(sqlErr.Code, Equals, uint16(1062))
err := errors.Trace(ErrCritical.GenWithStackByArgs("test"))
c.Assert(ErrCritical.Equal(err), IsTrue)
err = errors.Trace(ErrCritical)
c.Assert(ErrCritical.Equal(err), IsTrue)
}
func (s *testTErrorSuite) TestJson(c *C) {
prevTErr := &Error{
class: ClassTable,
code: CodeExecResultIsEmpty,
message: "json test",
}
buf, err := json.Marshal(prevTErr)
c.Assert(err, IsNil)
var curTErr Error
err = json.Unmarshal(buf, &curTErr)
c.Assert(err, IsNil)
isEqual := prevTErr.Equal(&curTErr)
c.Assert(isEqual, IsTrue)
}
var predefinedErr = ClassExecutor.New(ErrCode(123), "predefiend error")
func example() error {
err := call()
return errors.Trace(err)
}
func call() error {
return predefinedErr.GenWithStack("error message:%s", "abc")
}
func (s *testTErrorSuite) TestTraceAndLocation(c *C) {
defer testleak.AfterTest(c)()
err := example()
stack := errors.ErrorStack(err)
lines := strings.Split(stack, "\n")
c.Assert(len(lines), Equals, 23)
var containTerr bool
for _, v := range lines {
if strings.Contains(v, "terror_test.go") {
containTerr = true
break
}
}
c.Assert(containTerr, IsTrue)
}
func (s *testTErrorSuite) TestErrorEqual(c *C) {
defer testleak.AfterTest(c)()
e1 := errors.New("test error")
c.Assert(e1, NotNil)
e2 := errors.Trace(e1)
c.Assert(e2, NotNil)
e3 := errors.Trace(e2)
c.Assert(e3, NotNil)
c.Assert(errors.Cause(e2), Equals, e1)
c.Assert(errors.Cause(e3), Equals, e1)
c.Assert(errors.Cause(e2), Equals, errors.Cause(e3))
e4 := errors.New("test error")
c.Assert(errors.Cause(e4), Not(Equals), e1)
e5 := errors.Errorf("test error")
c.Assert(errors.Cause(e5), Not(Equals), e1)
c.Assert(ErrorEqual(e1, e2), IsTrue)
c.Assert(ErrorEqual(e1, e3), IsTrue)
c.Assert(ErrorEqual(e1, e4), IsTrue)
c.Assert(ErrorEqual(e1, e5), IsTrue)
var e6 error
c.Assert(ErrorEqual(nil, nil), IsTrue)
c.Assert(ErrorNotEqual(e1, e6), IsTrue)
code1 := ErrCode(1)
code2 := ErrCode(2)
te1 := ClassParser.New(code1, "abc")
te2 := ClassParser.New(code1, "def")
te3 := ClassKV.New(code1, "abc")
te4 := ClassKV.New(code2, "abc")
c.Assert(ErrorEqual(te1, te2), IsTrue)
c.Assert(ErrorEqual(te1, te3), IsFalse)
c.Assert(ErrorEqual(te3, te4), IsFalse)
}