Documentation
¶
Index ¶
- Variables
- func AddCustomCallStack(err error, callStack *Stack) error
- func FindFirstErrorWithStack(err error) error
- func FindPublicError(err error) error
- func New(description string) error
- func NewWithStack(description string) error
- func Wrap(err error, description string) error
- func WrapPublic(err error, message string) error
- func WrapWithCustomErr(originalErr, wrappingErr error) error
- func Wrapf(err error, format string, args ...any) error
- func WrapfPublic(err error, format string, args ...any) error
- func WrapfWithCustomErr(originalErr, wrappingErr error, format string, args ...any) error
- type Error
- func (e *Error) Error() string
- func (e *Error) Format(f fmt.State, _ rune)
- func (e *Error) GetCallStack() []string
- func (e *Error) GetOriginalErrorMessage() string
- func (e *Error) GetOriginalPredefinedError() error
- func (e *Error) IsPublic() bool
- func (e *Error) Message() string
- func (e *Error) Unwrap() error
- func (e *Error) Wrap(err error) error
- func (e *Error) Wrapf(format string, err error) error
- type Stack
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Is a wrapper for errors.Is. Is = stdErrors.Is // Unwrap is a wrapper for errors.Unwrap. Unwrap = stdErrors.Unwrap // As is a wrapper for errors.As. As = stdErrors.As // Join is a wrapper for errors.Join. Join = stdErrors.Join // Errorf is a wrapper for fmt.Errorf. Errorf = fmt.Errorf //nolint:gochecknoglobals )
var ( ErrBadRequest = New("bad request") // HTTP 400 ErrRegistrationRequired = New("registration required") // HTTP 401 ErrPaymentError = New("payment error") // HTTP 402 ErrForbiddenAction = New("forbidden") // HTTP 403 ErrNotFound = New("entity not found") // HTTP 404 ErrConflict = New("conflict request") // HTTP 409 ErrPreconditionFailed = New("precondition failed") // HTTP 412 ErrValidation = New("validation failed") // HTTP 422 ErrInternalServerError = New("internal server error") // HTTP 500 )
errors...
Functions ¶
func AddCustomCallStack ¶
AddCustomCallStack wraps the given error with a custom call stack and returns a new error that includes both. It preserves the original error message while providing additional call stack context useful for debugging.
Parameters:
- err: The original error to wrap. If nil, returns nil.
- callStack: The custom call stack to attach to the error. Must be a valid Stack pointer.
Returns:
- A new Error that embeds both the original error and the provided call stack.
- nil if the input error is nil.
func FindFirstErrorWithStack ¶
FindFirstErrorWithStack traverses an error chain to locate the first framework-specific error.
Parameters:
- err: the root error to traverse
Returns:
- *Error: the first framework-specific error in the chain, or nil if not found
func FindPublicError ¶ added in v1.0.3
FindPublicError traverses an error chain to locate the first public error that is safe to display to end users.
Parameters:
- err: the root error to search through
Returns:
- error: the first public error in the chain, or nil if none are found
Example ¶
ExampleFindPublicError demonstrates finding the first public error in an error chain.
package main
import (
"fmt"
"github.com/ceearrashee/errors"
)
func main() {
// Create a chain of errors with one public error
internalErr := errors.New("internal database error")
wrappedErr := errors.Wrap(internalErr, "query failed")
publicErr := errors.WrapPublic(wrappedErr, "Service temporarily unavailable")
finalErr := errors.Wrap(publicErr, "handler error")
// Find the public error
var frameworkErr *errors.Error
if errors.As(errors.FindPublicError(finalErr), &frameworkErr) {
fmt.Println("Found public error:", frameworkErr.Message())
}
}
Output: Found public error: Service temporarily unavailable
func New ¶
New creates a new Error instance with the specified description.
Parameters:
- description: a text message describing the error.
Returns:
- error: an Error instance encapsulating the provided description.
func NewWithStack ¶
NewWithStack creates a new error with a description and captures the current call stack.
Parameters:
- description: the error description
Returns:
- error: a newly created error with stack trace included
func Wrap ¶
Wrap wraps an existing error with additional context and a stack trace.
Parameters:
- err: the original error to wrap
- description: a description providing context for the error
Returns:
- error: a wrapped error with the original error, description, and stack trace, or nil if the input error is nil
func WrapPublic ¶ added in v1.0.3
WrapPublic wraps an existing error with a public message that is safe to display to end users.
Parameters:
- err: the original error to wrap
- message: a user-friendly public message
Returns:
- error: a wrapped error marked as public with the original error, message, and stack trace, or nil if the input error is nil
Example ¶
ExampleWrapPublic demonstrates wrapping an error with a public message.
package main
import (
"fmt"
"github.com/ceearrashee/errors"
)
func main() {
internalErr := errors.New("database connection failed")
publicErr := errors.WrapPublic(internalErr, "Unable to connect to service")
var frameworkErr *errors.Error
if errors.As(publicErr, &frameworkErr) {
fmt.Println("Is public:", frameworkErr.IsPublic())
fmt.Println("Message:", frameworkErr.Message())
}
}
Output: Is public: true Message: Unable to connect to service
func WrapWithCustomErr ¶
WrapWithCustomErr wraps an original error with a custom error, maintaining context and a call stack.
Parameters:
- originalErr: the error to be wrapped
- wrappingErr: the error providing additional context
Returns:
- error: a new error combining the original and custom errors, or nil if the original error is nil
func Wrapf ¶
Wrapf logs the given error with a formatted message and wraps the error with the same message.
func WrapfPublic ¶ added in v1.0.3
WrapfPublic wraps an existing error with a formatted public message that is safe to display to end users.
Parameters:
- err: the original error to wrap
- format: a format string for the public message
- args: optional arguments for formatting the message
Returns:
- error: a wrapped error marked as public with formatted message and stack trace, or nil if the input error is nil
Example ¶
ExampleWrapfPublic demonstrates wrapping an error with a formatted public message.
package main
import (
"fmt"
"github.com/ceearrashee/errors"
)
func main() {
internalErr := errors.New("SQL error: syntax error near line 42")
publicErr := errors.WrapfPublic(internalErr, "Failed to process request #%d", 12345)
var frameworkErr *errors.Error
if errors.As(publicErr, &frameworkErr) {
fmt.Println("Is public:", frameworkErr.IsPublic())
fmt.Println("Message:", frameworkErr.Message())
}
}
Output: Is public: true Message: Failed to process request #12345
func WrapfWithCustomErr ¶
WrapfWithCustomErr creates a new Error instance by wrapping an original error with a custom error and formatted message.
Parameters:
- originalErr: the error to wrap; if nil, the function returns nil
- wrappingErr: the error to use for wrapping the original error
- format: a format string for the custom error description
- args: optional arguments for formatting the description
Returns:
- error: an Error with formatted description and wrapped errors, or nil if the original error is nil
Types ¶
type Error ¶
type Error struct {
Description string
// contains filtered or unexported fields
}
Error represents an error structure that includes a description and an error string. It is useful for providing additional context or user-friendly messages alongside the error text.
func FindOriginalErrorWithStack ¶
FindOriginalErrorWithStack traverses an error chain to locate the latest framework error containing a call stack.
Parameters:
- err: the root error to search through
Returns:
- *Error: the last framework error containing a call stack, or nil if none are found
func Newf ¶
Newf creates a new Error instance with a formatted description.
Parameters:
- formatedDescription: a string with formatting placeholders for the description
- args: values to replace the placeholders in the formatted description
Returns:
- *Error: a pointer to the newly created Error instance with the formatted description set.
func (*Error) Error ¶
Error returns the error message, combining the description and underlying error if present.
Returns:
- string: the error message, formatted as a string.
func (*Error) Format ¶
Format customizes the formatted output of an Error instance.
Parameters:
- f: the formatter state used for custom formatting
- _: the rune specifying the format verb (unused)
Returns: none (writes the formatted description to f)
func (*Error) GetCallStack ¶
GetCallStack retrieves the function call stack associated with the error.
Returns:
- []string: a slice of formatted call stack frames as strings, in order from most to least recent.
func (*Error) GetOriginalErrorMessage ¶
GetOriginalErrorMessage returns the deepest error message in the error chain, optionally prefixed by the error's description.
Returns:
- string: the error message, formatted as a string.
func (*Error) GetOriginalPredefinedError ¶
GetOriginalPredefinedError retrieves the first predefined error in the error chain if any exist.
Returns:
- error: the first predefined error in the chain, or the original error if no predefined error is found.
func (*Error) IsPublic ¶ added in v1.0.3
IsPublic returns whether this error message is safe to display to end users.
Returns:
- bool: true if the error is marked as public, false otherwise.
func (*Error) Message ¶
Message returns the description if set; otherwise, it returns the underlying error's message.
Returns:
- string: the error message, formatted as a string.
func (*Error) Unwrap ¶
Unwrap returns the wrapped error, enabling error unwrapping in chains and supporting the errors.Unwrap interface.
func (*Error) Wrap ¶
Wrap adds context to an existing error using the Error's description.
Parameters:
- err: the error to wrap; if nil or no description is provided, returns nil.
Returns:
- error: a new Error instance incorporating the provided error.
Errors:
- None directly, but may wrap any provided error with additional context.
func (*Error) Wrapf ¶
Wrapf formats and wraps an existing error with the Error's description and a custom message.
Parameters:
- format: a format string for the error message
- err: the error to wrap; if nil or no description is provided, returns nil
Returns:
- error: a new error combining the format, description, and wrapped error
Errors:
- None directly, but wraps the provided error with formatted context, if present.