Documentation
¶
Overview ¶
Package errors provides structured error handling utilities with actionable suggestions.
This package extracts common error handling patterns to provide reusable, testable components for creating, wrapping, aggregating, and formatting errors with rich context and user-friendly suggestions.
Key interfaces:
- ErrorHandler: Handles and formats errors with suggestions
- ErrorWrapper: Wraps errors with additional context
- ErrorAggregator: Collects and manages multiple errors
Key types:
- StructuredError: Rich error type with categorization, context, and suggestions
- ValidationResult: Result of validation operations with errors and warnings
- ErrorCollection: Collection of multiple errors
The package supports error categorization, context preservation, suggestion generation, and specialized aggregators for validation and multi-field scenarios.
Index ¶
- func Annotate(err error, annotation string) error
- func Chain(errors ...error) error
- func IsFileNotFoundError(err error) bool
- func IsPermissionError(err error) bool
- func IsTimeoutError(err error) bool
- func WrapWithContext(err error, context map[string]interface{}) error
- func WrapWithField(err error, field string) error
- func WrapWithFileContext(err error, filePath string, lineNumber int) error
- func WrapWithFileContextAndColumn(err error, filePath string, lineNumber, columnNumber int) error
- func WrapWithFullContext(err error, filePath string, lineNumber, columnNumber int, operation string) error
- func WrapWithOperation(err error, operation string) error
- func WrapWithSuggestions(err error, suggestions ...string) error
- type CredentialMasker
- type DefaultErrorAggregator
- func (a *DefaultErrorAggregator) AddError(err error)
- func (a *DefaultErrorAggregator) AddErrorWithContext(field string, err error)
- func (a *DefaultErrorAggregator) Clear()
- func (a *DefaultErrorAggregator) Count() int
- func (a *DefaultErrorAggregator) CountByType(errorType ErrorType) int
- func (a *DefaultErrorAggregator) GetErrors() []error
- func (a *DefaultErrorAggregator) GetErrorsByType(errorType ErrorType) []error
- func (a *DefaultErrorAggregator) GetSummary() string
- func (a *DefaultErrorAggregator) HasErrors() bool
- func (a *DefaultErrorAggregator) ToError() error
- type DefaultErrorHandler
- func (h *DefaultErrorHandler) CreateConfigError(message string, cause error) *StructuredError
- func (h *DefaultErrorHandler) CreateFileError(operation, path string, cause error) *StructuredError
- func (h *DefaultErrorHandler) CreateValidationError(field, message string, suggestions ...string) *StructuredError
- func (h *DefaultErrorHandler) FormatError(err error) string
- func (h *DefaultErrorHandler) GetSuggestions(err error) []string
- func (h *DefaultErrorHandler) HandleError(err error) *StructuredError
- func (h *DefaultErrorHandler) IsRetryable(err error) bool
- func (h *DefaultErrorHandler) Wrap(err error, operation, context string) error
- type DefaultErrorWrapper
- func (w *DefaultErrorWrapper) UnwrapError(err error) error
- func (w *DefaultErrorWrapper) WrapError(err error, message string) error
- func (w *DefaultErrorWrapper) WrapErrorWithContext(ctx context.Context, err error, message string) error
- func (w *DefaultErrorWrapper) WrapErrorWithType(err error, errorType ErrorType, message string) error
- type ErrorAggregator
- type ErrorCollection
- type ErrorHandler
- type ErrorMiddleware
- func (m *ErrorMiddleware) GetSuggestions(err error) []string
- func (m *ErrorMiddleware) Handle(ctx context.Context, operation string, fn func() error) error
- func (m *ErrorMiddleware) HandleError(ctx context.Context, operation string, err error) error
- func (m *ErrorMiddleware) HandleValidationErrors(ctx context.Context, operation string, errs []*StructuredError) error
- func (m *ErrorMiddleware) IsRetryable(err error) bool
- func (m *ErrorMiddleware) RecoverPanic(ctx context.Context, operation string) error
- func (m *ErrorMiddleware) WrapCommand(operation string, fn func(ctx context.Context) error) func(ctx context.Context) error
- func (m *ErrorMiddleware) WrapCommandWithArgs(operation string, fn func(ctx context.Context, args []string) error) func(ctx context.Context, args []string) error
- type ErrorType
- type ErrorWrapper
- type Logger
- type MultiFieldAggregator
- func (mfa *MultiFieldAggregator) AddFieldError(field string, err error)
- func (mfa *MultiFieldAggregator) GetAllFieldErrors() map[string][]error
- func (mfa *MultiFieldAggregator) GetFieldErrors(field string) []error
- func (mfa *MultiFieldAggregator) HasAnyErrors() bool
- func (mfa *MultiFieldAggregator) HasFieldErrors(field string) bool
- func (mfa *MultiFieldAggregator) ToError() error
- type StructuredError
- func CreateCloudError(provider, operation, message string, cause error) *StructuredError
- func CreateConfigError(field, message string, cause error) *StructuredError
- func CreateCredentialError(credentialType, field, message string, cause error) *StructuredError
- func CreateFileError(operation, path string, cause error) *StructuredError
- func CreateGenerationError(stage, message string, cause error) *StructuredError
- func CreateGenerationErrorWithFile(stage, filePath string, message string, cause error) *StructuredError
- func CreatePathError(path, message string, cause error) *StructuredError
- func CreatePermissionError(resource, operation string, cause error) *StructuredError
- func CreateSOPSError(operation, message string, cause error) *StructuredError
- func CreateServiceError(serviceName, operation, message string, cause error) *StructuredError
- func CreateTemplateError(templatePath string, lineNumber int, message string, cause error) *StructuredError
- func CreateTemplateErrorWithColumn(templatePath string, lineNumber, columnNumber int, message string, cause error) *StructuredError
- func CreateValidationError(field, message string, suggestions ...string) *StructuredError
- type ValidationAggregator
- func (va *ValidationAggregator) AddWarning(err error)
- func (va *ValidationAggregator) AddWarningWithContext(field string, err error)
- func (va *ValidationAggregator) ClearAll()
- func (va *ValidationAggregator) ClearWarnings()
- func (va *ValidationAggregator) GetWarnings() []error
- func (va *ValidationAggregator) HasWarnings() bool
- func (va *ValidationAggregator) ToValidationResult() *ValidationResult
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsFileNotFoundError ¶
IsFileNotFoundError checks if an error is a file not found error
func IsPermissionError ¶
IsPermissionError checks if an error is a permission error
func IsTimeoutError ¶
IsTimeoutError checks if an error is a timeout error
func WrapWithContext ¶
WrapWithContext wraps an error with additional context information
func WrapWithField ¶
WrapWithField wraps an error with a field context
func WrapWithFileContext ¶
WrapWithFileContext wraps an error with file path and line number information
func WrapWithFileContextAndColumn ¶
WrapWithFileContextAndColumn wraps an error with file path, line number, and column number
func WrapWithFullContext ¶
func WrapWithFullContext(err error, filePath string, lineNumber, columnNumber int, operation string) error
WrapWithFullContext wraps an error with complete context information
func WrapWithOperation ¶
WrapWithOperation wraps an error with operation context
func WrapWithSuggestions ¶
WrapWithSuggestions wraps an error with additional suggestions
Types ¶
type CredentialMasker ¶
CredentialMasker defines the interface for masking sensitive data in error messages. This interface is defined here to avoid import cycles with the security package.
type DefaultErrorAggregator ¶
type DefaultErrorAggregator struct {
// contains filtered or unexported fields
}
DefaultErrorAggregator implements ErrorAggregator interface
func NewDefaultErrorAggregator ¶
func NewDefaultErrorAggregator() *DefaultErrorAggregator
NewDefaultErrorAggregator creates a new default error aggregator
func (*DefaultErrorAggregator) AddError ¶
func (a *DefaultErrorAggregator) AddError(err error)
AddError adds an error to the aggregator
func (*DefaultErrorAggregator) AddErrorWithContext ¶
func (a *DefaultErrorAggregator) AddErrorWithContext(field string, err error)
AddErrorWithContext adds an error with field context to the aggregator
func (*DefaultErrorAggregator) Clear ¶
func (a *DefaultErrorAggregator) Clear()
Clear removes all errors from the aggregator
func (*DefaultErrorAggregator) Count ¶
func (a *DefaultErrorAggregator) Count() int
Count returns the number of errors in the aggregator
func (*DefaultErrorAggregator) CountByType ¶
func (a *DefaultErrorAggregator) CountByType(errorType ErrorType) int
CountByType returns the number of errors of a specific type
func (*DefaultErrorAggregator) GetErrors ¶
func (a *DefaultErrorAggregator) GetErrors() []error
GetErrors returns all errors in the aggregator
func (*DefaultErrorAggregator) GetErrorsByType ¶
func (a *DefaultErrorAggregator) GetErrorsByType(errorType ErrorType) []error
GetErrorsByType returns errors of a specific type
func (*DefaultErrorAggregator) GetSummary ¶
func (a *DefaultErrorAggregator) GetSummary() string
GetSummary returns a summary of all errors
func (*DefaultErrorAggregator) HasErrors ¶
func (a *DefaultErrorAggregator) HasErrors() bool
HasErrors returns true if there are any errors in the aggregator
func (*DefaultErrorAggregator) ToError ¶
func (a *DefaultErrorAggregator) ToError() error
ToError converts the aggregated errors to a single error
type DefaultErrorHandler ¶
type DefaultErrorHandler struct {
// contains filtered or unexported fields
}
DefaultErrorHandler implements ErrorHandler interface
func NewDefaultErrorHandler ¶
func NewDefaultErrorHandler(masker CredentialMasker) *DefaultErrorHandler
NewDefaultErrorHandler creates a new default error handler with the provided credential masker. If you don't have a credential masker available, use NewDefaultErrorHandlerWithoutMasking instead.
func NewDefaultErrorHandlerWithoutMasking ¶
func NewDefaultErrorHandlerWithoutMasking() *DefaultErrorHandler
NewDefaultErrorHandlerWithoutMasking creates a new default error handler without credential masking. This is useful in contexts where the security package cannot be imported due to import cycles. For production use, prefer NewDefaultErrorHandler with a proper CredentialMasker.
func (*DefaultErrorHandler) CreateConfigError ¶
func (h *DefaultErrorHandler) CreateConfigError(message string, cause error) *StructuredError
CreateConfigError creates a configuration-related error
func (*DefaultErrorHandler) CreateFileError ¶
func (h *DefaultErrorHandler) CreateFileError(operation, path string, cause error) *StructuredError
CreateFileError creates a file operation error with context
func (*DefaultErrorHandler) CreateValidationError ¶
func (h *DefaultErrorHandler) CreateValidationError(field, message string, suggestions ...string) *StructuredError
CreateValidationError creates a validation error with suggestions
func (*DefaultErrorHandler) FormatError ¶
func (h *DefaultErrorHandler) FormatError(err error) string
FormatError formats an error for display
func (*DefaultErrorHandler) GetSuggestions ¶
func (h *DefaultErrorHandler) GetSuggestions(err error) []string
GetSuggestions returns suggestions for resolving an error
func (*DefaultErrorHandler) HandleError ¶
func (h *DefaultErrorHandler) HandleError(err error) *StructuredError
HandleError converts an error to a structured error
func (*DefaultErrorHandler) IsRetryable ¶
func (h *DefaultErrorHandler) IsRetryable(err error) bool
IsRetryable determines if an error is retryable
type DefaultErrorWrapper ¶
type DefaultErrorWrapper struct{}
DefaultErrorWrapper implements ErrorWrapper interface
func NewDefaultErrorWrapper ¶
func NewDefaultErrorWrapper() *DefaultErrorWrapper
NewDefaultErrorWrapper creates a new default error wrapper
func (*DefaultErrorWrapper) UnwrapError ¶
func (w *DefaultErrorWrapper) UnwrapError(err error) error
UnwrapError unwraps an error to get the underlying cause
func (*DefaultErrorWrapper) WrapError ¶
func (w *DefaultErrorWrapper) WrapError(err error, message string) error
WrapError wraps an error with additional context message
func (*DefaultErrorWrapper) WrapErrorWithContext ¶
func (w *DefaultErrorWrapper) WrapErrorWithContext(ctx context.Context, err error, message string) error
WrapErrorWithContext wraps an error with context information
func (*DefaultErrorWrapper) WrapErrorWithType ¶
func (w *DefaultErrorWrapper) WrapErrorWithType(err error, errorType ErrorType, message string) error
WrapErrorWithType wraps an error with a specific error type
type ErrorAggregator ¶
type ErrorAggregator interface {
AddError(err error)
AddErrorWithContext(field string, err error)
HasErrors() bool
GetErrors() []error
GetErrorsByType(errorType ErrorType) []error
Clear()
ToError() error
}
ErrorAggregator interface for collecting and managing multiple errors
type ErrorCollection ¶
type ErrorCollection struct {
Errors []error `json:"errors"`
}
ErrorCollection represents a collection of errors
func (*ErrorCollection) Error ¶
func (ec *ErrorCollection) Error() string
Error implements the error interface for ErrorCollection
type ErrorHandler ¶
type ErrorHandler interface {
HandleError(err error) *StructuredError
FormatError(err error) string
GetSuggestions(err error) []string
IsRetryable(err error) bool
CreateValidationError(field, message string, suggestions ...string) *StructuredError
CreateFileError(operation, path string, cause error) *StructuredError
CreateConfigError(message string, cause error) *StructuredError
Wrap(err error, operation, context string) error
}
ErrorHandler interface for handling and formatting errors
type ErrorMiddleware ¶
type ErrorMiddleware struct {
// contains filtered or unexported fields
}
ErrorMiddleware provides consistent error handling across all commands Requirements: 21.5
func NewErrorMiddleware ¶
func NewErrorMiddleware(logger Logger, masker CredentialMasker) *ErrorMiddleware
NewErrorMiddleware creates a new error middleware with the provided credential masker. If you don't have a credential masker available, use NewErrorMiddlewareWithoutMasking instead.
func NewErrorMiddlewareWithoutMasking ¶
func NewErrorMiddlewareWithoutMasking(logger Logger) *ErrorMiddleware
NewErrorMiddlewareWithoutMasking creates a new error middleware without credential masking. This is useful in contexts where the security package cannot be imported due to import cycles. For production use, prefer NewErrorMiddleware with a proper CredentialMasker.
func (*ErrorMiddleware) GetSuggestions ¶
func (m *ErrorMiddleware) GetSuggestions(err error) []string
GetSuggestions gets fix suggestions for an error Requirements: 21.5
func (*ErrorMiddleware) Handle ¶
Handle wraps a command function with error handling middleware Provides: panic recovery, credential masking, logging Requirements: 21.5
func (*ErrorMiddleware) HandleError ¶
HandleError processes an error with masking and logging Requirements: 21.5
func (*ErrorMiddleware) HandleValidationErrors ¶
func (m *ErrorMiddleware) HandleValidationErrors(ctx context.Context, operation string, errs []*StructuredError) error
HandleValidationErrors handles multiple validation errors Requirements: 21.5
func (*ErrorMiddleware) IsRetryable ¶
func (m *ErrorMiddleware) IsRetryable(err error) bool
IsRetryable checks if an error is retryable Requirements: 21.5
func (*ErrorMiddleware) RecoverPanic ¶
func (m *ErrorMiddleware) RecoverPanic(ctx context.Context, operation string) error
RecoverPanic recovers from a panic and converts it to an error This can be used in goroutines or other contexts where defer is needed Requirements: 21.5
func (*ErrorMiddleware) WrapCommand ¶
func (m *ErrorMiddleware) WrapCommand(operation string, fn func(ctx context.Context) error) func(ctx context.Context) error
WrapCommand wraps a command function with full error handling This is a convenience function for use in Cobra commands Requirements: 21.5
type ErrorType ¶
type ErrorType string
ErrorType represents different categories of errors
const ( ValidationError ErrorType = "validation" PathError ErrorType = "path" PermissionError ErrorType = "permission" TemplateError ErrorType = "template" SOPSError ErrorType = "sops" ConfigError ErrorType = "config" NetworkError ErrorType = "network" FileError ErrorType = "file" SystemError ErrorType = "system" UserError ErrorType = "user" CloudError ErrorType = "cloud" CredentialError ErrorType = "credential" ServiceError ErrorType = "service" GenerationError ErrorType = "generation" OperationalError ErrorType = "operational" )
type ErrorWrapper ¶
type ErrorWrapper interface {
WrapError(err error, message string) error
WrapErrorWithContext(ctx context.Context, err error, message string) error
WrapErrorWithType(err error, errorType ErrorType, message string) error
UnwrapError(err error) error
}
ErrorWrapper interface for wrapping errors with context
type Logger ¶
type Logger interface {
Error(msg string, keysAndValues ...interface{})
Warn(msg string, keysAndValues ...interface{})
Info(msg string, keysAndValues ...interface{})
Debug(msg string, keysAndValues ...interface{})
}
Logger interface for logging errors
type MultiFieldAggregator ¶
type MultiFieldAggregator struct {
// contains filtered or unexported fields
}
MultiFieldAggregator aggregates errors by field name
func NewMultiFieldAggregator ¶
func NewMultiFieldAggregator() *MultiFieldAggregator
NewMultiFieldAggregator creates a new multi-field aggregator
func (*MultiFieldAggregator) AddFieldError ¶
func (mfa *MultiFieldAggregator) AddFieldError(field string, err error)
AddFieldError adds an error for a specific field
func (*MultiFieldAggregator) GetAllFieldErrors ¶
func (mfa *MultiFieldAggregator) GetAllFieldErrors() map[string][]error
GetAllFieldErrors returns all field errors
func (*MultiFieldAggregator) GetFieldErrors ¶
func (mfa *MultiFieldAggregator) GetFieldErrors(field string) []error
GetFieldErrors returns errors for a specific field
func (*MultiFieldAggregator) HasAnyErrors ¶
func (mfa *MultiFieldAggregator) HasAnyErrors() bool
HasAnyErrors returns true if there are any field errors
func (*MultiFieldAggregator) HasFieldErrors ¶
func (mfa *MultiFieldAggregator) HasFieldErrors(field string) bool
HasFieldErrors returns true if there are errors for the specified field
func (*MultiFieldAggregator) ToError ¶
func (mfa *MultiFieldAggregator) ToError() error
ToError converts all field errors to a single error
type StructuredError ¶
type StructuredError struct {
Type ErrorType `json:"type"`
Field string `json:"field,omitempty"`
Message string `json:"message"`
Cause error `json:"-"`
Suggestions []string `json:"suggestions,omitempty"`
Context map[string]interface{} `json:"context,omitempty"`
Retryable bool `json:"retryable"`
FilePath string `json:"file_path,omitempty"`
LineNumber int `json:"line_number,omitempty"`
ColumnNumber int `json:"column_number,omitempty"`
Operation string `json:"operation,omitempty"`
}
StructuredError represents a structured error with additional context
func CreateCloudError ¶
func CreateCloudError(provider, operation, message string, cause error) *StructuredError
CreateCloudError creates a cloud provider-related error
func CreateConfigError ¶
func CreateConfigError(field, message string, cause error) *StructuredError
CreateConfigError creates a configuration-related error
func CreateCredentialError ¶
func CreateCredentialError(credentialType, field, message string, cause error) *StructuredError
CreateCredentialError creates a credential-related error
func CreateFileError ¶
func CreateFileError(operation, path string, cause error) *StructuredError
CreateFileError creates a file operation error with context
func CreateGenerationError ¶
func CreateGenerationError(stage, message string, cause error) *StructuredError
CreateGenerationError creates a GitOps generation-related error
func CreateGenerationErrorWithFile ¶
func CreateGenerationErrorWithFile(stage, filePath string, message string, cause error) *StructuredError
CreateGenerationErrorWithFile creates a GitOps generation error with file context
func CreatePathError ¶
func CreatePathError(path, message string, cause error) *StructuredError
CreatePathError creates a path-related error with suggestions
func CreatePermissionError ¶
func CreatePermissionError(resource, operation string, cause error) *StructuredError
CreatePermissionError creates a permission-related error
func CreateSOPSError ¶
func CreateSOPSError(operation, message string, cause error) *StructuredError
CreateSOPSError creates a SOPS-related error with suggestions
func CreateServiceError ¶
func CreateServiceError(serviceName, operation, message string, cause error) *StructuredError
CreateServiceError creates a service-related error
func CreateTemplateError ¶
func CreateTemplateError(templatePath string, lineNumber int, message string, cause error) *StructuredError
CreateTemplateError creates a template-related error with file context
func CreateTemplateErrorWithColumn ¶
func CreateTemplateErrorWithColumn(templatePath string, lineNumber, columnNumber int, message string, cause error) *StructuredError
CreateTemplateErrorWithColumn creates a template error with full file context
func CreateValidationError ¶
func CreateValidationError(field, message string, suggestions ...string) *StructuredError
CreateValidationError creates a validation error with suggestions
func (*StructuredError) Error ¶
func (e *StructuredError) Error() string
Error implements the error interface
func (*StructuredError) Is ¶
func (e *StructuredError) Is(target error) bool
Is checks if the target error matches this error's type
func (*StructuredError) Unwrap ¶
func (e *StructuredError) Unwrap() error
Unwrap returns the underlying cause error
type ValidationAggregator ¶
type ValidationAggregator struct {
*DefaultErrorAggregator
// contains filtered or unexported fields
}
ValidationAggregator is a specialized aggregator for validation errors
func NewValidationAggregator ¶
func NewValidationAggregator() *ValidationAggregator
NewValidationAggregator creates a new validation aggregator
func (*ValidationAggregator) AddWarning ¶
func (va *ValidationAggregator) AddWarning(err error)
AddWarning adds a warning to the aggregator
func (*ValidationAggregator) AddWarningWithContext ¶
func (va *ValidationAggregator) AddWarningWithContext(field string, err error)
AddWarningWithContext adds a warning with field context
func (*ValidationAggregator) ClearAll ¶
func (va *ValidationAggregator) ClearAll()
ClearAll removes all errors and warnings from the aggregator
func (*ValidationAggregator) ClearWarnings ¶
func (va *ValidationAggregator) ClearWarnings()
ClearWarnings removes all warnings from the aggregator
func (*ValidationAggregator) GetWarnings ¶
func (va *ValidationAggregator) GetWarnings() []error
GetWarnings returns all warnings
func (*ValidationAggregator) HasWarnings ¶
func (va *ValidationAggregator) HasWarnings() bool
HasWarnings returns true if there are any warnings
func (*ValidationAggregator) ToValidationResult ¶
func (va *ValidationAggregator) ToValidationResult() *ValidationResult
ToValidationResult converts the aggregated errors and warnings to a validation result
type ValidationResult ¶
type ValidationResult struct {
Valid bool `json:"valid"`
Errors []*StructuredError `json:"errors,omitempty"`
Warnings []*StructuredError `json:"warnings,omitempty"`
}
ValidationResult represents the result of a validation operation
func (*ValidationResult) HasErrors ¶
func (vr *ValidationResult) HasErrors() bool
HasErrors returns true if there are validation errors
func (*ValidationResult) HasWarnings ¶
func (vr *ValidationResult) HasWarnings() bool
HasWarnings returns true if there are validation warnings
func (*ValidationResult) ToError ¶
func (vr *ValidationResult) ToError() error
ToError converts the validation result to an error if there are errors