-
Notifications
You must be signed in to change notification settings - Fork 109
/
errors.go
140 lines (121 loc) · 3.15 KB
/
errors.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
package grpc
import (
"context"
any "github.com/golang/protobuf/ptypes/any"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/errors"
)
// IsErrorCode returns true if the given error represents a specific gRPC error code.
func IsErrorCode(err error, code codes.Code) bool {
status := GetErrorStatus(err)
if status == nil {
return false
}
return status.Code() == code
}
// GetErrorStatus returns gRPC status from error.
func GetErrorStatus(err error) *status.Status {
var grpcError interface {
error
GRPCStatus() *status.Status
}
if !errors.As(err, &grpcError) {
return nil
}
return grpcError.GRPCStatus()
}
// grpcError is a serializable error.
type grpcError struct {
Module string `json:"module,omitempty"`
Code uint32 `json:"code,omitempty"`
}
func errorToGrpc(err error) error {
if err == nil {
return nil
}
// Convert the error.
module, code := errors.Code(err)
if module == errors.UnknownModule {
// If the error is not known, just pass the original error.
return err
}
// NOTE: Although this is protobuf, the message is actually serialized using
// our provided CBOR codec when configured. We need to use this directly
// in order to be able to set the Details field.
return status.FromProto(&spb.Status{
// We keep any set gRPC error code (with fallback to codes.Unknown).
Code: int32(status.Code(err)),
Message: err.Error(),
Details: []*any.Any{
{
// Double serialization seems ugly, but there is no way around
// it as the format for errors is predefined.
Value: cbor.Marshal(&grpcError{Module: module, Code: code}),
},
},
}).Err()
}
func errorFromGrpc(err error) error {
if err == nil {
return nil
}
// Convert the error back.
if s, ok := status.FromError(err); ok {
sp := s.Proto()
if len(sp.Details) != 1 {
return err
}
var ge grpcError
if cerr := cbor.Unmarshal(sp.Details[0].Value, &ge); cerr != nil {
return err
}
if mappedErr := errors.FromCode(ge.Module, ge.Code, sp.Message); mappedErr != nil {
return mappedErr
}
}
return err
}
func serverUnaryErrorMapper(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
rsp, err := handler(ctx, req)
return rsp, errorToGrpc(err)
}
func serverStreamErrorMapper(
srv interface{},
ss grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
err := handler(srv, ss)
return errorToGrpc(err)
}
func clientUnaryErrorMapper(
ctx context.Context,
method string,
req, rsp interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
err := invoker(ctx, method, req, rsp, cc, opts...)
return errorFromGrpc(err)
}
func clientStreamErrorMapper(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
cs, err := streamer(ctx, desc, cc, method, opts...)
return cs, errorFromGrpc(err)
}