mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-05 05:18:52 +08:00
675e6d352b
Signed-off-by: dragondriver <jiquan.long@zilliz.com>
48 lines
1.2 KiB
Go
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
|
|
}
|