github.com/TeaOSLab/EdgeNode@v1.3.8/internal/errors/error.go (about) 1 package errors 2 3 import ( 4 "errors" 5 "github.com/TeaOSLab/EdgeNode/internal/utils" 6 "path/filepath" 7 "runtime" 8 "strconv" 9 ) 10 11 type errorObj struct { 12 err error 13 file string 14 line int 15 funcName string 16 } 17 18 func (this *errorObj) Error() string { 19 s := this.err.Error() + "\n " + utils.RemoveWorkspace(this.file) 20 if len(this.funcName) > 0 { 21 s += ":" + this.funcName + "()" 22 } 23 s += ":" + strconv.Itoa(this.line) 24 return s 25 } 26 27 // New 新错误 28 func New(errText string) error { 29 ptr, file, line, ok := runtime.Caller(1) 30 funcName := "" 31 if ok { 32 frame, _ := runtime.CallersFrames([]uintptr{ptr}).Next() 33 funcName = filepath.Base(frame.Function) 34 } 35 return &errorObj{ 36 err: errors.New(errText), 37 file: file, 38 line: line, 39 funcName: funcName, 40 } 41 } 42 43 // Wrap 包装已有错误 44 func Wrap(err error) error { 45 if err == nil { 46 return nil 47 } 48 49 ptr, file, line, ok := runtime.Caller(1) 50 funcName := "" 51 if ok { 52 frame, _ := runtime.CallersFrames([]uintptr{ptr}).Next() 53 funcName = filepath.Base(frame.Function) 54 } 55 return &errorObj{ 56 err: err, 57 file: file, 58 line: line, 59 funcName: funcName, 60 } 61 }