Documentation
¶
Overview ¶
Package errbuf provides a thread-safe buffer for accumulating multiple errors. It implements the standard error interface and supports custom formatting for single-line or multi-line display of the aggregated errors.
Index ¶
- type BufferedError
- func (b *BufferedError) Add(errs ...error)
- func (b *BufferedError) As(target any) bool
- func (b *BufferedError) Clear()
- func (b *BufferedError) Err() error
- func (b *BufferedError) Error() string
- func (b *BufferedError) Format(f fmt.State, verb rune)
- func (b *BufferedError) Grow(n int)
- func (b *BufferedError) Is(target error) bool
- func (b *BufferedError) Unwrap() []error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufferedError ¶
BufferedError represents an aggregated collection of errors. It is safe for concurrent use by multiple goroutines. The zero value is an empty, ready-to-use error buffer.
func New ¶ added in v1.2.0
func New() *BufferedError
New creates and returns an empty, ready-to-use BufferedError.
func NewFromError ¶ added in v1.2.0
func NewFromError(err error) *BufferedError
NewFromError creates a new BufferedError initialized with the given error. If the given err is nil, it returns an empty BufferedError.
func (*BufferedError) Add ¶
func (b *BufferedError) Add(errs ...error)
Add appends one or more errors to the buffer. Nil errors are ignored and will not be added to the buffer.
func (*BufferedError) As ¶ added in v1.3.0
func (b *BufferedError) As(target any) bool
As finds the first error in b's buffer that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.
func (*BufferedError) Clear ¶ added in v1.2.0
func (b *BufferedError) Clear()
Clear empties the internal buffer. To avoid costly reallocations, the underlying backing array is retained. Elements are nil'd out so the garbage collector can reclaim the error objects safely.
func (*BufferedError) Err ¶
func (b *BufferedError) Err() error
Err returns the BufferedError itself if it contains one or more errors. If the buffer is empty, it returns nil. This is useful for conditional error returning like: `if err := buf.Err(); err != nil { return err }`.
func (*BufferedError) Error ¶
func (b *BufferedError) Error() string
Error returns a string representation of all buffered errors. If the buffer is empty, it returns an empty string. If the buffer contains exactly one error, it returns that error's string representation. For two or more errors, they are joined by a semicolon separator ("; ").
func (*BufferedError) Format ¶ added in v1.2.0
func (b *BufferedError) Format(f fmt.State, verb rune)
Format implements the fmt.Formatter interface to support custom error formatting.
The following format verbs are supported:
%s Prints errors on one line separated by semicolons (e.g. "err1; err2").
%v Prints errors separated by newlines (multiline).
%+v Prints the raw internal representation: "BufferedError{errors:[...]}".
func (*BufferedError) Grow ¶ added in v1.2.0
func (b *BufferedError) Grow(n int)
Grow increases the internal slice capacity to accommodate n errors. If the internal capacity is already greater than or equal to n, this is a no-op. This is useful for optimizing allocations when the number of errors to be added is known in advance.
func (*BufferedError) Is ¶ added in v1.3.0
func (b *BufferedError) Is(target error) bool
Is reports whether any error in b's buffer matches target.
func (*BufferedError) Unwrap ¶
func (b *BufferedError) Unwrap() []error
Unwrap returns a shallow copy of the underlying slice of accumulated errors. Modifications to the returned slice will not affect the buffer.