Skip to content

Commit 37afb3c

Browse files
jtolioStorj Robot
authored and
Storj Robot
committed
debug/error: improve monkit naming of drpc errors
also allows for an error to specifically override what err class monkit displays. example usage: https://review.dev.storj.io/c/storj/storj/+/16258 Change-Id: Ib5dc0e12020c3f3a68841fdd8acbf015721173b1
1 parent 93e4c2a commit 37afb3c

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

debug/error.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,41 @@ import (
88

99
"github.com/spacemonkeygo/monkit/v3"
1010

11-
"storj.io/drpc/drpcerr"
11+
"storj.io/common/rpc/rpcstatus"
1212
)
1313

1414
func init() {
1515
monkit.AddErrorNameHandler(func(err error) (string, bool) {
16-
if code := drpcerr.Code(err); code != 0 {
17-
return fmt.Sprintf("drpc_%d", code), true
16+
var code uint64
17+
forLoop:
18+
for i := 0; i < 100; i++ {
19+
if v, ok := err.(interface{ Name() (string, bool) }); ok {
20+
if cls, ok := v.Name(); ok && cls != "" {
21+
return cls, true
22+
}
23+
}
24+
if v, ok := err.(interface{ Code() uint64 }); ok {
25+
if code == 0 {
26+
code = v.Code()
27+
}
28+
}
29+
switch v := err.(type) { //nolint: errorlint // this is a custom unwrap loop
30+
case interface{ Cause() error }:
31+
err = v.Cause()
32+
case interface{ Unwrap() error }:
33+
err = v.Unwrap()
34+
case interface{ Unwrap() []error }:
35+
errs := v.Unwrap()
36+
if len(errs) == 0 {
37+
break
38+
}
39+
err = errs[0]
40+
default:
41+
break forLoop
42+
}
43+
}
44+
if code != 0 {
45+
return fmt.Sprintf("drpc_%s", rpcstatus.StatusCode(code).String()), true
1846
}
1947
return "", false
2048
})

rpc/rpcstatus/status.go

+24-10
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,18 @@ func Code(err error) StatusCode {
7070
case errors.Is(err, context.DeadlineExceeded):
7171
return DeadlineExceeded
7272
default:
73-
if code := StatusCode(drpcerr.Code(err)); code != Unknown {
74-
return code
75-
}
76-
77-
return Unknown
73+
return StatusCode(drpcerr.Code(err))
7874
}
7975
}
8076

81-
// Wrap wraps the error with the provided status code.
82-
func Wrap(code StatusCode, err error) error {
77+
// NamedWrap returns the err error wrapped with a defined monkit class name and status code.
78+
func NamedWrap(name string, code StatusCode, err error) error {
8379
if err == nil {
8480
return nil
8581
}
8682

8783
ce := &codeErr{
84+
name: name,
8885
code: code,
8986
}
9087

@@ -96,14 +93,29 @@ func Wrap(code StatusCode, err error) error {
9693
return ce
9794
}
9895

96+
// NamedError creates and returns an error wrapped with a defined monkit class name and status code.
97+
func NamedError(name string, code StatusCode, msg string) error {
98+
return NamedWrap(name, code, errs.New("%s", msg))
99+
}
100+
101+
// NamedErrorf creates and returns an error wrapped with a defined monkit class name and status code.
102+
func NamedErrorf(name string, code StatusCode, format string, a ...any) error {
103+
return NamedWrap(name, code, errs.New(format, a...))
104+
}
105+
106+
// Wrap wraps the error with the provided status code.
107+
func Wrap(code StatusCode, err error) error {
108+
return NamedWrap("", code, err)
109+
}
110+
99111
// Error wraps the message with a status code into an error.
100112
func Error(code StatusCode, msg string) error {
101-
return Wrap(code, errs.New("%s", msg))
113+
return NamedError("", code, msg)
102114
}
103115

104116
// Errorf : Error :: fmt.Sprintf : fmt.Sprint.
105117
func Errorf(code StatusCode, format string, a ...interface{}) error {
106-
return Wrap(code, errs.New(format, a...))
118+
return NamedErrorf("", code, format, a...)
107119
}
108120

109121
type errsError interface {
@@ -114,11 +126,13 @@ type errsError interface {
114126

115127
// codeErr implements error that can work both in grpc and drpc.
116128
type codeErr struct {
129+
name string
117130
errsError
118131
code StatusCode
119132
}
120133

121134
func (c *codeErr) Unwrap() error { return c.errsError }
122135
func (c *codeErr) Cause() error { return c.errsError }
123136

124-
func (c *codeErr) Code() uint64 { return uint64(c.code) }
137+
func (c *codeErr) Code() uint64 { return uint64(c.code) }
138+
func (c *codeErr) Name() (string, bool) { return c.name, c.name != "" }

0 commit comments

Comments
 (0)