fix: error formatting

This commit is contained in:
yusing 2025-05-04 07:12:28 +08:00
parent 7552a706a7
commit 5fce4b445b
3 changed files with 41 additions and 45 deletions

View file

@ -14,6 +14,9 @@ func Multiline() *MultilineError {
} }
func (m *MultilineError) add(err error) { func (m *MultilineError) add(err error) {
if err == nil {
return
}
m.Extras = append(m.Extras, err) m.Extras = append(m.Extras, err)
} }

View file

@ -68,75 +68,58 @@ func (err *nestedError) Is(other error) bool {
var nilError = newError("<nil>") var nilError = newError("<nil>")
var bulletPrefix = []byte("• ") var bulletPrefix = []byte("• ")
var markdownBulletPrefix = []byte("- ") var markdownBulletPrefix = []byte("- ")
var spaces = []byte(" ") var spaces = []byte(" ")
type appendLineFunc func(buf []byte, err error, level int) []byte type appendLineFunc func(buf []byte, err error, level int) []byte
func (err *nestedError) Error() string { func (err *nestedError) fmtError(appendLine appendLineFunc) []byte {
if err.Err == nil { if err == nil {
return nilError.Error() return appendLine(nil, nilError, 0)
} }
buf := appendLineNormal(nil, err.Err, 0) if err.Err != nil {
if len(err.Extras) > 0 { buf := appendLine(nil, err.Err, 0)
buf = append(buf, '\n') buf = append(buf, '\n')
buf = appendLines(buf, err.Extras, 1, appendLineNormal) buf = appendLines(buf, err.Extras, 1, appendLine)
return buf
} }
return string(buf) return appendLines(nil, err.Extras, 0, appendLine)
}
func (err *nestedError) Error() string {
return string(err.fmtError(appendLineNormal))
} }
func (err *nestedError) Plain() []byte { func (err *nestedError) Plain() []byte {
if err.Err == nil { return err.fmtError(appendLinePlain)
return appendLinePlain(nil, nilError, 0)
}
buf := appendLinePlain(nil, err.Err, 0)
if len(err.Extras) > 0 {
buf = append(buf, '\n')
buf = appendLines(buf, err.Extras, 1, appendLinePlain)
}
return buf
} }
func (err *nestedError) Markdown() []byte { func (err *nestedError) Markdown() []byte {
if err.Err == nil { return err.fmtError(appendLineMd)
return appendLineMd(nil, nilError, 0) }
}
buf := appendLineMd(nil, err.Err, 0) func appendLine(buf []byte, err error, level int, prefix []byte, format func(err error) []byte) []byte {
if len(err.Extras) > 0 { if err == nil {
buf = append(buf, '\n') return appendLine(buf, nilError, level, prefix, format)
buf = appendLines(buf, err.Extras, 1, appendLineMd)
} }
if level == 0 {
return append(buf, format(err)...)
}
buf = append(buf, spaces[:2*level]...)
buf = append(buf, prefix...)
buf = append(buf, format(err)...)
return buf return buf
} }
func appendLineNormal(buf []byte, err error, level int) []byte { func appendLineNormal(buf []byte, err error, level int) []byte {
if level == 0 { return appendLine(buf, err, level, bulletPrefix, Normal)
return append(buf, err.Error()...)
}
buf = append(buf, spaces[:2*level]...)
buf = append(buf, bulletPrefix...)
buf = append(buf, err.Error()...)
return buf
} }
func appendLinePlain(buf []byte, err error, level int) []byte { func appendLinePlain(buf []byte, err error, level int) []byte {
if level == 0 { return appendLine(buf, err, level, bulletPrefix, Plain)
return append(buf, Plain(err)...)
}
buf = append(buf, spaces[:2*level]...)
buf = append(buf, bulletPrefix...)
buf = append(buf, Plain(err)...)
return buf
} }
func appendLineMd(buf []byte, err error, level int) []byte { func appendLineMd(buf []byte, err error, level int) []byte {
if level == 0 { return appendLine(buf, err, level, markdownBulletPrefix, Markdown)
return append(buf, Markdown(err)...)
}
buf = append(buf, spaces[:2*level]...)
buf = append(buf, markdownBulletPrefix...)
buf = append(buf, Markdown(err)...)
return buf
} }
func appendLines(buf []byte, errs []error, level int, appendLine appendLineFunc) []byte { func appendLines(buf []byte, errs []error, level int, appendLine appendLineFunc) []byte {
@ -154,6 +137,9 @@ func appendLines(buf []byte, errs []error, level int, appendLine appendLineFunc)
buf = appendLines(buf, err.Extras, level, appendLine) buf = appendLines(buf, err.Extras, level, appendLine)
} }
default: default:
if err == nil {
continue
}
buf = appendLine(buf, err, level) buf = appendLine(buf, err, level)
buf = append(buf, '\n') buf = append(buf, '\n')
} }

View file

@ -91,6 +91,13 @@ func Collect[T any, Err error, Arg any, Func func(Arg) (T, Err)](eb *Builder, fn
return result return result
} }
func Normal(err error) []byte {
if err == nil {
return nil
}
return []byte(err.Error())
}
func Plain(err error) []byte { func Plain(err error) []byte {
if err == nil { if err == nil {
return nil return nil