-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
45 lines (39 loc) · 941 Bytes
/
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 node
import (
"fmt"
"strings"
typespb "github.com/elysiumstation/fury/protos/fury"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type ErrorCode codes.Code
type StatusError struct {
Code codes.Code
Details []string
}
func (e *StatusError) Error() string {
if len(e.Details) == 0 {
return e.Code.String()
}
return fmt.Sprintf("%s: %v", e.Code.String(), strings.Join(e.Details, ", "))
}
// intoStatusError extracts useful information from a gRPC status error.
// It returns nil if the underlying error is not a gRPC status error.
func intoStatusError(err error) *StatusError {
st, ok := status.FromError(err)
if !ok {
return nil
}
statusErr := &StatusError{
Code: st.Code(),
Details: []string{},
}
for _, v := range st.Details() {
v, ok := v.(*typespb.ErrorDetail)
if !ok {
continue
}
statusErr.Details = append(statusErr.Details, v.GetMessage())
}
return statusErr
}