-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patherrors.go
45 lines (38 loc) · 1.02 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
package dgrpc
import (
"errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// IsGRPCErrorCode is a convenience to reduce code when using [AsGRPCError]:
//
// if err := AsGRPCError(err); err != nil && err.Code() == code {
// return true
// }
//
// return false
func IsGRPCErrorCode(err error, code codes.Code) bool {
if err := AsGRPCError(err); err != nil && err.Code() == code {
return true
}
return false
}
// AsGRPCError recursively finds the first value [Status] representation out of
// this error stack. Refers to [status.FromError] for details how this is tested.
//
// If no such [Status] can be found, nil is returned, expected usage is:
//
// if err := AsGRPCError(err); err != nil && err.Code == codes.Canceled {
// // Do something
// }
func AsGRPCError(err error) *status.Status {
type grpcError interface {
GRPCStatus() *status.Status
}
for next := err; next != nil; next = errors.Unwrap(next) {
if status, ok := next.(grpcError); ok {
return status.GRPCStatus()
}
}
return nil
}