mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-03 04:19:18 +08:00
c9d0c157ec
Signed-off-by: jaime <yun.zhang@zilliz.com>
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package funcutil
|
|
|
|
import (
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/milvus-io/milvus-proto/go-api/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
|
|
}
|