Skip to content

Commit

Permalink
feat: Improve the "unexpected GRPC response error" (#131)
Browse files Browse the repository at this point in the history
Unfortunately I can't figure out how to get the actual type of the response.
At least now it's wrapped in a function so it can be easily improved,
and it eliminates the special case in makeRequest().
  • Loading branch information
schwern authored and cprice404 committed Mar 14, 2023
1 parent ba50db9 commit abd4c59
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion momento/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ func (r *GetRequest) interpretGrpcResponse() error {
r.response = &GetMiss{}
return nil
} else {
return errUnexpectedGrpcResponse
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}
}
2 changes: 1 addition & 1 deletion momento/list_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (r *ListFetchRequest) interpretGrpcResponse() error {
case *pb.XListFetchResponse_Missing:
r.response = &ListFetchMiss{}
default:
return errUnexpectedGrpcResponse
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}
return nil
}
2 changes: 1 addition & 1 deletion momento/list_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (r *ListLengthRequest) interpretGrpcResponse() error {
case *pb.XListLengthResponse_Missing:
r.response = &ListLengthMiss{}
default:
return errUnexpectedGrpcResponse
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}
return nil
}
2 changes: 1 addition & 1 deletion momento/list_pop_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *ListPopBackRequest) interpretGrpcResponse() error {
case *pb.XListPopBackResponse_Missing:
r.response = &ListPopBackMiss{}
default:
return errUnexpectedGrpcResponse
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}
return nil
}
2 changes: 1 addition & 1 deletion momento/list_pop_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *ListPopFrontRequest) interpretGrpcResponse() error {
case *pb.XListPopFrontResponse_Missing:
r.response = &ListPopFrontMiss{}
default:
return errUnexpectedGrpcResponse
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}
return nil
}
12 changes: 10 additions & 2 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package momento

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand All @@ -16,7 +15,16 @@ import (
"github.com/momentohq/client-sdk-go/utils"
)

var errUnexpectedGrpcResponse = errors.New("unexpected gRPC response")
func errUnexpectedGrpcResponse(r requester, grpcResp grpcResponse) momentoerrors.MomentoSvcErr {
return momentoerrors.NewMomentoSvcErr(
momentoerrors.InternalServerError,
fmt.Sprintf(
"%s request got an unexpected response %T '%s'",
r.requestName(), grpcResp, grpcResp,
),
nil,
)
}

type requester interface {
hasCacheName
Expand Down
16 changes: 2 additions & 14 deletions momento/scs_data_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package momento

import (
"context"
"fmt"
"time"

"github.com/momentohq/client-sdk-go/internal/grpcmanagers"
Expand Down Expand Up @@ -68,24 +67,13 @@ func (client scsDataClient) makeRequest(ctx context.Context, r requester) error
ctx, client.CreateNewMetadata(r.cacheName()),
)

grpcResp, err := r.makeGrpcRequest(requestMetadata, client)
_, err := r.makeGrpcRequest(requestMetadata, client)
if err != nil {
return momentoerrors.ConvertSvcErr(err)
}

if err := r.interpretGrpcResponse(); err != nil {
if err == errUnexpectedGrpcResponse {
return momentoerrors.NewMomentoSvcErr(
momentoerrors.InternalServerError,
fmt.Sprintf(
"%s request: %v. Request returned '%s'",
r.requestName(), err, grpcResp,
),
nil,
)
} else {
return err
}
return err
}

return nil
Expand Down
9 changes: 3 additions & 6 deletions momento/sorted_set_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,15 @@ func (r *SortedSetFetchRequest) makeGrpcRequest(metadata context.Context, client
}

func (r *SortedSetFetchRequest) interpretGrpcResponse() error {
grpcResp := r.grpcResponse

// Convert from grpc struct to internal struct
switch rsp := grpcResp.SortedSet.(type) {
switch grpcResp := r.grpcResponse.SortedSet.(type) {
case *pb.XSortedSetFetchResponse_Found:
r.response = &SortedSetFetchHit{
Elements: sortedSetGrpcElementToModel(rsp.Found.GetElements()),
Elements: sortedSetGrpcElementToModel(grpcResp.Found.GetElements()),
}
case *pb.XSortedSetFetchResponse_Missing:
r.response = &SortedSetFetchMiss{}
default:
return errUnexpectedGrpcResponse
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion momento/sorted_set_get_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r *SortedSetGetRankRequest) interpretGrpcResponse() error {
case *pb.XSortedSetGetRankResponse_Missing:
resp = &SortedSetGetRankMiss{}
default:
return errUnexpectedGrpcResponse
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}

r.response = resp
Expand Down
15 changes: 5 additions & 10 deletions momento/sorted_set_get_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,17 @@ func (r *SortedSetGetScoreRequest) makeGrpcRequest(metadata context.Context, cli
}

func (r *SortedSetGetScoreRequest) interpretGrpcResponse() error {
grpcResp := r.grpcResponse

var resp SortedSetGetScoreResponse
switch r := grpcResp.SortedSet.(type) {
switch grpcResp := r.grpcResponse.SortedSet.(type) {
case *pb.XSortedSetGetScoreResponse_Found:
resp = &SortedSetGetScoreHit{
Elements: convertSortedSetScoreElement(r.Found.GetElements()),
r.response = &SortedSetGetScoreHit{
Elements: convertSortedSetScoreElement(grpcResp.Found.GetElements()),
}
case *pb.XSortedSetGetScoreResponse_Missing:
resp = &SortedSetGetScoreMiss{}
r.response = &SortedSetGetScoreMiss{}
default:
return errUnexpectedGrpcResponse
return errUnexpectedGrpcResponse(r, r.grpcResponse)
}

r.response = resp

return nil
}

Expand Down

0 comments on commit abd4c59

Please sign in to comment.