Files
podman/vendor/github.com/shirou/gopsutil/v4/internal/common/warnings.go
renovate[bot] b309044006 fix(deps): update module github.com/shirou/gopsutil/v4 to v4.25.10
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-11-01 03:08:38 +00:00

54 lines
1.1 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package 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 {
sb.WriteString(fmt.Sprintf("\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))
}