The primary purpose is to facility debugging and make error log more useful,but It is a big change,so I want to know:any poptential problem?
package main
import(
"errors"
//"fmt"
"runtime"
"github.com/fatih/structs"
"github.com/Sirupsen/logrus"
)
type Error interface {
Mapify() map[string]interface{}
Error() string
}
func New(err error) Error {
//get error runtime info
pc, file, line, _ := runtime.Caller(1)
funcName := runtime.FuncForPC(pc).Name()
return &ErrorString{err.Error(), file, funcName, line}
}
type ErrorString struct {
Err string
File string//file name
Func string//function name
Line int
}
func (s *ErrorString) Mapify() map[string]interface{} {
return structs.Map(s)
}
func (s *ErrorString) Error() string {
return s.Err
}
func main(){
logrus.WithFields(logrus.Fields(throw().Mapify())).Error(errors.New("test"))
}
func throw() Error{
return New(errors.New("any error"))
}