Skip to content

Commit

Permalink
Handle responseHeader.is_error. Closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
radekg authored Feb 16, 2022
1 parent 18ac3ad commit 4331d18
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions client/single_node_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@ func (c *defaultSingleNodeClient) readResponseInto(reader *bytes.Buffer, m proto
}
}

if *responseHeader.IsError {
errorResponse := &ybApi.ErrorStatusPB{}
errorUnmarshalErr := utils.DeserializeProto(responsePayloadBuf, errorResponse)
if errorUnmarshalErr != nil {
return &errors.UnprocessableResponseError{
Cause: errorUnmarshalErr,
ConsumedPayload: responseHeaderBuf,
}
}
return &errors.ServiceRPCError{
Cause: errorResponse,
}
}

protoErr2 := utils.DeserializeProto(responsePayloadBuf, m)
if protoErr2 != nil {
return &errors.UnprocessableResponseError{
Expand Down
21 changes: 21 additions & 0 deletions errors/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
ErrorMessageReconnectRequired = "client: reconnect required"
// ErrorMessageSendFailed is an error message.
ErrorMessageSendFailed = "client: send failed"
// ErrorMessageServiceError is an error message.
ErrorMessageServiceError = "client: service error"
// ErrorMessageUnprocessableResponse is an error message.
ErrorMessageUnprocessableResponse = "client: unprocessable response"
)
Expand Down Expand Up @@ -124,6 +126,25 @@ func (e *RequiresReconnectError) Error() string {
return fmt.Sprintf("%s: no service for type '%s'", ErrorMessageReconnectRequired, e.Cause.Error())
}

// ServiceRPCError is returned when the client responds with
// a response header with is_error true.
type ServiceRPCError struct {
Cause *ybApi.ErrorStatusPB
}

func (e *ServiceRPCError) Error() string {
codeString := ybApi.ErrorStatusPB_RpcErrorCodePB_name[int32(ybApi.ErrorStatusPB_FATAL_UNKNOWN)]
if e.Cause.Code != nil {
if v, ok := ybApi.ErrorStatusPB_RpcErrorCodePB_name[int32(*e.Cause.Code)]; ok {
codeString = v
}
}
if e.Cause.Message == nil {
return fmt.Sprintf("%s: %s", ErrorMessageServiceError, codeString)
}
return fmt.Sprintf("%s: %s: %s", ErrorMessageServiceError, codeString, *e.Cause.Message)
}

// SendError is returned when the client is unable to
// send the payload or receive from the server.
type SendError struct {
Expand Down

0 comments on commit 4331d18

Please sign in to comment.