75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package zerr
|
|
|
|
import (
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/pborman/indent"
|
|
)
|
|
|
|
// WriteBuilder writes the error to the builder.
|
|
//
|
|
// w is guaranteed to never return error because
|
|
// the underlying most writer is a strings.Builder.
|
|
func (er *Err) WriteBuilder(w io.Writer) {
|
|
if len(er.errs) == 0 {
|
|
_, _ = io.WriteString(w, "[nil]")
|
|
return
|
|
}
|
|
var previous string
|
|
if err := er.sequence.Outer(); err != nil && !err.IsMulti() {
|
|
previous = err.GetMessage()
|
|
}
|
|
current, _ := strings.CutPrefix(er.message, previous)
|
|
if current != "" {
|
|
if previous != "" {
|
|
_, _ = io.WriteString(w, ": ")
|
|
}
|
|
_, _ = io.WriteString(w, current)
|
|
}
|
|
|
|
if len(er.errs) == 1 {
|
|
err := er.errs[0]
|
|
if wb, ok := err.(interface{ WriteBuilder(io.Writer) }); ok {
|
|
wb.WriteBuilder(w)
|
|
return
|
|
}
|
|
_, _ = io.WriteString(w, ": ")
|
|
_, _ = io.WriteString(w, err.Error())
|
|
return
|
|
}
|
|
|
|
_, _ = io.WriteString(w, ":\n")
|
|
w = indent.NewWriter(w, " ")
|
|
|
|
for i, e := range er.errs {
|
|
if i > 0 {
|
|
_, _ = io.WriteString(w, "\n")
|
|
}
|
|
_, _ = io.WriteString(w, strconv.Itoa(i+1))
|
|
_, _ = io.WriteString(w, ". ")
|
|
if wb, ok := e.(interface{ WriteBuilder(io.Writer) }); ok {
|
|
wb.WriteBuilder(w)
|
|
continue
|
|
}
|
|
_, _ = io.WriteString(w, e.Error())
|
|
_, _ = io.WriteString(w, "\n")
|
|
}
|
|
}
|
|
|
|
func writeString(w io.Writer, s string) (n int64, err error) {
|
|
n64, err := io.WriteString(w, s)
|
|
return int64(n64), err
|
|
}
|
|
|
|
func writeByte(w io.Writer, b byte) (n int64, err error) {
|
|
n64, err := w.Write([]byte{b})
|
|
return int64(n64), err
|
|
}
|
|
|
|
func writeBytes(w io.Writer, b []byte) (n int64, err error) {
|
|
n64, err := w.Write(b)
|
|
return int64(n64), err
|
|
}
|