You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.1 KiB

// =============================================================================
// Active-GO Framework
// Copyright (c) 2024 ActiveThing (https://activething.com)
// Author: Juan V. Navarro juanvnl@activething.com
// =============================================================================
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// =============================================================================
package errs
type (
// Causers is an interface for types that can provide multiple underlying causes
// of an error. This is useful in scenarios where an error condition might have
// resulted from multiple underlying issues or failures.
//
// This interface is particularly useful for:
// - Aggregated error reporting
// - Parallel operation failures
// - Multiple validation errors
// - Complex error scenarios with multiple root causes
Causers interface {
// Causes returns a slice of errors that contributed to this error condition.
// The order of errors in the slice may be significant depending on the
// implementation.
//
// Returns:
// - []error: A slice of underlying errors that caused this error condition.
// May return an empty slice if there are no causes.
Causes() []error
}
// CausesFnc is a function type that implements the Causers interface.
// It allows for dynamic generation of multiple error causes and can be used
// to create error causes lazily or based on runtime conditions.
//
// Common use cases include:
// - Lazy evaluation of error causes
// - Dynamic error aggregation
// - Conditional error collection
CausesFnc func() []error
)
// Causes implements the Causers interface for CausesFnc.
// It executes the function and returns its slice of errors.
//
// Returns:
// - []error: The slice of errors returned by executing the CausesFnc
//
// Example:
//
// causes := CausesFnc(func() []error {
// return []error{
// errors.New("first cause"),
// errors.New("second cause"),
// }
// })
// errs := causes.Causes() // returns slice with both errors
func (f CausesFnc) Causes() []error {
return f()
}

Powered by TurnKey Linux.