forked from cloudwego/kitex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codes.go
67 lines (62 loc) · 2.24 KB
/
codes.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
/*
* Copyright 2021 CloudWeGo Authors
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nphttp2
import (
"errors"
"fmt"
"github.com/pjanthony2001/kitex/pkg/kerrors"
"github.com/pjanthony2001/kitex/pkg/remote"
"github.com/pjanthony2001/kitex/pkg/remote/trans/nphttp2/codes"
"github.com/pjanthony2001/kitex/pkg/remote/trans/nphttp2/status"
)
var kitexErrConvTab = map[error]codes.Code{
kerrors.ErrInternalException: codes.Internal,
kerrors.ErrOverlimit: codes.ResourceExhausted,
kerrors.ErrRemoteOrNetwork: codes.Unavailable,
kerrors.ErrACL: codes.PermissionDenied,
}
// convert error to *status.Status
func convertStatus(err error) *status.Status {
if err == nil {
return status.New(codes.OK, "")
}
// biz error should add biz info which is convenient to be recognized by client side
if errors.Is(err, kerrors.ErrBiz) {
ie := errors.Unwrap(err)
if se, ok := ie.(status.Iface); ok {
gs := se.GRPCStatus()
gs.AppendMessage(fmt.Sprintf("[%s]", kerrors.ErrBiz.Error())).Err()
return gs
}
if te, ok := ie.(*remote.TransError); ok {
return status.New(codes.Code(te.TypeID()), fmt.Sprintf("%s [%s]", ie.Error(), kerrors.ErrBiz.Error()))
}
return status.New(codes.Internal, fmt.Sprintf("%s [%s]", ie.Error(), kerrors.ErrBiz.Error()))
}
// return GRPCStatus() if err is built with status.Error
if se, ok := err.(interface{ GRPCStatus() *status.Status }); ok {
return se.GRPCStatus()
}
// build status.Status with code if error is remote.TransError
if te, ok := err.(*remote.TransError); ok {
return status.New(codes.Code(te.TypeID()), err.Error())
}
// covert from kitex error
if c, ok := kitexErrConvTab[err]; ok {
return status.New(c, err.Error())
}
return status.New(codes.Internal, err.Error())
}