-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrpc.go
66 lines (62 loc) · 1.57 KB
/
grpc.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
package ez
import (
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// NewFromGRPC wraps a GRPC Error into a standar application error
func NewFromGRPC(op string, err error) *Error {
status := status.Convert(err)
code := status.Code()
return New(op, GRPCCodeToError(code), status.Message(), err)
}
// GRPCCodeToError converts a GRPC error code to a standar application error code
func GRPCCodeToError(c codes.Code) string {
switch c {
case codes.FailedPrecondition:
return ECONFLICT
case codes.Internal:
return EINTERNAL
case codes.InvalidArgument:
return EINVALID
case codes.NotFound:
return ENOTFOUND
case codes.PermissionDenied:
return ENOTAUTHORIZED
case codes.Unauthenticated:
return ENOTAUTHENTICATED
case codes.ResourceExhausted:
return ERESOURCEEXHAUSTED
case codes.Unimplemented:
return ENOTIMPLEMENTED
case codes.Unavailable:
return EUNAVAILABLE
default:
return EINTERNAL
}
}
// ErrorToGRPCCode converts an standar application error code to a GRPC error code
func ErrorToGRPCCode(err error) codes.Code {
code := ErrorCode(err)
switch code {
case ECONFLICT:
return codes.FailedPrecondition
case EINTERNAL:
return codes.Internal
case EINVALID:
return codes.InvalidArgument
case ENOTFOUND:
return codes.NotFound
case ENOTAUTHORIZED:
return codes.PermissionDenied
case ENOTAUTHENTICATED:
return codes.Unauthenticated
case ERESOURCEEXHAUSTED:
return codes.ResourceExhausted
case ENOTIMPLEMENTED:
return codes.Unimplemented
case EUNAVAILABLE:
return codes.Unavailable
default:
return codes.Internal
}
}