gitee.com/woood2/luca@v1.0.4/cmd/micro/pkg/err_micro.go (about)

     1  package pkg
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  )
     6  
     7  var (
     8  	BadRequest            = errors.New("非法输入")
     9  	MissingAuthentication = errors.New("鉴权凭证缺失")
    10  	InvalidAuthentication = errors.New("非法的鉴权凭证")
    11  	ExpiredAuthentication = errors.New("鉴权凭证已过期")
    12  	InvalidSn             = errors.New("非法的签名")
    13  )
    14  
    15  var codeErr = map[string]error{
    16  	"BadRequest":            BadRequest,
    17  	"MissingAuthentication": MissingAuthentication,
    18  	"InvalidAuthentication": InvalidAuthentication,
    19  	"ExpiredAuthentication": ExpiredAuthentication,
    20  	"InvalidSn":             InvalidSn,
    21  }
    22  
    23  var errMap = make(map[error]bool)
    24  
    25  func init() {
    26  	for err, str := range BusinessErrCode {
    27  		codeErr[str] = err
    28  	}
    29  	for _, err := range codeErr {
    30  		errMap[err] = true
    31  	}
    32  }
    33  
    34  func isEnumeratedError(err error) bool {
    35  	_, ok := errMap[err]
    36  	return ok
    37  }
    38  
    39  func code2Err(code string) error {
    40  	if err, ok := codeErr[code]; ok {
    41  		return err
    42  	} else {
    43  		return errors.Errorf("unknown err code: %s", code)
    44  	}
    45  }