Skip to content

Instantly share code, notes, and snippets.

@Linuxpizi
Created August 5, 2019 07:34
Show Gist options
  • Save Linuxpizi/b67a86710723775eb64c08d453d0f3da to your computer and use it in GitHub Desktop.
Save Linuxpizi/b67a86710723775eb64c08d453d0f3da to your computer and use it in GitHub Desktop.
add error imp to track error stack show where error (file,line and date)
package uError
import (
"fmt"
"runtime"
)
// LError ..
type LError struct {
errors []error
msg string
}
// New ..
func New() *LError {
L := &LError{}
pc, f, l, ok := runtime.Caller(2)
if ok {
_fun := runtime.FuncForPC(pc)
funName := ""
if _fun == nil {
funName += `未知`
} else {
funName += _fun.Name()
}
err := fmt.Errorf("%s\t%s:%d", funName, f, l)
L.Add(err)
}
return L
}
// Add ..
func (lError *LError) Add(e error) *LError {
lError.errors = append(lError.errors, e)
return lError
}
// Error ..
func (lError *LError) Error() string {
msg := ""
for i, v := range lError.errors {
if i == len(lError.errors)-1 {
msg += fmt.Sprintf("%+v", v)
} else {
msg += fmt.Sprintf("%+v", v) + "\n"
}
}
return lError.GetMsg() + msg
}
// Msg ..
func (lError *LError) Msg(msg string) *LError {
lError.msg = msg
return lError
}
// GetMsg ..
func (lError *LError) GetMsg() string {
return lError.msg
}
add error imp to track error stack show where error (file,line and date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment