github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/errors.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package caches
     4  
     5  import "errors"
     6  
     7  // 常用的几个错误
     8  var (
     9  	ErrNotFound                = errors.New("cache not found")
    10  	ErrFileIsWriting           = errors.New("the cache file is updating")
    11  	ErrInvalidRange            = errors.New("invalid range")
    12  	ErrEntityTooLarge          = errors.New("entity too large")
    13  	ErrWritingUnavailable      = errors.New("writing unavailable")
    14  	ErrWritingQueueFull        = errors.New("writing queue full")
    15  	ErrServerIsBusy            = errors.New("server is busy")
    16  	ErrUnexpectedContentLength = errors.New("unexpected content length")
    17  )
    18  
    19  // CapacityError 容量错误
    20  // 独立出来是为了可以在有些场合下可以忽略,防止产生没必要的错误提示数量太多
    21  type CapacityError struct {
    22  	err string
    23  }
    24  
    25  func NewCapacityError(err string) error {
    26  	return &CapacityError{err: err}
    27  }
    28  
    29  func (this *CapacityError) Error() string {
    30  	return this.err
    31  }
    32  
    33  // CanIgnoreErr 检查错误是否可以忽略
    34  func CanIgnoreErr(err error) bool {
    35  	if err == nil {
    36  		return true
    37  	}
    38  	if errors.Is(err, ErrFileIsWriting) ||
    39  		errors.Is(err, ErrEntityTooLarge) ||
    40  		errors.Is(err, ErrWritingUnavailable) ||
    41  		errors.Is(err, ErrWritingQueueFull) ||
    42  		errors.Is(err, ErrServerIsBusy) {
    43  		return true
    44  	}
    45  
    46  	var capacityErr *CapacityError
    47  	return errors.As(err, &capacityErr)
    48  }
    49  
    50  func IsCapacityError(err error) bool {
    51  	if err == nil {
    52  		return false
    53  	}
    54  
    55  	var capacityErr *CapacityError
    56  	return errors.As(err, &capacityErr)
    57  }
    58  
    59  func IsBusyError(err error) bool {
    60  	return err != nil && errors.Is(err, ErrServerIsBusy)
    61  }