github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/merr/errors.go (about)

     1  // Licensed to the LF AI & Data foundation under one
     2  // or more contributor license agreements. See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership. The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License. You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package merr
    18  
    19  import (
    20  	"github.com/cockroachdb/errors"
    21  )
    22  
    23  const (
    24  	CanceledCode int32 = 10000
    25  	TimeoutCode  int32 = 10001
    26  )
    27  
    28  // Define leaf errors here,
    29  // WARN: take care to add new error,
    30  // check whehter you can use the erorrs below before adding a new one.
    31  // Name: Err + related prefix + error name
    32  var (
    33  	// Service related
    34  	ErrServiceNotReady             = newMilvusError("service not ready", 1, true) // This indicates the service is still in init
    35  	ErrServiceUnavailable          = newMilvusError("service unavailable", 2, true)
    36  	ErrServiceMemoryLimitExceeded  = newMilvusError("memory limit exceeded", 3, false)
    37  	ErrServiceRequestLimitExceeded = newMilvusError("request limit exceeded", 4, true)
    38  	ErrServiceInternal             = newMilvusError("service internal error", 5, false) // Never return this error out of Milvus
    39  	ErrServiceCrossClusterRouting  = newMilvusError("cross cluster routing", 6, false)
    40  	ErrServiceDiskLimitExceeded    = newMilvusError("disk limit exceeded", 7, false)
    41  	ErrServiceRateLimit            = newMilvusError("rate limit exceeded", 8, true)
    42  	ErrServiceForceDeny            = newMilvusError("force deny", 9, false)
    43  	ErrServiceUnimplemented        = newMilvusError("service unimplemented", 10, false)
    44  
    45  	// Collection related
    46  	ErrCollectionNotFound         = newMilvusError("collection not found", 100, false)
    47  	ErrCollectionNotLoaded        = newMilvusError("collection not loaded", 101, false)
    48  	ErrCollectionNumLimitExceeded = newMilvusError("exceeded the limit number of collections", 102, false)
    49  	ErrCollectionNotFullyLoaded   = newMilvusError("collection not fully loaded", 103, true)
    50  
    51  	// Partition related
    52  	ErrPartitionNotFound       = newMilvusError("partition not found", 200, false)
    53  	ErrPartitionNotLoaded      = newMilvusError("partition not loaded", 201, false)
    54  	ErrPartitionNotFullyLoaded = newMilvusError("partition not fully loaded", 202, true)
    55  
    56  	// ResourceGroup related
    57  	ErrResourceGroupNotFound = newMilvusError("resource group not found", 300, false)
    58  
    59  	// Replica related
    60  	ErrReplicaNotFound     = newMilvusError("replica not found", 400, false)
    61  	ErrReplicaNotAvailable = newMilvusError("replica not available", 401, false)
    62  
    63  	// Channel & Delegator related
    64  	ErrChannelNotFound     = newMilvusError("channel not found", 500, false)
    65  	ErrChannelLack         = newMilvusError("channel lacks", 501, false)
    66  	ErrChannelReduplicate  = newMilvusError("channel reduplicates", 502, false)
    67  	ErrChannelNotAvailable = newMilvusError("channel not available", 503, false)
    68  
    69  	// Segment related
    70  	ErrSegmentNotFound    = newMilvusError("segment not found", 600, false)
    71  	ErrSegmentNotLoaded   = newMilvusError("segment not loaded", 601, false)
    72  	ErrSegmentLack        = newMilvusError("segment lacks", 602, false)
    73  	ErrSegmentReduplicate = newMilvusError("segment reduplicates", 603, false)
    74  
    75  	// Index related
    76  	ErrIndexNotFound     = newMilvusError("index not found", 700, false)
    77  	ErrIndexNotSupported = newMilvusError("index type not supported", 701, false)
    78  	ErrIndexDuplicate    = newMilvusError("index duplicates", 702, false)
    79  
    80  	// Database related
    81  	ErrDatabaseNotFound         = newMilvusError("database not found", 800, false)
    82  	ErrDatabaseNumLimitExceeded = newMilvusError("exceeded the limit number of database", 801, false)
    83  	ErrDatabaseInvalidName      = newMilvusError("invalid database name", 802, false)
    84  
    85  	// Node related
    86  	ErrNodeNotFound     = newMilvusError("node not found", 901, false)
    87  	ErrNodeOffline      = newMilvusError("node offline", 902, false)
    88  	ErrNodeLack         = newMilvusError("node lacks", 903, false)
    89  	ErrNodeNotMatch     = newMilvusError("node not match", 904, false)
    90  	ErrNodeNotAvailable = newMilvusError("node not available", 905, false)
    91  
    92  	// IO related
    93  	ErrIoKeyNotFound = newMilvusError("key not found", 1000, false)
    94  	ErrIoFailed      = newMilvusError("IO failed", 1001, false)
    95  
    96  	// Parameter related
    97  	ErrParameterInvalid = newMilvusError("invalid parameter", 1100, false)
    98  
    99  	// Metrics related
   100  	ErrMetricNotFound = newMilvusError("metric not found", 1200, false)
   101  
   102  	// Message queue related
   103  	ErrMqTopicNotFound = newMilvusError("topic not found", 1300, false)
   104  	ErrMqTopicNotEmpty = newMilvusError("topic not empty", 1301, false)
   105  	ErrMqInternal      = newMilvusError("message queue internal error", 1302, false)
   106  	ErrDenyProduceMsg  = newMilvusError("deny to write the message to mq", 1303, false)
   107  
   108  	// Privilege related
   109  	// this operation is denied because the user not authorized, user need to login in first
   110  	ErrPrivilegeNotAuthenticated = newMilvusError("not authenticated", 1400, false)
   111  	// this operation is denied because the user has no permission to do this, user need higher privilege
   112  	ErrPrivilegeNotPermitted = newMilvusError("privilege not permitted", 1401, false)
   113  
   114  	// Alias related
   115  	ErrAliasNotFound               = newMilvusError("alias not found", 1600, false)
   116  	ErrAliasCollectionNameConfilct = newMilvusError("alias and collection name conflict", 1601, false)
   117  	ErrAliasAlreadyExist           = newMilvusError("alias already exist", 1602, false)
   118  
   119  	// field related
   120  	ErrFieldNotFound    = newMilvusError("field not found", 1700, false)
   121  	ErrFieldInvalidName = newMilvusError("field name invalid", 1701, false)
   122  
   123  	// high-level restful api related
   124  	ErrNeedAuthenticate          = newMilvusError("user hasn't authenticated", 1800, false)
   125  	ErrIncorrectParameterFormat  = newMilvusError("can only accept json format request", 1801, false)
   126  	ErrMissingRequiredParameters = newMilvusError("missing required parameters", 1802, false)
   127  	ErrMarshalCollectionSchema   = newMilvusError("fail to marshal collection schema", 1803, false)
   128  	ErrInvalidInsertData         = newMilvusError("fail to deal the insert data", 1804, false)
   129  	ErrInvalidSearchResult       = newMilvusError("fail to parse search result", 1805, false)
   130  	ErrCheckPrimaryKey           = newMilvusError("please check the primary key and its' type can only in [int, string]", 1806, false)
   131  
   132  	// replicate related
   133  	ErrDenyReplicateMessage = newMilvusError("deny to use the replicate message in the normal instance", 1900, false)
   134  	ErrInvalidMsgBytes      = newMilvusError("invalid replicate msg bytes", 1901, false)
   135  	ErrNoAssignSegmentID    = newMilvusError("no assign segment id", 1902, false)
   136  	ErrInvalidStreamObj     = newMilvusError("invalid stream object", 1903, false)
   137  
   138  	// Segcore related
   139  	ErrSegcore = newMilvusError("segcore error", 2000, false)
   140  
   141  	// Do NOT export this,
   142  	// never allow programmer using this, keep only for converting unknown error to milvusError
   143  	errUnexpected = newMilvusError("unexpected error", (1<<16)-1, false)
   144  
   145  	// import
   146  	ErrImportFailed = newMilvusError("importing data failed", 2100, false)
   147  )
   148  
   149  type milvusError struct {
   150  	msg       string
   151  	detail    string
   152  	retriable bool
   153  	errCode   int32
   154  }
   155  
   156  func newMilvusError(msg string, code int32, retriable bool) milvusError {
   157  	return milvusError{
   158  		msg:       msg,
   159  		detail:    msg,
   160  		retriable: retriable,
   161  		errCode:   code,
   162  	}
   163  }
   164  
   165  func newMilvusErrorWithDetail(msg string, detail string, code int32, retriable bool) milvusError {
   166  	return milvusError{
   167  		msg:       msg,
   168  		detail:    detail,
   169  		retriable: retriable,
   170  		errCode:   code,
   171  	}
   172  }
   173  
   174  func (e milvusError) code() int32 {
   175  	return e.errCode
   176  }
   177  
   178  func (e milvusError) Error() string {
   179  	return e.msg
   180  }
   181  
   182  func (e milvusError) Detail() string {
   183  	return e.detail
   184  }
   185  
   186  func (e milvusError) Is(err error) bool {
   187  	cause := errors.Cause(err)
   188  	if cause, ok := cause.(milvusError); ok {
   189  		return e.errCode == cause.errCode
   190  	}
   191  	return false
   192  }
   193  
   194  type multiErrors struct {
   195  	errs []error
   196  }
   197  
   198  func (e multiErrors) Unwrap() error {
   199  	if len(e.errs) <= 1 {
   200  		return nil
   201  	}
   202  	// To make merr work for multi errors,
   203  	// we need cause of multi errors, which defined as the last error
   204  	if len(e.errs) == 2 {
   205  		return e.errs[1]
   206  	}
   207  
   208  	return multiErrors{
   209  		errs: e.errs[1:],
   210  	}
   211  }
   212  
   213  func (e multiErrors) Error() string {
   214  	final := e.errs[0]
   215  	for i := 1; i < len(e.errs); i++ {
   216  		final = errors.Wrap(e.errs[i], final.Error())
   217  	}
   218  	return final.Error()
   219  }
   220  
   221  func (e multiErrors) Is(err error) bool {
   222  	for _, item := range e.errs {
   223  		if errors.Is(item, err) {
   224  			return true
   225  		}
   226  	}
   227  	return false
   228  }
   229  
   230  func Combine(errs ...error) error {
   231  	var filtered []error
   232  	for _, e := range errs {
   233  		if e != nil {
   234  			filtered = append(filtered, e)
   235  		}
   236  	}
   237  	if len(filtered) == 0 {
   238  		return nil
   239  	}
   240  	return multiErrors{
   241  		errs,
   242  	}
   243  }