2024-08-23 18:41:19 +07:00
|
|
|
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
|
2024-08-26 16:24:41 +07:00
|
|
|
// the bottom most writer is a strings.Builder.
|
2024-08-23 18:41:19 +07:00
|
|
|
func (er *Err) WriteBuilder(w io.Writer) {
|
|
|
|
if len(er.errs) == 0 {
|
|
|
|
_, _ = io.WriteString(w, "[nil]")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var previous string
|
2024-08-27 17:08:52 +07:00
|
|
|
if parent := er.sequence.Parent(); parent != nil {
|
|
|
|
if err := parent.Error(); err != nil && !err.IsMulti() {
|
|
|
|
previous = err.GetMessage()
|
|
|
|
}
|
2024-08-23 18:41:19 +07:00
|
|
|
}
|
2024-08-26 16:24:41 +07:00
|
|
|
current := strings.TrimPrefix(er.message, previous)
|
2024-08-23 18:41:19 +07:00
|
|
|
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
|
|
|
|
}
|