Created
August 5, 2019 07:34
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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