feat(api): add types for api returned errors

This commit is contained in:
Alexader 2021-04-15 10:37:47 +08:00
parent 7c1f8b9e1b
commit 3828217bf4
2 changed files with 17 additions and 4 deletions

View File

@ -2,10 +2,14 @@ package grpc
import (
"context"
"errors"
"fmt"
"github.com/meshplus/bitxhub-kit/storage"
"github.com/meshplus/bitxhub-kit/types"
"github.com/meshplus/bitxhub-model/pb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (cbs *ChainBrokerService) GetReceipt(ctx context.Context, req *pb.TransactionHashMsg) (*pb.Receipt, error) {
@ -13,5 +17,12 @@ func (cbs *ChainBrokerService) GetReceipt(ctx context.Context, req *pb.Transacti
if hash == nil {
return nil, fmt.Errorf("invalid format of receipt hash for querying receipt")
}
return cbs.api.Broker().GetReceipt(hash)
r, err := cbs.api.Broker().GetReceipt(hash)
if err != nil {
if errors.Is(storage.ErrorNotFound, err) {
return nil, status.Newf(codes.NotFound, fmt.Sprintf("cannot found receipt for %s", hash), err.Error()).Err()
}
return nil, status.Newf(codes.Internal, "internal handling error", err.Error()).Err()
}
return r, nil
}

View File

@ -9,6 +9,8 @@ import (
"github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/meshplus/bitxhub-kit/types"
"github.com/meshplus/bitxhub-model/pb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// SendTransaction handles transaction sent by the client.
@ -16,16 +18,16 @@ import (
func (cbs *ChainBrokerService) SendTransaction(ctx context.Context, tx *pb.Transaction) (*pb.TransactionHashMsg, error) {
err := cbs.api.Broker().OrderReady()
if err != nil {
return nil, fmt.Errorf("the system is temporarily unavailable, err: %s", err.Error())
return nil, status.Newf(codes.Internal, "the system is temporarily unavailable %s", err.Error()).Err()
}
if err := cbs.checkTransaction(tx); err != nil {
return nil, err
return nil, status.Newf(codes.InvalidArgument, "check transaction fail for %s", err.Error()).Err()
}
hash, err := cbs.sendTransaction(tx)
if err != nil {
return nil, err
return nil, status.Newf(codes.Internal, "internal handling transaction fail %s", err.Error()).Err()
}
return &pb.TransactionHashMsg{TxHash: hash}, nil