Skip to content

Commit

Permalink
feat(types/errors): add GRPCWrap to add ABCICode, Codespace and event…
Browse files Browse the repository at this point in the history
…ual stack trace on gRPC requests

backport of 0740170
  • Loading branch information
kakysha committed Jan 29, 2025
1 parent 738074d commit c2faa1b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,10 @@ func gRPCErrorToSDKError(err error) error {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}

if len(status.Details()) > 0 {
err = errorsmod.Wrapf(err, "%v", status.Details())
}

switch status.Code() {
case codes.NotFound:
return errorsmod.Wrap(sdkerrors.ErrKeyNotFound, err.Error())
Expand Down
26 changes: 26 additions & 0 deletions types/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package errors

import (
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/pkg/errors"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// RootCodespace is the codespace for all errors defined in this package
Expand Down Expand Up @@ -147,3 +153,23 @@ var (
// explicitly set timeout timestamp.
ErrTxTimeout = errorsmod.Register(RootCodespace, 42, "tx timeout")
)

func GRPCWrap(err error, c codes.Code, msg string) error {
if err == nil {
return nil
}
st := status.New(c, msg)
var sdkErr *errorsmod.Error
if errors.As(err, &sdkErr) {
errorInfo := &errdetails.ErrorInfo{
Reason: sdkErr.Error(),
Metadata: map[string]string{"Codespace": sdkErr.Codespace(), "ABCICode": fmt.Sprintf("%d", sdkErr.ABCICode())},
}
var withDetailsErr error
st, withDetailsErr = st.WithDetails(errorInfo)
if withDetailsErr != nil {
return status.Errorf(c, "%v (failed to add error details: %v)", msg, withDetailsErr)
}
}
return st.Err()
}
3 changes: 2 additions & 1 deletion x/auth/tx/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tx

import (
"context"
"fmt"
"strings"

gogogrpc "github.com/cosmos/gogoproto/grpc"
Expand Down Expand Up @@ -96,7 +97,7 @@ func (s txServer) Simulate(ctx context.Context, req *txtypes.SimulateRequest) (*

gasInfo, result, err := s.simulate(txBytes)
if err != nil {
return nil, status.Errorf(codes.Unknown, "%v with gas used: '%d'", err, gasInfo.GasUsed)
return nil, sdkerrors.GRPCWrap(err, codes.Unknown, fmt.Sprintf("%v With gas wanted: '%d' and gas used: '%d' ", err, gasInfo.GasWanted, gasInfo.GasUsed))
}

return &txtypes.SimulateResponse{
Expand Down

0 comments on commit c2faa1b

Please sign in to comment.