errs: fix duplicate error message

This commit is contained in:
Tigor Hutasuhut 2024-05-14 12:07:07 +07:00
parent 6cea6394f3
commit 8698d61e1d

View file

@ -68,25 +68,34 @@ func (er *Err) LogValue() slog.Value {
return slog.GroupValue(values...) return slog.GroupValue(values...)
} }
func (er *Err) Error() string { func (e *Err) Error() string {
s := strings.Builder{} s := strings.Builder{}
if er.message != "" { if e.message != "" {
s.WriteString(er.message) s.WriteString(e.message)
if er.origin != nil { if e.origin != nil {
s.WriteString(": ") s.WriteString(": ")
} }
} }
for unwrap := errors.Unwrap(er); unwrap != nil; { unwrap := errors.Unwrap(e)
for unwrap != nil {
var current string
if e, ok := unwrap.(Error); ok && e.GetMessage() != "" { if e, ok := unwrap.(Error); ok && e.GetMessage() != "" {
s.WriteString(e.GetMessage()) current = e.GetMessage()
s.WriteString(": ") } else {
continue current = unwrap.Error()
} }
s.WriteString(unwrap.Error())
next := errors.Unwrap(unwrap) next := errors.Unwrap(unwrap)
if next != nil {
current, _ = strings.CutSuffix(current, next.Error())
current, _ = strings.CutSuffix(current, ": ")
}
if current != "" {
s.WriteString(current)
if next != nil { if next != nil {
s.WriteString(": ") s.WriteString(": ")
} }
}
unwrap = next unwrap = next
} }
return s.String() return s.String()