From d02962dc65b6a4ed3173e3b2040b237feec70f1c Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Sat, 6 Apr 2024 01:33:03 +0700 Subject: [PATCH] update err lib --- errs/errs.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/errs/errs.go b/errs/errs.go index e898eb0..a85c932 100644 --- a/errs/errs.go +++ b/errs/errs.go @@ -3,6 +3,7 @@ package errs import ( "context" "errors" + "fmt" "log/slog" "os" "reflect" @@ -71,27 +72,37 @@ func (er *Err) Error() string { var ( s = strings.Builder{} source = er.origin - msg = er.msg + ": " + source.Error() + msg = source.Error() + unwrap = errors.Unwrap(source) ) + if unwrap == nil { + if er.msg != "" { + s.WriteString(er.msg) + s.WriteString(": ") + } + s.WriteString(msg) + return s.String() + } for unwrap := errors.Unwrap(source); unwrap != nil; source = unwrap { originMsg := unwrap.Error() - // TODO: Test this! + var write string if cut, found := strings.CutSuffix(msg, originMsg); found { - s.WriteString(cut) - msg = originMsg + write = cut } else { - s.WriteString(msg) + write = msg + } + msg = originMsg + if write != "" { + s.WriteString(write) + s.WriteString(": ") } - - s.WriteString(": ") - - unwrap = errors.Unwrap(unwrap) } return s.String() } func (er *Err) Message(msg string, args ...any) Error { - panic("not implemented") // TODO: Implement + er.msg = fmt.Sprintf(msg, args...) + return er } func (er *Err) GetMessage() string {