forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtgate_error.go
97 lines (87 loc) · 2.86 KB
/
vtgate_error.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vtgate
import (
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
"github.com/youtube/vitess/go/vt/vterrors"
"github.com/youtube/vitess/go/vt/vtgate/proto"
)
// rpcErrFromTabletError translate an error from VTGate to an *mproto.RPCError
func rpcErrFromVtGateError(err error) *mproto.RPCError {
if err == nil {
return nil
}
// TODO(aaijazi): for now, we don't have any differentiation of VtGate errors.
// However, we should have them soon, so that clients don't have to parse the
// returned error string.
return &mproto.RPCError{
Code: vterrors.UnknownVtgateError,
Message: err.Error(),
}
}
// AddVtGateErrorToQueryResult will mutate a QueryResult struct to fill in the Err
// field with details from the VTGate error.
func AddVtGateErrorToQueryResult(err error, reply *proto.QueryResult) {
if err == nil {
return
}
reply.Err = rpcErrFromVtGateError(err)
}
// AddVtGateErrorToQueryResultList will mutate a QueryResultList struct to fill in the Err
// field with details from the VTGate error.
func AddVtGateErrorToQueryResultList(err error, reply *proto.QueryResultList) {
if err == nil {
return
}
reply.Err = rpcErrFromVtGateError(err)
}
// AddVtGateErrorToSplitQueryResult will mutate a SplitQueryResult struct to fill in the Err
// field with details from the VTGate error.
func AddVtGateErrorToSplitQueryResult(err error, reply *proto.SplitQueryResult) {
if err == nil {
return
}
reply.Err = rpcErrFromVtGateError(err)
}
// AddVtGateErrorToBeginResponse will mutate a BeginResponse struct to fill in the Err
// field with details from the VTGate error.
func AddVtGateErrorToBeginResponse(err error, reply *proto.BeginResponse) {
if err == nil {
return
}
reply.Err = rpcErrFromVtGateError(err)
}
// AddVtGateErrorToCommitResponse will mutate a CommitResponse struct to fill in the Err
// field with details from the VTGate error.
func AddVtGateErrorToCommitResponse(err error, reply *proto.CommitResponse) {
if err == nil {
return
}
reply.Err = rpcErrFromVtGateError(err)
}
// AddVtGateErrorToRollbackResponse will mutate a RollbackResponse struct to fill in the Err
// field with details from the VTGate error.
func AddVtGateErrorToRollbackResponse(err error, reply *proto.RollbackResponse) {
if err == nil {
return
}
reply.Err = rpcErrFromVtGateError(err)
}
// VtGateErrorToVtRPCError converts a vtgate error into a vtrpc error.
func VtGateErrorToVtRPCError(err error, errString string) *vtrpc.RPCError {
if err == nil && errString == "" {
return nil
}
message := ""
if err != nil {
message = err.Error()
} else {
message = errString
}
return &vtrpc.RPCError{
Code: vtrpc.ErrorCodeDeprecated_UnknownVtgateError,
Message: message,
}
}