github.com/tencent/goom@v1.0.1/erro/func_not_found.go (about)

     1  package erro
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/tencent/goom/internal/logger"
     7  )
     8  
     9  const prefix = "func not found: "
    10  
    11  // FuncNotFound 函数未找到异常
    12  type FuncNotFound struct {
    13  	funcName    string
    14  	suggestions []string
    15  }
    16  
    17  // Error 返回错误字符串
    18  func (e *FuncNotFound) Error() string {
    19  	msg := prefix + e.funcName
    20  	if e.suggestions == nil {
    21  		return msg
    22  	}
    23  
    24  	var noEmptyStrings []string
    25  	for _, v := range e.suggestions {
    26  		if v == "" {
    27  			continue
    28  		}
    29  		noEmptyStrings = append(noEmptyStrings, v)
    30  	}
    31  	if len(noEmptyStrings) == 0 {
    32  		return msg
    33  	}
    34  
    35  	tips := "\ndo you mean: (?) \n* "
    36  	tips += strings.Join(noEmptyStrings, "\n* ")
    37  	return msg + logger.Magenta.AddAll(tips)
    38  }
    39  
    40  // NewFuncNotFoundError 函数未找到
    41  // funcName 函数名称
    42  func NewFuncNotFoundError(funcName string) error {
    43  	return &FuncNotFound{funcName: funcName}
    44  }
    45  
    46  // NewFuncNotFoundErrorWithSuggestion 函数未找到并给出提示
    47  // funcName 函数名称
    48  func NewFuncNotFoundErrorWithSuggestion(funcName string, suggestions []string) error {
    49  	return &FuncNotFound{funcName: funcName, suggestions: suggestions}
    50  }