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

     1  package erro
     2  
     3  // TraceableError 可跟踪的错误,异常转述包装
     4  type TraceableError struct {
     5  	err    error
     6  	errStr string
     7  	cause  error
     8  }
     9  
    10  // Error 错误描述
    11  func (w *TraceableError) Error() string {
    12  	s := ""
    13  	if w.err != nil {
    14  		s += w.err.Error()
    15  	}
    16  	if w.errStr != "" {
    17  		s += w.errStr
    18  	}
    19  	if w.cause != nil {
    20  		s = s + "\ncause: " + w.cause.Error()
    21  	}
    22  	return s
    23  }
    24  
    25  // Cause 获取错误的原因
    26  func (w *TraceableError) Cause() error {
    27  	return w.cause
    28  }
    29  
    30  // NewTraceableError 创建可跟踪的错误
    31  func NewTraceableError(err error, cause error) error {
    32  	return &TraceableError{
    33  		err:   err,
    34  		cause: cause,
    35  	}
    36  }
    37  
    38  // NewTraceableErrorf 通过 string 描述创建可跟踪的错误
    39  func NewTraceableErrorf(errStr string, cause error) error {
    40  	return &TraceableError{
    41  		errStr: errStr,
    42  		cause:  cause,
    43  	}
    44  }