Skip to content

Commit 4331d18

Browse files
authored
Handle responseHeader.is_error. Closes #64
1 parent 18ac3ad commit 4331d18

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

client/single_node_client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,20 @@ func (c *defaultSingleNodeClient) readResponseInto(reader *bytes.Buffer, m proto
303303
}
304304
}
305305

306+
if *responseHeader.IsError {
307+
errorResponse := &ybApi.ErrorStatusPB{}
308+
errorUnmarshalErr := utils.DeserializeProto(responsePayloadBuf, errorResponse)
309+
if errorUnmarshalErr != nil {
310+
return &errors.UnprocessableResponseError{
311+
Cause: errorUnmarshalErr,
312+
ConsumedPayload: responseHeaderBuf,
313+
}
314+
}
315+
return &errors.ServiceRPCError{
316+
Cause: errorResponse,
317+
}
318+
}
319+
306320
protoErr2 := utils.DeserializeProto(responsePayloadBuf, m)
307321
if protoErr2 != nil {
308322
return &errors.UnprocessableResponseError{

errors/client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838
ErrorMessageReconnectRequired = "client: reconnect required"
3939
// ErrorMessageSendFailed is an error message.
4040
ErrorMessageSendFailed = "client: send failed"
41+
// ErrorMessageServiceError is an error message.
42+
ErrorMessageServiceError = "client: service error"
4143
// ErrorMessageUnprocessableResponse is an error message.
4244
ErrorMessageUnprocessableResponse = "client: unprocessable response"
4345
)
@@ -124,6 +126,25 @@ func (e *RequiresReconnectError) Error() string {
124126
return fmt.Sprintf("%s: no service for type '%s'", ErrorMessageReconnectRequired, e.Cause.Error())
125127
}
126128

129+
// ServiceRPCError is returned when the client responds with
130+
// a response header with is_error true.
131+
type ServiceRPCError struct {
132+
Cause *ybApi.ErrorStatusPB
133+
}
134+
135+
func (e *ServiceRPCError) Error() string {
136+
codeString := ybApi.ErrorStatusPB_RpcErrorCodePB_name[int32(ybApi.ErrorStatusPB_FATAL_UNKNOWN)]
137+
if e.Cause.Code != nil {
138+
if v, ok := ybApi.ErrorStatusPB_RpcErrorCodePB_name[int32(*e.Cause.Code)]; ok {
139+
codeString = v
140+
}
141+
}
142+
if e.Cause.Message == nil {
143+
return fmt.Sprintf("%s: %s", ErrorMessageServiceError, codeString)
144+
}
145+
return fmt.Sprintf("%s: %s: %s", ErrorMessageServiceError, codeString, *e.Cause.Message)
146+
}
147+
127148
// SendError is returned when the client is unable to
128149
// send the payload or receive from the server.
129150
type SendError struct {

0 commit comments

Comments
 (0)