milvus/internal/util/funcutil/verify_response.go
Jiquan Long 675e6d352b
QueryNodes send search/query results by rpc (#15223)
Signed-off-by: dragondriver <jiquan.long@zilliz.com>
2022-01-17 14:41:35 +08:00

48 lines
1.2 KiB
Go

package funcutil
import (
"errors"
"github.com/milvus-io/milvus/internal/proto/commonpb"
)
// errors for VerifyResponse
var errNilResponse = errors.New("response is nil")
var errNilStatusResponse = errors.New("response has nil status")
var errUnknownResponseType = errors.New("unknown response type")
// Response response interface for verification
type Response interface {
GetStatus() *commonpb.Status
}
// VerifyResponse verify grpc Response 1. check error is nil 2. check response.GetStatus() with status success
func VerifyResponse(response interface{}, err error) error {
if err != nil {
return err
}
if response == nil {
return errNilResponse
}
switch resp := response.(type) {
case Response:
// note that resp will not be nil here, since it's still an interface
if resp.GetStatus() == nil {
return errNilStatusResponse
}
if resp.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success {
return errors.New(resp.GetStatus().GetReason())
}
case *commonpb.Status:
if resp == nil {
return errNilResponse
}
if resp.ErrorCode != commonpb.ErrorCode_Success {
return errors.New(resp.GetReason())
}
default:
return errUnknownResponseType
}
return nil
}