Skip to content

Commit

Permalink
fix: PRT-fix-grpc-error-code-parsing (lavanet#1405)
Browse files Browse the repository at this point in the history
* adding status code to grpc replies

* fixing status code parsing.
  • Loading branch information
ranlavanet authored May 5, 2024
1 parent d4cd0e1 commit 41aaff0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpcInterfaceMessages
import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/fullstorydev/grpcurl"
Expand Down Expand Up @@ -32,7 +33,10 @@ type GrpcMessage struct {
}

func (jm GrpcMessage) CheckResponseError(data []byte, httpStatusCode int) (hasError bool, errorMessage string) {
// grpc doesn't get here as it returns a real error
// grpc status code different than OK or 0 is a node error.
if httpStatusCode != 0 && httpStatusCode != http.StatusOK {
return true, ""
}
return false, ""
}

Expand Down
25 changes: 9 additions & 16 deletions protocol/chainlib/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,21 +533,6 @@ func (cp *GrpcChainProxy) SendNodeMsg(ctx context.Context, ch chan interface{},
connectCtx, cancel := cp.CapTimeoutForSend(ctx, chainMessage)
defer cancel()
err = conn.Invoke(connectCtx, "/"+nodeMessage.Path, msg, response, grpc.Header(&respHeaders))
// Extract status code from response headers
statusCodeHeader := respHeaders.Get("grpc-status")
if len(statusCodeHeader) > 0 {
statusCodeTest, err := strconv.Atoi(statusCodeHeader[0])
if err != nil {
// Handle error
utils.LavaFormatError("Error:", err, utils.LogAttr("statusCode", statusCodeTest))
} else {
// Use the status code
utils.LavaFormatDebug("Status Code:", utils.LogAttr("statusCode", statusCodeTest))
}
} else {
utils.LavaFormatDebug("NO Status Code:")
// No status code found in response headers
}
if err != nil {
// Validate if the error is related to the provider connection to the node or it is a valid error
// in case the error is valid (e.g. bad input parameters) the error will return in the form of a valid error reply
Expand All @@ -559,6 +544,9 @@ func (cp *GrpcChainProxy) SendNodeMsg(ctx context.Context, ch chan interface{},
if handlingError != nil {
return nil, "", nil, handlingError
}
// set status code for user header
trailer := metadata.Pairs(common.StatusCodeMetadataKey, strconv.Itoa(int(statusCode)))
grpc.SetTrailer(ctx, trailer) // we ignore this error here since this code can be triggered not from grpc
reply := &RelayReplyWrapper{
StatusCode: int(statusCode),
RelayReply: &pairingtypes.RelayReply{
Expand All @@ -574,8 +562,13 @@ func (cp *GrpcChainProxy) SendNodeMsg(ctx context.Context, ch chan interface{},
if err != nil {
return nil, "", nil, utils.LavaFormatError("proto.Marshal(response) Failed", err, utils.Attribute{Key: "GUID", Value: ctx})
}
// set response status code
validResponseStatus := http.StatusOK
trailer := metadata.Pairs(common.StatusCodeMetadataKey, strconv.Itoa(validResponseStatus))
grpc.SetTrailer(ctx, trailer) // we ignore this error here since this code can be triggered not from grpc
// create reply wrapper
reply := &RelayReplyWrapper{
StatusCode: http.StatusOK, // status code is used only for rest at the moment
StatusCode: validResponseStatus, // status code is used only for rest at the moment
RelayReply: &pairingtypes.RelayReply{
Data: respBytes,
Metadata: convertToMetadataMapOfSlices(respHeaders),
Expand Down

0 comments on commit 41aaff0

Please sign in to comment.