Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetCode ¶
GetCode examines an error and its chain of wrapped errors to find the first ErrorTag. Returns UNTAGGED if no tag is found or if the error is nil. The search traverses the error chain using errors.Unwrap until either a tag is found or the chain is exhausted.
Example:
err := errors.New("base error")
withTag := Tag(DATABASE_ERROR)(err)
wrapped := fmt.Errorf("wrapped: %w", withTag)
code, ok := GetCode(wrapped)
Output: DATABASE_ERROR, true
func InternalMessage ¶
InternalMessage extracts all internal messages from an error chain and combines them into a single message for logging purposes. It traverses the error chain from newest to oldest, collecting only the internal descriptions.
This is useful for logging detailed error information without including the full error chain or underlying error messages.
Returns an empty string if:
- The input error is nil
- The error is not a wrapped error
- No internal messages were set in the error chain
func New ¶
New creates a new error with the given message. The message is stored as an internal error detail, not exposed to end users. Optionally accepts a variadic list of Wrapper functions to modify the error's behavior or add metadata.
Example:
err := fault.New("database connection failed")
wrappedErr := fault.New("query error", fault.With("internal message", "user facing message"))
The location where the error was created is automatically captured and stored. The returned error can be further wrapped using fault.Wrap().
func UserFacingMessage ¶
UserFacingMessage extracts all public messages from an error chain and combines them into a single user-safe message. It traverses the error chain from newest to oldest, collecting only the public descriptions that were set using WithDesc.
The function is designed to provide safe, user-friendly error messages that can be returned in API responses or displayed to end users, without exposing sensitive internal details about the error.
The messages are joined with spaces rather than colons (unlike Error()) to create a more readable user-facing message.
Returns an empty string if:
- The input error is nil
- The error is not a wrapped error
- No public messages were set in the error chain
Example usage:
baseErr := fault.New("internal db error",
fault.WithDesc(
"connection timeout to db://internal.example.com",
"The service is temporarily unavailable",
))
wrappedErr := fault.Wrap(baseErr,
fault.WithDesc(
"failed to process user request",
"Please try again later",
))
msg := fault.UserFacingMessage(wrappedErr)
// msg = "Please try again later The service is temporarily unavailable"
Note that only messages set with WithDesc's public parameter are included in the result, maintaining a clear separation between internal error details and user-safe messages.
func Wrap ¶
Wrap applies a series of Wrapper functions to an error while capturing the call location for debugging purposes. If the input error is nil, it returns nil. Multiple wrappers within a single Wrap call are applied to a single wrapped instance for efficiency.
Example:
err := fault.New("database error")
withLocationErr := fault.Wrap(baseErr,
fault.Code(DATABASE_ERROR),
fault.Internal("connection failed"),
fault.Public("Service unavailable"),
)
Types ¶
type Step ¶
Step represents a single frame in an error chain, capturing the internal message and source location where the error was wrapped. Steps are ordered from the root cause to the outermost wrapper when returned by Flatten.
type Wrapper ¶
Wrapper is a function type that transforms one error into another. It's used to build chains of error transformations while preserving the original error context.
func Code ¶
Code creates a new error Wrapper that adds an error code for classification. Use this to categorize errors for consistent handling across your application.
Example:
err := fault.Wrap(baseErr, fault.Code(DATABASE_ERROR))