-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathgrpc_err_redaction.go
46 lines (40 loc) · 1.24 KB
/
grpc_err_redaction.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
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
//
package grpcutil
import (
"github.com/cockroachdb/errors"
"github.com/cockroachdb/errors/errbase"
"github.com/cockroachdb/redact"
"github.com/gogo/status"
grpcStatus "google.golang.org/grpc/status"
)
func grpcSpecialCasePrintFn(err error, p errors.Printer, isLeaf bool) (handled bool, next error) {
// If GRPCStatus() returns nil, status.FromError will still
// return true, but the retuned error will have no message and
// a code of OK.
if se := (interface{ GRPCStatus() *grpcStatus.Status })(nil); errors.As(err, &se) {
if se.GRPCStatus() == nil {
return false, nil
}
}
s, ok := status.FromError(err)
if !ok {
return false, nil
}
p.Printf("grpc: %s [code %[2]d/%[2]s]", s.Message(), redact.Safe(s.Code()))
for _, d := range s.Details() {
p.Printf(", %s", d)
}
return true, nil
}
func init() {
errbase.RegisterSpecialCasePrinter(grpcSpecialCasePrintFn)
}