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.

68 lines
2.8 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
// Causer is an interface for types that can provide an underlying cause of an error.
// It is commonly used in error wrapping and error chain scenarios to access the
// root cause of an error condition.
type (
// Causer defines the interface for error types that can reveal their underlying cause.
// This interface is useful for implementing error wrapping patterns and
// for building error chains that preserve cause information.
Causer interface {
// Cause returns the underlying error that caused this error, if any.
// If there is no underlying error, it may return nil.
//
// Returns:
// - error: The underlying error that caused this error, or nil if none exists
Cause() error
}
// CauseFnc is a function type that implements the Causer interface.
// It allows for dynamic generation of error causes and can be used
// to create error causes lazily or based on runtime conditions.
//
// The function is called when ErrWithCause() is invoked on the CauseFnc.
CauseFnc func() error
)
// Cause implements the Causer interface for CauseFnc.
// It executes the function and returns its error result.
//
// Returns:
// - error: The result of executing the CauseFnc
//
// Example:
//
// cause := CauseFnc(func() error {
// return errors.New("root cause")
// })
// err := cause.ErrWithCause() // returns "root cause" error
func (f CauseFnc) Cause() error {
return f()
}

Powered by TurnKey Linux.