2022-01-17 14:41:35 +08:00
|
|
|
package funcutil
|
|
|
|
|
|
|
|
import (
|
2023-02-26 11:31:49 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2022-10-16 20:49:27 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/commonpb"
|
2022-01-17 14:41:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// errors for VerifyResponse
|
|
|
|
var errNilResponse = errors.New("response is nil")
|
2023-04-06 19:14:32 +08:00
|
|
|
|
2022-01-17 14:41:35 +08:00
|
|
|
var errNilStatusResponse = errors.New("response has nil status")
|
2023-04-06 19:14:32 +08:00
|
|
|
|
2022-01-17 14:41:35 +08:00
|
|
|
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
|
|
|
|
}
|