forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_error.go
45 lines (40 loc) · 1.05 KB
/
grpc_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
package tabletconn
import (
"fmt"
"strings"
"github.com/youtube/vitess/go/vt/vterrors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
// TabletErrorFromGRPC returns a ServerError or a
// OperationalError from the gRPC error.
func TabletErrorFromGRPC(err error) error {
if err == nil {
return nil
}
// TODO(aaijazi): Unfortunately, there's no better way to check for a gRPC server
// error (vs a client error).
// See: https://github.com/grpc/grpc-go/issues/319
if !strings.Contains(err.Error(), vterrors.GRPCServerErrPrefix) {
return OperationalError(fmt.Sprintf("vttablet: %v", err))
}
// server side error, convert it
var code int
switch grpc.Code(err) {
case codes.Internal:
code = ERR_FATAL
case codes.FailedPrecondition:
code = ERR_RETRY
case codes.ResourceExhausted:
code = ERR_TX_POOL_FULL
case codes.Aborted:
code = ERR_NOT_IN_TX
default:
code = ERR_NORMAL
}
return &ServerError{
Code: code,
Err: fmt.Sprintf("vttablet: %v", err),
ServerCode: vterrors.GRPCCodeToErrorCode(grpc.Code(err)),
}
}