Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 52 additions & 43 deletions compatible_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,72 +25,81 @@ import (
// json to errors.Error, using this map to convert error class to RFC
// error code text. here is reference:
// https://github.com/pingcap/parser/blob/release-3.0/terror/terror.go#L58
var class2RFCCode = map[int]string{
1: "autoid",
2: "ddl",
3: "domain",
4: "evaluator",
5: "executor",
6: "expression",
7: "admin",
8: "kv",
9: "meta",
10: "planner",
11: "parser",
12: "perfschema",
13: "privilege",
14: "schema",
15: "server",
16: "struct",
17: "variable",
18: "xeval",
19: "table",
20: "types",
21: "global",
22: "mocktikv",
23: "json",
24: "tikv",
25: "session",
26: "plugin",
27: "util",
var class2RFCCode = []string{
"", // 0 (reserved)
"autoid", // 1
"ddl", // 2
"domain", // 3
"evaluator", // 4
"executor", // 5
"expression", // 6
"admin", // 7
"kv", // 8
"meta", // 9
"planner", // 10
"parser", // 11
"perfschema", // 12
"privilege", // 13
"schema", // 14
"server", // 15
"struct", // 16
"variable", // 17
"xeval", // 18
"table", // 19
"types", // 20
"global", // 21
"mocktikv", // 22
"json", // 23
"tikv", // 24
"session", // 25
"plugin", // 26
"util", // 27
}
var rfcCode2class map[string]int

func init() {
rfcCode2class = make(map[string]int)
for k, v := range class2RFCCode {
rfcCode2class[v] = k
if k > 0 {
rfcCode2class[v] = k
}
}
}

// MarshalJSON implements json.Marshaler interface.
// aware that this function cannot save a 'registered' status,
// since we cannot access the registry when unmarshaling,
// and the original global registry would be removed here.
// This function is reserved for compatibility.
// MarshalJSON implements the json.Marshaler interface.
//
// Note that this method cannot preserve a "registered" status, because the
// registry is inaccessible when unmarshaling and the original global registry
// has been removed. This method is kept for compatibility.
func (e *Error) MarshalJSON() ([]byte, error) {
ec := strings.Split(string(e.codeText), ":")[0]
ec, _, _ := strings.Cut(string(e.codeText), ":")
class, _ := rfcCode2class[ec]
return json.Marshal(&jsonError{
Class: rfcCode2class[ec],
Class: class,
Code: int(e.code),
Msg: e.GetMsg(),
RFCCode: string(e.codeText),
})
}

// UnmarshalJSON implements json.Unmarshaler interface.
// aware that this function cannot create a 'registered' error,
// since we cannot access the registry in this context,
// and the original global registry is removed.
// This function is reserved for compatibility.
// UnmarshalJSON implements the json.Unmarshaler interface.
//
// It decodes into a fresh Error, all pre-existing fields will be reset.
// Use a locally defined prototype when generating an error with new arguments.
//
// Note that this method cannot create a "registered" error, because the
// registry is inaccessible in this context and the original global registry
// has been removed. This method is kept for compatibility.
func (e *Error) UnmarshalJSON(data []byte) error {
var tErr jsonError
if err := json.Unmarshal(data, &tErr); err != nil {
return Trace(err)
}
e.codeText = ErrCodeText(tErr.RFCCode)
if tErr.RFCCode == "" && tErr.Class > 0 {
e.codeText = ErrCodeText(class2RFCCode[tErr.Class] + ":" + strconv.Itoa(tErr.Code))
if tErr.RFCCode == "" {
if tErr.Class > 0 && tErr.Class < len(class2RFCCode) {
e.codeText = ErrCodeText(class2RFCCode[tErr.Class] + ":" + strconv.Itoa(tErr.Code))
Comment thread
kennytm marked this conversation as resolved.
Resolved
}
}
e.code = ErrCode(tErr.Code)
e.message = tErr.Msg
Expand Down
27 changes: 27 additions & 0 deletions compatible_shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@ package errors

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestUnmarshalJSONLegacyClass(t *testing.T) {
const code = 123
const message = "legacy error"
for _, tt := range []struct {
class int
rfcCode ErrCodeText
}{
{class: -1},
{class: 0},
{class: 1, rfcCode: "autoid:123"},
{class: 27, rfcCode: "util:123"},
{class: 28},
} {
t.Run(fmt.Sprintf("class=%d", tt.class), func(t *testing.T) {
data := []byte(fmt.Sprintf(`{"class":%d,"code":%d,"message":%q}`, tt.class, code, message))
var e Error
require.NotPanics(t, func() {
require.NoError(t, json.Unmarshal(data, &e))
})
require.Equal(t, tt.rfcCode, e.codeText)
require.Equal(t, ErrCode(code), e.Code())
require.Equal(t, message, e.GetMsg())
})
}
}

func TestUnmarshalJSONClearsLocalState(t *testing.T) {
e := Error{
message: "old %s",
Expand Down