Something went wrong. Try again.
forked from shirou/gopsutil
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354// SPDX-License-Identifier: BSD-3-Clausepackage common
import ( "fmt" "strings")
const ( maxWarnings = 100 // An arbitrary limit to avoid excessive memory usage, it has no sense to store hundreds of errors tooManyErrorsMessage = "too many errors reported, next errors were discarded" numberOfWarningsMessage = "Number of warnings:")
type Warnings struct { List []error tooManyErrors bool Verbose bool}
func (w *Warnings) Add(err error) { if len(w.List) >= maxWarnings { w.tooManyErrors = true return } w.List = append(w.List, err)}
func (w *Warnings) Reference() error { if len(w.List) > 0 { return w } return nil}
func (w *Warnings) Error() string { if w.Verbose { str := "" var sb strings.Builder for i, e := range w.List { fmt.Fprintf(&sb, "\tError %d: %s\n", i, e.Error()) } str += sb.String() if w.tooManyErrors { str += fmt.Sprintf("\t%s\n", tooManyErrorsMessage) } return str } if w.tooManyErrors { return fmt.Sprintf("%s > %v - %s", numberOfWarningsMessage, maxWarnings, tooManyErrorsMessage) } return fmt.Sprintf("%s %v", numberOfWarningsMessage, len(w.List))}