errorhandling

package
v0.0.0-...-a719f44 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2025 License: UPL-1.0 Imports: 16 Imported by: 0

README

Error Handling in Coherence Operator

This package provides standardized error handling patterns for the Coherence Operator. It includes utilities for error wrapping, context addition, and common error scenarios.

Key Components

Error Types
  • OperationError: Represents an error that occurred during an operation. It includes:
    • Operation name
    • Resource name and namespace (optional)
    • Underlying error
    • Context map for additional information
Error Creation
  • NewOperationError: Creates a new operation error
  • NewResourceError: Creates an error for a specific resource
Common Error Scenarios

The package provides helper functions for common error scenarios:

  • NewCreateResourceError: For resource creation failures
  • NewUpdateResourceError: For resource update failures
  • NewDeleteResourceError: For resource deletion failures
  • NewGetResourceError: For resource retrieval failures
  • NewListResourceError: For resource listing failures
  • NewPatchResourceError: For resource patching failures
  • NewReconcileError: For reconciliation failures
  • NewValidationError: For validation failures
  • NewTimeoutError: For timeout failures
  • NewConnectionError: For connection failures
  • NewAuthenticationError: For authentication failures
  • NewAuthorizationError: For authorization failures
Error Wrapping
  • WrapError: Wraps an error with context information
  • WrapErrorf: Wraps an error with formatted context information
  • WithStack: Adds a stack trace to an error
Error Handling
  • ErrorHandler: Handles errors in the reconciliation loop
    • Categorizes errors (Transient, Permanent, Recoverable, Unknown)
    • Updates error tracking information
    • Updates resource status
    • Handles errors based on their category

Usage Examples

Creating Errors
// Create a simple operation error
err := errorhandling.NewOperationError("update_config", originalErr)

// Add context to the error
err.WithContext("resource_type", "ConfigMap").WithContext("retry_count", "3")

// Create a resource-specific error
err := errorhandling.NewResourceError("update", "my-configmap", "default", originalErr)

// Use helper functions for common scenarios
err := errorhandling.NewCreateResourceError("my-pod", "default", originalErr)
Handling Errors
// Create an error handler
errorHandler := errorhandling.NewErrorHandler(client, logger, recorder)

// Handle an error
result, err := errorHandler.HandleError(ctx, originalErr, resource, "Failed to update resource")

// Handle a resource-specific error
result, err := errorHandler.HandleResourceError(ctx, originalErr, resource, "update", "Failed to update resource")

// Retry an operation with context
err := errorHandler.RetryWithContext(ctx, "update", "my-configmap", "default", func() error {
    // Operation that might fail
    return client.Update(ctx, configMap)
})

Best Practices

  1. Always add context to errors: Use WithContext to add relevant information to errors.
  2. Use the appropriate helper function: Choose the most specific helper function for your error scenario.
  3. Handle errors based on their category: Use the ErrorHandler to handle errors appropriately based on their category.
  4. Include stack traces: Use WithStack to add stack traces to errors for better debugging.
  5. Log errors with context: Use the LogAndWrapError functions to log errors with context.

Documentation

Index

Constants

View Source
const (
	// ErrorCategoryTransient represents a transient error that may resolve itself with time
	ErrorCategoryTransient ErrorCategory = "Transient"
	// ErrorCategoryPermanent represents a permanent error that requires manual intervention
	ErrorCategoryPermanent ErrorCategory = "Permanent"
	// ErrorCategoryRecoverable represents an error that can be automatically recovered from
	ErrorCategoryRecoverable ErrorCategory = "Recoverable"
	// ErrorCategoryUnknown represents an error of unknown category
	ErrorCategoryUnknown ErrorCategory = "Unknown"

	// AnnotationLastError is the annotation key for the last error message
	AnnotationLastError = "coherence.oracle.com/last-error"
	// AnnotationErrorCount is the annotation key for the error count
	AnnotationErrorCount = "coherence.oracle.com/error-count"
	// AnnotationLastRecoveryAttempt is the annotation key for the last recovery attempt timestamp
	AnnotationLastRecoveryAttempt = "coherence.oracle.com/last-recovery-attempt"
)

Variables

This section is empty.

Functions

func GetCallerInfo

func GetCallerInfo(skip int) string

GetCallerInfo returns the file and line number of the caller

func IsAlreadyExists

func IsAlreadyExists(err error) bool

IsAlreadyExists returns true if the error is an already exists error

func IsConflict

func IsConflict(err error) bool

IsConflict returns true if the error is a conflict error

func IsForbidden

func IsForbidden(err error) bool

IsForbidden returns true if the error is a forbidden error

func IsInvalid

func IsInvalid(err error) bool

IsInvalid returns true if the error is an invalid error

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the error is a not found error

func IsServerTimeout

func IsServerTimeout(err error) bool

IsServerTimeout returns true if the error is a server timeout error

func IsServiceUnavailable

func IsServiceUnavailable(err error) bool

IsServiceUnavailable returns true if the error is a service unavailable error

func IsTimeout

func IsTimeout(err error) bool

IsTimeout returns true if the error is a timeout error

func IsTooManyRequests

func IsTooManyRequests(err error) bool

IsTooManyRequests returns true if the error is a too many requests error

func MustNotError

func MustNotError(err error)

MustNotError panics if the error is not nil

func NewAuthenticationError

func NewAuthenticationError(resource string, err error) error

NewAuthenticationError creates an error for authentication failures

func NewAuthorizationError

func NewAuthorizationError(resource string, err error) error

NewAuthorizationError creates an error for authorization failures

func NewConnectionError

func NewConnectionError(endpoint string, err error) error

NewConnectionError creates an error for connection failures

func NewCreateResourceError

func NewCreateResourceError(resource string, namespace string, err error) error

NewCreateResourceError creates an error for resource creation failures

func NewDeleteResourceError

func NewDeleteResourceError(resource string, namespace string, err error) error

NewDeleteResourceError creates an error for resource deletion failures

func NewError

func NewError(message string) error

NewError creates a new error with the given message

func NewErrorf

func NewErrorf(format string, args ...interface{}) error

NewErrorf creates a new error with the given formatted message

func NewGetResourceError

func NewGetResourceError(resource string, namespace string, err error) error

NewGetResourceError creates an error for resource retrieval failures

func NewListResourceError

func NewListResourceError(resource string, namespace string, err error) error

NewListResourceError creates an error for resource listing failures

func NewPatchResourceError

func NewPatchResourceError(resource string, namespace string, err error) error

NewPatchResourceError creates an error for resource patching failures

func NewReconcileError

func NewReconcileError(resource string, namespace string, err error) error

NewReconcileError creates an error for reconciliation failures

func NewTimeoutError

func NewTimeoutError(operation string, resource string, namespace string, err error) error

NewTimeoutError creates an error for timeout failures

func NewUpdateResourceError

func NewUpdateResourceError(resource string, namespace string, err error) error

NewUpdateResourceError creates an error for resource update failures

func NewValidationError

func NewValidationError(resource string, namespace string, err error) error

NewValidationError creates an error for validation failures

func WithStack

func WithStack(err error) error

WithStack adds a stack trace to the error if it doesn't already have one

func WrapError

func WrapError(err error, message string) error

WrapError wraps an error with context information

func WrapErrorf

func WrapErrorf(err error, format string, args ...interface{}) error

WrapErrorf wraps an error with formatted context information

Types

type ErrorCategory

type ErrorCategory string

ErrorCategory represents the category of an error

type ErrorHandler

type ErrorHandler struct {
	Client        client.Client
	Log           logr.Logger
	EventRecorder record.EventRecorder
}

ErrorHandler handles errors in the reconciliation loop

func NewErrorHandler

func NewErrorHandler(client client.Client, log logr.Logger, recorder record.EventRecorder) *ErrorHandler

NewErrorHandler creates a new ErrorHandler

func (*ErrorHandler) HandleError

func (eh *ErrorHandler) HandleError(ctx context.Context, err error, resource coh.CoherenceResource, msg string) (reconcile.Result, error)

HandleError handles an error in the reconciliation loop

func (*ErrorHandler) HandleResourceError

func (eh *ErrorHandler) HandleResourceError(ctx context.Context, err error, resource coh.CoherenceResource, operation string, msg string) (reconcile.Result, error)

HandleResourceError handles an error related to a specific resource

func (*ErrorHandler) LogAndReturnError

func (eh *ErrorHandler) LogAndReturnError(err error, msg string) error

LogAndReturnError logs the error and returns it

func (*ErrorHandler) LogAndWrapError

func (eh *ErrorHandler) LogAndWrapError(err error, msg string) error

LogAndWrapError logs the error, wraps it with the message, and returns it

func (*ErrorHandler) LogAndWrapErrorf

func (eh *ErrorHandler) LogAndWrapErrorf(err error, format string, args ...interface{}) error

LogAndWrapErrorf logs the error, wraps it with the formatted message, and returns it

func (*ErrorHandler) RetryOnError

func (eh *ErrorHandler) RetryOnError(operation string, fn func() error) error

RetryOnError retries the given function with exponential backoff on error

func (*ErrorHandler) RetryWithContext

func (eh *ErrorHandler) RetryWithContext(ctx context.Context, operation string, resource string, namespace string, fn func() error) error

RetryWithContext retries the given function with context and additional metadata

type OperationError

type OperationError struct {
	Operation string
	Resource  string
	Namespace string
	Err       error
	Context   map[string]string
}

OperationError represents an error that occurred during an operation

func NewOperationError

func NewOperationError(operation string, err error) *OperationError

NewOperationError creates a new OperationError

func NewResourceError

func NewResourceError(operation string, resource string, namespace string, err error) *OperationError

NewResourceError creates a new OperationError for a specific resource

func (*OperationError) Cause

func (e *OperationError) Cause() error

Cause returns the underlying error (for compatibility with github.com/pkg/errors)

func (*OperationError) Error

func (e *OperationError) Error() string

Error returns the error message

func (*OperationError) Unwrap

func (e *OperationError) Unwrap() error

Unwrap returns the underlying error

func (*OperationError) WithContext

func (e *OperationError) WithContext(key, value string) *OperationError

WithContext adds context to the error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL