Skip to content

Commit

Permalink
chore: refine error code
Browse files Browse the repository at this point in the history
  • Loading branch information
will-2012 committed Mar 22, 2023
1 parent 28bed3e commit c0ca23e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
28 changes: 28 additions & 0 deletions model/errors/rpc_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package errors

import (
"errors"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"
)

// common error
Expand Down Expand Up @@ -95,3 +99,27 @@ var (
// ErrSPNumber defines failed to get insufficient SPs from DB
ErrSPNumber = errors.New("failed to get sufficient SPs from DB")
)

// InnerErrorToGRPCError convents inner error to grpc/status error
func InnerErrorToGRPCError(err error) error {
if errors.Is(err, gorm.ErrRecordNotFound) ||
errors.Is(err, ErrNoSuchObject) {
return status.Errorf(codes.NotFound, "Object is not found")
}
if errors.Is(err, ErrCheckQuotaEnough) {
return status.Errorf(codes.PermissionDenied, "Quota is not enough")
}
return err
}

// GRPCErrorToInnerError convents grpc/status error to inner error
func GRPCErrorToInnerError(err error) error {
errStatus, _ := status.FromError(err)
if codes.NotFound == errStatus.Code() {
return ErrNoSuchObject
}
if codes.PermissionDenied == errStatus.Code() {
return ErrNoPermission
}
return err
}
5 changes: 4 additions & 1 deletion service/challenge/challenge_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package challenge
import (
"context"

merrors "github.com/bnb-chain/greenfield-storage-provider/model/errors"
"github.com/bnb-chain/greenfield-storage-provider/model/piecestore"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/service/challenge/types"
Expand All @@ -22,6 +23,7 @@ func (challenge *Challenge) ChallengePiece(
integrity, err := challenge.spDB.GetObjectIntegrity(req.GetObjectId())
if err != nil {
log.CtxErrorw(ctx, "failed to get integrity hash from db", "error", err)
err = merrors.InnerErrorToGRPCError(err)
return
}
resp.IntegrityHash = integrity.IntegrityHash
Expand All @@ -37,9 +39,10 @@ func (challenge *Challenge) ChallengePiece(
resp.PieceData, err = challenge.pieceStore.GetSegment(ctx, key, 0, -1)
if err != nil {
log.CtxErrorw(ctx, "failed to get payload", "error", err)
err = merrors.InnerErrorToGRPCError(err)
return
}
log.CtxInfow(ctx, "success to challenge the payload", "object_id", req.GetObjectId(),
log.CtxInfow(ctx, "succeed to challenge the payload", "object_id", req.GetObjectId(),
"piece_idx", req.GetSegmentIdx(), "replicate_idx", req.GetReplicaIdx(), "segment_count", len(integrity.Checksum))
return
}
4 changes: 4 additions & 0 deletions service/challenge/client/challenge_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/bnb-chain/greenfield-storage-provider/model"
merrors "github.com/bnb-chain/greenfield-storage-provider/model/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

Expand Down Expand Up @@ -50,5 +51,8 @@ func (client *ChallengeClient) ChallengePiece(ctx context.Context, objectID uint
SegmentIdx: segmentIdx,
}, opts...)
log.Debugw("finish to challenge piece", "error", err)
if err != nil {
return nil, nil, nil, merrors.GRPCErrorToInnerError(err)
}
return resp.GetIntegrityHash(), resp.GetPieceHash(), resp.GetPieceData(), err
}
3 changes: 2 additions & 1 deletion service/downloader/downloader_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/bnb-chain/greenfield-storage-provider/model"
merrors "github.com/bnb-chain/greenfield-storage-provider/model/errors"
"github.com/bnb-chain/greenfield-storage-provider/model/piecestore"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/service/downloader/types"
Expand Down Expand Up @@ -53,7 +54,7 @@ func (downloader *Downloader) GetObject(req *types.GetObjectRequest,
},
); err != nil {
log.Errorw("failed to check billing due to bucket quota", "error", err)
return err
return merrors.InnerErrorToGRPCError(err)
}

// TODO: It will be optimized
Expand Down
3 changes: 2 additions & 1 deletion service/gateway/object_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"

merrors "github.com/bnb-chain/greenfield-storage-provider/model/errors"
"github.com/bnb-chain/greenfield/types/s3util"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -101,7 +102,7 @@ func (gateway *Gateway) getObjectHandler(w http.ResponseWriter, r *http.Request)
}
if err != nil {
log.Errorw("failed to read stream", "error", err)
errDescription = makeErrorDescription(err)
errDescription = makeErrorDescription(merrors.GRPCErrorToInnerError(err))
return
}

Expand Down
14 changes: 7 additions & 7 deletions store/piecestore/storage/disk_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ func (d *diskFileStore) GetObject(ctx context.Context, key string, offset, limit

f, err := os.Open(p)
if err != nil {
log.Errorw("disk file failed to get object dut to open file", "error", err)
log.Errorw("disk file failed to get object due to open file", "error", err)
return nil, err
}

info, err := f.Stat()
if err != nil {
_ = f.Close()
log.Errorw("disk file failed to get object dut to Stat file", "error", err)
log.Errorw("disk file failed to get object due to stat file", "error", err)
return nil, err
}
if info.IsDir() {
Expand All @@ -82,7 +82,7 @@ func (d *diskFileStore) GetObject(ctx context.Context, key string, offset, limit
buf := make([]byte, limit)
n, err := f.Read(buf)
if err != nil {
log.Errorw("disk file failed to get object dut to read file", "error", err)
log.Errorw("disk file failed to get object due to read file", "error", err)
return nil, err
}
return io.NopCloser(bytes.NewBuffer(buf[:n])), nil
Expand All @@ -101,13 +101,13 @@ func (d *diskFileStore) PutObject(ctx context.Context, key string, reader io.Rea
f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(filepath.Dir(p), os.FileMode(0755)); err != nil {
log.Errorw("disk file failed to put object dut to MkdirAll", "error", err)
log.Errorw("disk file failed to put object due to MkdirAll", "error", err)
return err
}
f, err = os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
}
if err != nil {
log.Errorw("disk file failed to put object dut to open file", "error", err)
log.Errorw("disk file failed to put object due to open file", "error", err)
return err
}
defer func() {
Expand All @@ -120,12 +120,12 @@ func (d *diskFileStore) PutObject(ctx context.Context, key string, reader io.Rea
defer bufPool.Put(buf)
_, err = io.CopyBuffer(f, reader, *buf)
if err != nil {
log.Errorw("disk file failed to put object dut to copy buffer", "error", err)
log.Errorw("disk file failed to put object due to copy buffer", "error", err)
_ = f.Close()
return err
}
if err = f.Close(); err != nil {
log.Errorw("disk file failed to put object due to Close file", "error", err)
log.Errorw("disk file failed to put object due to close file", "error", err)
return err
}

Expand Down
5 changes: 5 additions & 0 deletions store/sqldb/object_integrity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package sqldb

import (
"encoding/hex"
"errors"
"fmt"

"github.com/bnb-chain/greenfield-storage-provider/util"
"gorm.io/gorm"
)

// GetObjectIntegrity return the integrity hash info
Expand All @@ -13,6 +15,9 @@ func (s *SpDBImpl) GetObjectIntegrity(objectID uint64) (*IntegrityMeta, error) {
result := s.db.Model(&IntegrityMetaTable{}).
Where("object_id = ?", objectID).
First(queryReturn)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, result.Error
}
if result.Error != nil {
return nil, fmt.Errorf("failed to query integrity meta record: %s", result.Error)
}
Expand Down

0 comments on commit c0ca23e

Please sign in to comment.