update err lib

This commit is contained in:
Tigor Hutasuhut 2024-04-06 01:33:03 +07:00
parent cacb699718
commit d02962dc65

View file

@ -3,6 +3,7 @@ package errs
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"log/slog" "log/slog"
"os" "os"
"reflect" "reflect"
@ -71,27 +72,37 @@ func (er *Err) Error() string {
var ( var (
s = strings.Builder{} s = strings.Builder{}
source = er.origin 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 { for unwrap := errors.Unwrap(source); unwrap != nil; source = unwrap {
originMsg := unwrap.Error() originMsg := unwrap.Error()
// TODO: Test this! var write string
if cut, found := strings.CutSuffix(msg, originMsg); found { if cut, found := strings.CutSuffix(msg, originMsg); found {
s.WriteString(cut) write = cut
msg = originMsg
} else { } else {
s.WriteString(msg) write = msg
}
msg = originMsg
if write != "" {
s.WriteString(write)
s.WriteString(": ")
} }
s.WriteString(": ")
unwrap = errors.Unwrap(unwrap)
} }
return s.String() return s.String()
} }
func (er *Err) Message(msg string, args ...any) Error { 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 { func (er *Err) GetMessage() string {