-
Notifications
You must be signed in to change notification settings - Fork 117
/
errors.go
52 lines (45 loc) · 1.33 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
46
47
48
49
50
51
52
package utils
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/gogo/protobuf/grpc"
"github.com/tendermint/tendermint/libs/log"
grpc2 "google.golang.org/grpc"
)
type causer interface {
Cause() error
}
// IsABCIError checks if the error is a (wrapped) registered error
func IsABCIError(err error) bool {
switch e := err.(type) {
case nil:
return false
case *errors.Error:
return true
case causer:
return IsABCIError(e.Cause())
default:
return false
}
}
// ErrorWrapper wraps around a grpc server to return registered errors
type ErrorWrapper struct {
grpc.Server
Err *errors.Error
Logger func(ctx sdk.Context) log.Logger
}
// RegisterService ensures that every server method that gets registered returns a registered error
func (r ErrorWrapper) RegisterService(sd *grpc2.ServiceDesc, server interface{}) {
for _, method := range sd.Methods {
method.Handler = func(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc2.UnaryServerInterceptor) (interface{}, error) {
res, err := method.Handler(srv, ctx, dec, interceptor)
if err != nil && !IsABCIError(err) {
err = r.Err.Wrap(err.Error())
r.Logger(sdk.UnwrapSDKContext(ctx)).Debug(err.Error())
}
return res, err
}
}
r.Server.RegisterService(sd, server)
}